#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*/ line;
typedef int /*<<< orphan*/ filename;
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*/ snprintf(char *, int, char *, int);
scalar_t__ strstr(char *, char *);
char *strtok(char *, char *);
long strtoul(char *, int /*<<< orphan*/ *, int);
long get_mod_base(pid_t pid, char *mod_name) {
  FILE *fp;
  long addr = 0;
  char *pch;
  char filename[32];
  char line[1024];
  if (pid < 0) {
    snprintf(filename, sizeof(filename), "/proc/self/maps", pid);
  } else {
    snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
  }
  fp = fopen(filename, "r");
  if (fp != NULL) {
    while (fgets(line, sizeof(line), fp)) {
      if (strstr(line, mod_name)) {
        pch = strtok(line, "-");
        addr = strtoul(pch, NULL, 16);
        break;
      }
    }
    fclose(fp);
  }
  return addr;
}


x86 assembly (clang, -O0):
<get_mod_base>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11b1 <get_mod_base+0x41>
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x20,%esi
lea    0xe59(%rip),%rdx
call   1040 <snprintf@plt>
jmp    11c9 <get_mod_base+0x59>
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x20,%esi
lea    0xe4c(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1266 <get_mod_base+0xf6>
jmp    11ed <get_mod_base+0x7d>
lea    -0x450(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     125d <get_mod_base+0xed>
lea    -0x450(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1258 <get_mod_base+0xe8>
lea    -0x450(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %rax,-0x20(%rbp)
jmp    125d <get_mod_base+0xed>
jmp    11ed <get_mod_base+0x7d>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mod_base>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
test   %edi,%edi
lea    0xe74(%rip),%rax
lea    0xe7d(%rip),%rdx
cmovs  %rax,%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1222 <get_mod_base+0xb2>
mov    %rax,%r12
lea    0x20(%rsp),%rbx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1218 <get_mod_base+0xa8>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11d0 <get_mod_base+0x60>
lea    0xe24(%rip),%rsi
lea    0x20(%rsp),%rdi
call   1070 <strtok@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1030 <fclose@plt>
jmp    1225 <get_mod_base+0xb5>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [14 x i8] c"/proc/%d/maps\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"-\00"
define i64 @get_mod_base(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca [32 x i8]
%9 = alloca [1024 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
store i64 0, i64* %6
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* %3
%15 = call i32 @snprintf(i8* %13, i32 32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %20
16: ; preds = %2
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%18 = load i32, i32* %3
%19 = call i32 @snprintf(i8* %17, i32 32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %16, %12
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %45
25: ; preds = %20
br label %26
26: ; preds = %41, %25
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%28 = load i32*, i32** %5
%29 = call i64 @fgets(i8* %27, i32 1024, i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %26
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%33 = load i8*, i8** %4
%34 = call i64 @strstr(i8* %32, i8* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%38 = call i8* @strtok(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = call i64 @strtoul(i8* %39, i32* null, i32 16)
store i64 %40, i64* %6
br label %42
41: ; preds = %31
br label %26
42: ; preds = %36, %26
%43 = load i32*, i32** %5
%44 = call i32 @fclose(i32* %43)
br label %45
45: ; preds = %42, %20
%46 = load i64, i64* %6
ret i64 %46
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [14 x i8] c"/proc/%d/maps\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"-\00"
define i64 @get_mod_base(i32 %0, i8* %1) {
%3 = alloca [32 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 0
%8 = select i1 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0)
%9 = call i32 @snprintf(i8* %5, i32 32, i8* %8, i32 %0)
%10 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %24, label %12
12: ; preds = %2, %15
%13 = call i64 @fgets(i8* %6, i32 1024, i32* %10)
%14 = icmp eq i64 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = call i64 @strstr(i8* %6, i8* %1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %12, label %18
18: ; preds = %15
%19 = call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = call i64 @strtoul(i8* nocapture %19, i32* null, i32 16)
br label %21
21: ; preds = %12, %18
%22 = phi i64 [ %20, %18 ], [ 0, %12 ]
%23 = call i32 @fclose(i32* %10)
br label %24
24: ; preds = %21, %2
%25 = phi i64 [ %22, %21 ], [ 0, %2 ]
ret i64 %25
}