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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ getwd(char *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
unsigned long long strtoll(char *, char **, int);
unsigned long long int libc_posiz(pid_t pid) {
  char shellcommand[120];
  char numero[60];
  char directory[PATH_MAX];
  int reto;
  FILE *leggi;
  getwd(directory);
  sprintf(shellcommand, "%s/mrmaps %d", directory, pid);
  leggi = popen(shellcommand, "re");
  if (leggi == NULL) {
    printf("Error locating libc. (0)\n");
    exit(-1);
  }
  fread(numero, sizeof(char), 60, leggi);
  reto = pclose(leggi);
  if (reto == 666) {
    printf("Error locating libc. (1)\n");
    exit(-2);
  }
  return strtoll(numero, (char **)NULL, 16);
}


x86 assembly (clang, -O0):
<libc_posiz>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0xc8(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0xe8(%rbp)
mov    %rdi,%rsp
mov    %rax,-0xd0(%rbp)
call   1070 <getwd@plt>
mov    -0xe8(%rbp),%rdx
lea    -0x80(%rbp),%rdi
mov    -0x4(%rbp),%ecx
lea    0xe23(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0xe0(%rbp)
cmpq   $0x0,-0xe0(%rbp)
jne    121d <libc_posiz+0x9d>
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0xc0(%rbp),%rdi
mov    -0xe0(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3c,%edx
call   1030 <fread@plt>
mov    -0xe0(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0xd4(%rbp)
cmpl   $0x29a,-0xd4(%rbp)
jne    1272 <libc_posiz+0xf2>
lea    0xdc7(%rip),%rdi
call   1040 <printf@plt>
mov    $0xfffffffe,%edi
call   10a0 <exit@plt>
lea    -0xc0(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtoll@plt>
mov    -0xc8(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libc_posiz>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xc0,%rsp
mov    %edi,%r14d
mov    %rsp,%r12
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1070 <getwd@plt>
lea    0xe3f(%rip),%rsi
lea    -0xe0(%rbp),%r15
mov    %r15,%rdi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1090 <sprintf@plt>
lea    0xe30(%rip),%rsi
mov    %r15,%rdi
call   1080 <popen@plt>
test   %rax,%rax
je     1232 <libc_posiz+0xb2>
mov    %rax,%rbx
lea    -0x60(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3c,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <pclose@plt>
cmp    $0x29a,%eax
je     1248 <libc_posiz+0xc8>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtoll@plt>
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xdc1(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"%s/mrmaps %d\00"
@.str.1 = [3 x i8] c"re\00"
@.str.2 = [26 x i8] c"Error locating libc. (0)\0A\00"
@.str.3 = [26 x i8] c"Error locating libc. (1)\0A\00"
define i64 @libc_posiz(i32 %0) {
%2 = alloca i32
%3 = alloca [120 x i8]
%4 = alloca [60 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32*
store i32 %0, i32* %2
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = call i32 @getwd(i8* %12)
%14 = getelementptr inbounds [120 x i8], [120 x i8]* %3, i64 0, i64 0
%15 = load i32, i32* %2
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %15)
%17 = getelementptr inbounds [120 x i8], [120 x i8]* %3, i64 0, i64 0
%18 = call i32* @popen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %1
%22 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %1
%25 = getelementptr inbounds [60 x i8], [60 x i8]* %4, i64 0, i64 0
%26 = load i32*, i32** %8
%27 = call i32 @fread(i8* %25, i32 1, i32 60, i32* %26)
%28 = load i32*, i32** %8
%29 = call i32 @pclose(i32* %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, 666
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 -2)
unreachable
35: ; preds = %24
%36 = getelementptr inbounds [60 x i8], [60 x i8]* %4, i64 0, i64 0
%37 = call i64 @strtoll(i8* %36, i8** null, i32 16)
%38 = load i8*, i8** %5
ret i64 %37
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"%s/mrmaps %d\00"
@.str.1 = [3 x i8] c"re\00"
@str = [25 x i8] c"Error locating libc. (1)\00"
@str.4 = [25 x i8] c"Error locating libc. (0)\00"
define i64 @libc_posiz(i32 %0) {
%2 = alloca [120 x i8]
%3 = alloca [60 x i8]
%4 = getelementptr inbounds [120 x i8], [120 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [60 x i8], [60 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @getwd(i8* %9)
%11 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %0)
%12 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %1
%18 = call i32 @fread(i8* %5, i32 1, i32 60, i32* %12)
%19 = call i32 @pclose(i32* %12)
%20 = icmp eq i32 %19, 666
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%23 = call i32 @exit(i32 -2)
unreachable
24: ; preds = %17
%25 = call i64 @strtoll(i8* nocapture %5, i8** null, i32 16)
ret i64 %25
}