#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int open_pagemap(pid_t pid) {
  char filename[30];
  if (0 > snprintf(filename, 30, "/proc/%d/pagemap", pid)) {
    fprintf(stderr, "Cant create file name\n");
    return -1;
  }
  return open(filename, O_RDONLY);
}


x86 assembly (clang, -O0):
<open_pagemap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1e,%esi
lea    0xeb2(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    1182 <open_pagemap+0x52>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <open_pagemap+0x67>
lea    -0x30(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_pagemap>:
sub    $0x28,%rsp
mov    %edi,%ecx
lea    0xec3(%rip),%rdx
mov    %rsp,%rdi
mov    $0x1e,%esi
call   1030 <snprintf@plt>
test   %rax,%rax
js     1165 <open_pagemap+0x35>
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1050 <open@plt>
add    $0x28,%rsp
ret
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
lea    0xe9c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/pagemap\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Cant create file name\0A\00"
@O_RDONLY = global i32 0
define i32 @open_pagemap(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [30 x i8]
store i32 %0, i32* %3
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* %3
%7 = call i64 @snprintf(i8* %5, i32 30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp sgt i64 0, %7
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %16
12: ; preds = %1
%13 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %2
br label %16
16: ; preds = %12, %9
%17 = load i32, i32* %2
ret i32 %17
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/pagemap\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Cant create file name\0A\00"
@O_RDONLY = global i32 0
define i32 @open_pagemap(i32 %0) {
%2 = alloca [30 x i8]
%3 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%4 = call i64 @snprintf(i8* %3, i32 30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %12
9: ; preds = %1
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %3, i32 %10)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ -1, %6 ], [ %11, %9 ]
ret i32 %13
}