I'm trying to get output similar to uname -m.
Why not look at how uname does it? Run apt source coreutils and in src/uname.c:
...
while ((c = getopt_long (argc, argv, "asnrvmpio",
uname_long_options, NULL)) != -1)
...
case 'm':
toprint |= PRINT_MACHINE;
and
if (uname (&name) == -1)
...
if (toprint & PRINT_MACHINE)
print_element (name.machine);
(notice the usage of the uname function).
uname is a syscall defined by POSIX and wrapped in glibc:
#include <sys/utsname.h>
int uname(struct utsname *buf);
and will store the machine hardware identifier in buf->machine, which is what's printed by uname -m.
As an aside - in lib/uname.c you can see coreutils' implementation of a custom uname function for Windows systems (which don't support the uname syscall). This implementation uses the GetVersionEx and GetSystemInfo WinAPIs which provide similar functionality.