#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
unsigned long long readlines(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
unsigned long long getOffset(pid_t pid) {
  FILE *mapsfile;
  char path[100];
  char *buff = NULL;
  unsigned long long res;
  snprintf(path, 100, "/proc/%d/maps", (int)pid);
  mapsfile = fopen(path, "r");
  if (mapsfile == NULL)
    return 0;
  res = readlines(mapsfile, buff);
  free(buff);
  fclose(mapsfile);
  return res;
}


x86 assembly (clang, -O0):
<getOffset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    $0x64,%esi
lea    0xe80(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b1 <getOffset+0x61>
movq   $0x0,-0x8(%rbp)
jmp    11e8 <getOffset+0x98>
mov    -0x18(%rbp),%rdi
mov    -0x88(%rbp),%rsi
call   1070 <readlines@plt>
mov    %rax,-0x90(%rbp)
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x90(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getOffset>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%rcx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <snprintf@plt>
lea    0xe96(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a6 <getOffset+0x56>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   1070 <readlines@plt>
mov    %rax,%r14
xor    %edi,%edi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11a9 <getOffset+0x59>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
define i64 @getOffset(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32*
%5 = alloca [100 x i8]
%6 = alloca i8*
%7 = alloca i64
store i64 %0, i64* %3
store i8* null, i8** %6
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%9 = load i64, i64* %3
%10 = trunc i64 %9 to i32
%11 = call i32 @snprintf(i8* %8, i32 100, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i64 0, i64* %2
br label %26
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = load i8*, i8** %6
%20 = call i64 @readlines(i32* %18, i8* %19)
store i64 %20, i64* %7
%21 = load i8*, i8** %6
%22 = call i32 @free(i8* %21)
%23 = load i32*, i32** %4
%24 = call i32 @fclose(i32* %23)
%25 = load i64, i64* %7
store i64 %25, i64* %2
br label %26
26: ; preds = %17, %16
%27 = load i64, i64* %2
ret i64 %27
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
define i64 @getOffset(i64 %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = trunc i64 %0 to i32
%5 = call i32 @snprintf(i8* %3, i32 100, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = call i64 @readlines(i32* %6, i8* null)
%10 = call i32 @free(i8* null)
%11 = call i32 @fclose(i32* %6)
br label %12
12: ; preds = %1, %8
%13 = phi i64 [ %9, %8 ], [ 0, %1 ]
ret i64 %13
}