#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, long *);
int /*<<< orphan*/ *strstr(char *, char *);
long getlibcaddr(pid_t pid) {
  FILE *fp;
  char filename[30];
  char line[850];
  long addr;
  char perms[5];
  char *modulePath;
  sprintf(filename, "/proc/%d/maps", pid);
  fp = fopen(filename, "r");
  if (fp == NULL)
    exit(1);
  while (fgets(line, 850, fp) != NULL) {
    sscanf(line, "%lx-%*lx %*s %*s %*s %*d", &addr);
    if (strstr(line, "libc-") != NULL) {
      break;
    }
  }
  fclose(fp);
  return addr;
}


x86 assembly (clang, -O0):
<getlibcaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3b0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe74(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <getlibcaddr+0x4a>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11bf <getlibcaddr+0x4f>
lea    -0x390(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x352,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     121f <getlibcaddr+0xaf>
lea    -0x390(%rbp),%rdi
lea    0xe24(%rip),%rsi
lea    -0x398(%rbp),%rdx
call   1050 <sscanf@plt>
lea    -0x390(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     121a <getlibcaddr+0xaa>
jmp    121f <getlibcaddr+0xaf>
jmp    11bf <getlibcaddr+0x4f>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x398(%rbp),%rax
add    $0x3b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlibcaddr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x390,%rsp
mov    %edi,%edx
lea    0xe77(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1221 <getlibcaddr+0xb1>
mov    %rax,%r14
lea    0x30(%rsp),%rbx
lea    0xe57(%rip),%r15
lea    0x8(%rsp),%r12
lea    0xe64(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x352,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1203 <getlibcaddr+0x93>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <sscanf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11d0 <getlibcaddr+0x60>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rax
add    $0x390,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"%lx-%*lx %*s %*s %*s %*d\00"
@.str.3 = [6 x i8] c"libc-\00"
define i64 @getlibcaddr(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [30 x i8]
%5 = alloca [850 x i8]
%6 = alloca i64
%7 = alloca [5 x i8]
%8 = alloca i8*
store i32 %0, i32* %2
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %4, 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** %3
%14 = load i32*, i32** %3
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
br label %19
19: ; preds = %31, %18
%20 = getelementptr inbounds [850 x i8], [850 x i8]* %5, i64 0, i64 0
%21 = load i32*, i32** %3
%22 = call i32* @fgets(i8* %20, i32 850, i32* %21)
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = getelementptr inbounds [850 x i8], [850 x i8]* %5, i64 0, i64 0
%26 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64* %6)
%27 = getelementptr inbounds [850 x i8], [850 x i8]* %5, i64 0, i64 0
%28 = call i32* @strstr(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %31
30: ; preds = %24
br label %32
31: ; preds = %24
br label %19
32: ; preds = %30, %19
%33 = load i32*, i32** %3
%34 = call i32 @fclose(i32* %33)
%35 = load i64, i64* %6
ret i64 %35
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"%lx-%*lx %*s %*s %*s %*d\00"
@.str.3 = [6 x i8] c"libc-\00"
define i64 @getlibcaddr(i32 %0) {
%2 = alloca [30 x i8]
%3 = alloca [850 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [850 x i8], [850 x i8]* %3, i64 0, i64 0
%7 = bitcast i64* %4 to i8*
%8 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %1, %16
%14 = call i32* @fgets(i8* %6, i32 850, i32* %9)
%15 = icmp eq i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64* %4)
%18 = call i32* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %13, label %20
20: ; preds = %16, %13
%21 = call i32 @fclose(i32* %9)
%22 = load i64, i64* %4
ret i64 %22
}