#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 ulong;
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 const *);
char *strtok(char *, char *);
int strtoul(char *, int /*<<< orphan*/ *, int);
ulong get_module_base(pid_t pid, const char *module_name) {
  FILE *fp;
  ulong 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, "rb");
  if (fp != NULL) {
    while (fgets(line, sizeof(line), fp)) {
      if (strstr(line, module_name)) {
        pch = strtok(line, "-");
        addr = strtoul(pch, NULL, 16);
        if (addr == 0x8000)
          addr = 0;
        break;
      }
    }
    fclose(fp);
  }
  return addr;
}


x86 assembly (clang, -O0):
<get_module_base>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11b0 <get_module_base+0x40>
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x20,%esi
lea    0xe5a(%rip),%rdx
call   1040 <snprintf@plt>
jmp    11c8 <get_module_base+0x58>
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x20,%esi
lea    0xe4d(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1278 <get_module_base+0x108>
jmp    11ec <get_module_base+0x7c>
lea    -0x450(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     126f <get_module_base+0xff>
lea    -0x450(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     126a <get_module_base+0xfa>
lea    -0x450(%rbp),%rdi
lea    0xdee(%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    %eax,-0x1c(%rbp)
cmpl   $0x8000,-0x1c(%rbp)
jne    1265 <get_module_base+0xf5>
movl   $0x0,-0x1c(%rbp)
jmp    126f <get_module_base+0xff>
jmp    11ec <get_module_base+0x7c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_module_base>:
push   %r15
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
test   %edi,%edi
lea    0xe76(%rip),%rax
lea    0xe7f(%rip),%rdx
cmovs  %rax,%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1225 <get_module_base+0xb5>
mov    %rax,%r15
lea    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1229 <get_module_base+0xb9>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11d0 <get_module_base+0x60>
lea    0xe25(%rip),%rsi
lea    0x20(%rsp),%rdi
call   1070 <strtok@plt>
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %eax,%ebx
cmp    $0x8000,%eax
cmove  %r14d,%ebx
jmp    122b <get_module_base+0xbb>
xor    %ebx,%ebx
jmp    1233 <get_module_base+0xc3>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x420,%rsp
pop    %rbx
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 = [3 x i8] c"rb\00"
@.str.3 = [2 x i8] c"-\00"
define i32 @get_module_base(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [32 x i8]
%9 = alloca [1024 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %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 ([3 x i8], [3 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 %49
25: ; preds = %20
br label %26
26: ; preds = %45, %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 %46
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 %45
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 i32 @strtoul(i8* %39, i32* null, i32 16)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, 32768
br i1 %42, label %43, label %44
43: ; preds = %36
store i32 0, i32* %6
br label %44
44: ; preds = %43, %36
br label %46
45: ; preds = %31
br label %26
46: ; preds = %44, %26
%47 = load i32*, i32** %5
%48 = call i32 @fclose(i32* %47)
br label %49
49: ; preds = %46, %20
%50 = load i32, i32* %6
ret i32 %50
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [14 x i8] c"/proc/%d/maps\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [2 x i8] c"-\00"
define i32 @get_module_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 ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %26, 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 %23, 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 i32 @strtoul(i8* nocapture %19, i32* null, i32 16)
%21 = icmp eq i32 %20, 32768
%22 = select i1 %21, i32 0, i32 %20
br label %23
23: ; preds = %12, %18
%24 = phi i32 [ %22, %18 ], [ 0, %12 ]
%25 = call i32 @fclose(i32* %10)
br label %26
26: ; preds = %23, %2
%27 = phi i32 [ %24, %23 ], [ 0, %2 ]
ret i32 %27
}