#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, void **, void **, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
void *vsyscall_getcpu(void) {
  FILE *maps;
  char line[128];
  bool found = false;
  maps = fopen("/proc/self/maps", "r");
  if (!maps)
    return NULL;
  while (fgets(line, 128, maps)) {
    char r, x;
    void *start, *end;
    char name[128];
    if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s", &start, &end, &r, &x,
               name) != 5)
      continue;
    if (strcmp(name, "[vsyscall]"))
      continue;
    found = true;
    break;
  }
  fclose(maps);
  if (!found) {
    printf("Warning: failed to find vsyscall getcpu\n");
    return NULL;
  }
  return (void *)(0xffffffffff600800);
}


x86 assembly (clang, -O0):
<vsyscall_getcpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x94(%rbp)
lea    0xe84(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <vsyscall_getcpu+0x44>
movq   $0x0,-0x8(%rbp)
jmp    1276 <vsyscall_getcpu+0x116>
jmp    11a9 <vsyscall_getcpu+0x49>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     123c <vsyscall_getcpu+0xdc>
lea    -0x90(%rbp),%rdi
lea    -0x130(%rbp),%rax
lea    0xe35(%rip),%rsi
lea    -0xa0(%rbp),%rdx
lea    -0xa8(%rbp),%rcx
lea    -0x95(%rbp),%r8
lea    -0x96(%rbp),%r9
mov    %rax,(%rsp)
call   1070 <sscanf@plt>
cmp    $0x5,%eax
je     1210 <vsyscall_getcpu+0xb0>
jmp    11a9 <vsyscall_getcpu+0x49>
lea    -0x130(%rbp),%rdi
lea    0xe14(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1232 <vsyscall_getcpu+0xd2>
jmp    11a9 <vsyscall_getcpu+0x49>
movl   $0x1,-0x94(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x94(%rbp)
jne    126b <vsyscall_getcpu+0x10b>
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1276 <vsyscall_getcpu+0x116>
mov    $0xffffffffff600800,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsyscall_getcpu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
lea    0xe88(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1246 <vsyscall_getcpu+0xe6>
mov    %rax,%r12
lea    0x20(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
sete   %bpl
je     1225 <vsyscall_getcpu+0xc5>
lea    0xa0(%rsp),%r14
lea    0x20(%rsp),%rbx
lea    0xf(%rsp),%r15
lea    0xe(%rsp),%r13
jmp    11e9 <vsyscall_getcpu+0x89>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
sete   %bpl
je     1225 <vsyscall_getcpu+0xc5>
mov    %r14,(%rsp)
mov    %rbx,%rdi
lea    0xe1b(%rip),%rsi
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %r15,%r8
mov    %r13,%r9
call   1070 <sscanf@plt>
cmp    $0x5,%eax
jne    11d0 <vsyscall_getcpu+0x70>
mov    %r14,%rdi
lea    0xe17(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11d0 <vsyscall_getcpu+0x70>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    $0xffffffffff600800,%rax
test   $0x1,%bpl
je     1248 <vsyscall_getcpu+0xe8>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"%p-%p %c-%cp %*x %*x:%*x %*u %s\00"
@.str.3 = [11 x i8] c"[vsyscall]\00"
@.str.4 = [41 x i8] c"Warning: failed to find vsyscall getcpu\0A\00"
define i8* @vsyscall_getcpu() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [128 x i8]
store i32 0, i32* %4
%10 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %2
%11 = load i32*, i32** %2
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %0
store i8* null, i8** %1
br label %40
14: ; preds = %0
br label %15
15: ; preds = %30, %25, %14
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %2
%18 = call i64 @fgets(i8* %16, i32 128, i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %15
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%23 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8** %7, i8** %8, i8* %5, i8* %6, i8* %22)
%24 = icmp ne i32 %23, 5
br i1 %24, label %25, label %26
25: ; preds = %20
br label %15
26: ; preds = %20
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %15
31: ; preds = %26
store i32 1, i32* %4
br label %32
32: ; preds = %31, %15
%33 = load i32*, i32** %2
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %4
%36 = icmp ne i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %1
br label %40
39: ; preds = %32
store i8* inttoptr (i64 -10483712 to i8*), i8** %1
br label %40
40: ; preds = %39, %37, %13
%41 = load i8*, i8** %1
ret i8* %41
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"%p-%p %c-%cp %*x %*x:%*x %*u %s\00"
@.str.3 = [11 x i8] c"[vsyscall]\00"
@str = [40 x i8] c"Warning: failed to find vsyscall getcpu\00"
define i8* @vsyscall_getcpu() {
%1 = alloca [128 x i8]
%2 = alloca i8
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [128 x i8]
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %32, label %10
10: ; preds = %0
%11 = bitcast i8** %4 to i8*
%12 = bitcast i8** %5 to i8*
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%14 = call i64 @fgets(i8* %7, i32 128, i32* %8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %10, %24
%17 = phi i1 [ %26, %24 ], [ %15, %10 ]
%18 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8** %4, i8** %5, i8* %2, i8* %3, i8* %13)
%19 = icmp eq i32 %18, 5
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
br label %27
24: ; preds = %20, %16
%25 = call i64 @fgets(i8* %7, i32 128, i32* %8)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %16
27: ; preds = %24, %10, %23
%28 = phi i1 [ %17, %23 ], [ %15, %10 ], [ %26, %24 ]
%29 = call i32 @fclose(i32* %8)
br i1 %28, label %30, label %32
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %27, %0, %30
%33 = phi i8* [ null, %30 ], [ null, %0 ], [ inttoptr (i64 -10483712 to i8*), %27 ]
ret i8* %33
}