#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ numa_maps_file;
typedef int /*<<< orphan*/ node_info;
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ addr_in_string;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char const *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
int /*<<< orphan*/ printf(char *, unsigned long, int);
int /*<<< orphan*/ snprintf(char const *, int, char *, int);
int strlen(char const *);
int /*<<< orphan*/ *strstr(char const *, char const *);
char *strtok(char const *, char *);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned long get_npages_from_numa_maps(void *addr, int node) {
  int pid;
  char numa_maps_file[32];
  const char line[1024];
  char addr_in_string[64];
  const char node_info[16];
  char *ptr_addr_in_string;
  char *string_ptr;
  char *temp_str_ptr;
  unsigned long npages = 0;
  ptr_addr_in_string = addr_in_string;
  pid = getpid();
  snprintf(addr_in_string, sizeof(addr_in_string), "%p", addr);
  snprintf(numa_maps_file, sizeof(numa_maps_file), "/proc/%d/numa_maps", pid);
  snprintf(node_info, sizeof(node_info), "N%d=", node);
  ptr_addr_in_string += 2;
  FILE *numa_maps = fopen(numa_maps_file, "r");
  if (numa_maps == NULL) {
    exit(-1);
  }
  while (fgets(line, sizeof(line), numa_maps)) {
    if (strstr(line, ptr_addr_in_string) != NULL) {
      string_ptr = strtok(line, " ");
      while (string_ptr != NULL) {
        temp_str_ptr = string_ptr;
        if (strstr(temp_str_ptr, node_info)) {
          npages = strtoul((string_ptr + strlen(node_info)), NULL, 0);
          printf("%lu pages allocated in Node %d\n", npages, node);
          return npages;
        }
        string_ptr = strtok(NULL, " ");
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_npages_from_numa_maps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x4b0(%rbp)
lea    -0x480(%rbp),%rax
mov    %rax,-0x498(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
lea    -0x480(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xe12(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    $0x20,%esi
lea    0xdfd(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x490(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xdf5(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x498(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x498(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xdd8(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x4b8(%rbp)
cmpq   $0x0,-0x4b8(%rbp)
jne    1267 <get_npages_from_numa_maps+0xc7>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    126c <get_npages_from_numa_maps+0xcc>
lea    -0x440(%rbp),%rdi
mov    -0x4b8(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1382 <get_npages_from_numa_maps+0x1e2>
lea    -0x440(%rbp),%rdi
mov    -0x498(%rbp),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     137d <get_npages_from_numa_maps+0x1dd>
lea    -0x440(%rbp),%rdi
lea    0xd64(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x4a0(%rbp)
cmpq   $0x0,-0x4a0(%rbp)
je     1378 <get_npages_from_numa_maps+0x1d8>
mov    -0x4a0(%rbp),%rax
mov    %rax,-0x4a8(%rbp)
mov    -0x4a8(%rbp),%rdi
lea    -0x490(%rbp),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     135c <get_npages_from_numa_maps+0x1bc>
mov    -0x4a0(%rbp),%rax
mov    %rax,-0x4c0(%rbp)
lea    -0x490(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c0(%rbp),%rdi
cltq
add    %rax,%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    %rax,-0x4b0(%rbp)
mov    -0x4b0(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xcd8(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4b0(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    138a <get_npages_from_numa_maps+0x1ea>
xor    %eax,%eax
mov    %eax,%edi
lea    0xcb6(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x4a0(%rbp)
jmp    12c5 <get_npages_from_numa_maps+0x125>
jmp    137d <get_npages_from_numa_maps+0x1dd>
jmp    126c <get_npages_from_numa_maps+0xcc>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x4c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_npages_from_numa_maps>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x488,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
lea    0xe39(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x40,%esi
mov    %ebx,%ecx
call   1060 <snprintf@plt>
lea    0xe24(%rip),%rdx
lea    0x60(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %ebp,%ecx
call   1060 <snprintf@plt>
lea    0xe1c(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
lea    0xe08(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     131a <get_npages_from_numa_maps+0x17a>
mov    %rax,%rbp
lea    0x80(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    $0xffffffffffffffff,%r12
test   %rax,%rax
je     1305 <get_npages_from_numa_maps+0x165>
mov    %r14d,0xc(%rsp)
lea    0x80(%rsp),%r13
lea    0xdbd(%rip),%rbx
lea    0x10(%rsp),%r14
jmp    1289 <get_npages_from_numa_maps+0xe9>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x400,%esi
mov    %rbp,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1305 <get_npages_from_numa_maps+0x165>
mov    %r13,%rdi
lea    0x22(%rsp),%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     1270 <get_npages_from_numa_maps+0xd0>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
je     1270 <get_npages_from_numa_maps+0xd0>
mov    %rax,%r15
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r14,%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
jne    12d4 <get_npages_from_numa_maps+0x134>
xor    %edi,%edi
mov    %rbx,%rsi
call   1090 <strtok@plt>
mov    %rax,%r15
test   %rax,%rax
jne    12b0 <get_npages_from_numa_maps+0x110>
jmp    1270 <get_npages_from_numa_maps+0xd0>
lea    0x10(%rsp),%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%r15
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    %rax,%r12
lea    0xd26(%rip),%rdi
mov    %rax,%rsi
mov    0xc(%rsp),%edx
call   1050 <printf@plt>
mov    %r12,%rax
add    $0x488,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%p\00"
@.str.1 = [19 x i8] c"/proc/%d/numa_maps\00"
@.str.2 = [5 x i8] c"N%d=\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c" \00"
@.str.5 = [32 x i8] c"%lu pages allocated in Node %d\0A\00"
define i64 @get_npages_from_numa_maps(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca [1024 x i8]
%9 = alloca [64 x i8]
%10 = alloca [16 x i8]
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 0, i64* %14
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
store i8* %16, i8** %11
%17 = call i32 (...) @getpid()
store i32 %17, i32* %6
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%19 = load i8*, i8** %4
%20 = ptrtoint i8* %19 to i32
%21 = call i32 @snprintf(i8* %18, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* %6
%24 = call i32 @snprintf(i8* %22, i32 32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%26 = load i32, i32* %5
%27 = call i32 @snprintf(i8* %25, i32 16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %11
%29 = getelementptr inbounds i8, i8* %28, i64 2
store i8* %29, i8** %11
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %31, i32** %15
%32 = load i32*, i32** %15
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %2
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %2
br label %37
37: ; preds = %73, %36
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%39 = load i32*, i32** %15
%40 = call i64 @fgets(i8* %38, i32 1024, i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %74
42: ; preds = %37
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = load i8*, i8** %11
%45 = call i32* @strstr(i8* %43, i8* %44)
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %73
47: ; preds = %42
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%49 = call i8* @strtok(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %49, i8** %12
br label %50
50: ; preds = %70, %47
%51 = load i8*, i8** %12
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %72
53: ; preds = %50
%54 = load i8*, i8** %12
store i8* %54, i8** %13
%55 = load i8*, i8** %13
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%57 = call i32* @strstr(i8* %55, i8* %56)
%58 = icmp ne i32* %57, null
br i1 %58, label %59, label %70
59: ; preds = %53
%60 = load i8*, i8** %12
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%62 = call i32 @strlen(i8* %61)
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
%65 = call i64 @strtoul(i8* %64, i32* null, i32 0)
store i64 %65, i64* %14
%66 = load i64, i64* %14
%67 = load i32, i32* %5
%68 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i64 %66, i32 %67)
%69 = load i64, i64* %14
store i64 %69, i64* %3
br label %75
70: ; preds = %53
%71 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %71, i8** %12
br label %50
72: ; preds = %50
br label %73
73: ; preds = %72, %42
br label %37
74: ; preds = %37
store i64 -1, i64* %3
br label %75
75: ; preds = %74, %59
%76 = load i64, i64* %3
ret i64 %76
}

LLVM IR (-O2):
@.str = [3 x i8] c"%p\00"
@.str.1 = [19 x i8] c"/proc/%d/numa_maps\00"
@.str.2 = [5 x i8] c"N%d=\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c" \00"
@.str.5 = [32 x i8] c"%lu pages allocated in Node %d\0A\00"
define i64 @get_npages_from_numa_maps(i8* %0, i32 %1) {
%3 = alloca [32 x i8]
%4 = alloca [1024 x i8]
%5 = alloca [64 x i8]
%6 = alloca [16 x i8]
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%11 = tail call i32 (...) @getpid()
%12 = ptrtoint i8* %0 to i64
%13 = trunc i64 %12 to i32
%14 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @snprintf(i8* %7, i32 32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%16 = call i32 @snprintf(i8* %10, i32 16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 2
%18 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %2
%21 = call i64 @fgets(i8* %8, i32 1024, i32* %18)
%22 = icmp eq i64 %21, 0
br i1 %22, label %47, label %25
23: ; preds = %2
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %20, %44
%26 = call i32* @strstr(i8* %8, i8* %17)
%27 = icmp eq i32* %26, null
br i1 %27, label %44, label %28
28: ; preds = %25
%29 = call i8* @strtok(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %44, label %31
31: ; preds = %28, %41
%32 = phi i8* [ %42, %41 ], [ %29, %28 ]
%33 = call i32* @strstr(i8* %32, i8* %10)
%34 = icmp eq i32* %33, null
br i1 %34, label %41, label %35
35: ; preds = %31
%36 = call i32 @strlen(i8* %10)
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %32, i64 %37
%39 = call i64 @strtoul(i8* nocapture %38, i32* null, i32 0)
%40 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i64 %39, i32 %1)
br label %47
41: ; preds = %31
%42 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %31
44: ; preds = %41, %28, %25
%45 = call i64 @fgets(i8* %8, i32 1024, i32* %18)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %25
47: ; preds = %44, %20, %35
%48 = phi i64 [ %39, %35 ], [ -1, %20 ], [ -1, %44 ]
ret i64 %48
}