#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 size_t uint64_t;
typedef int /*<<< orphan*/ line;
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 sscanf(char *, char *, size_t *, size_t *, char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strrchr(char const *, char);
int /*<<< orphan*/ *strstr(char *, char const *);
int s2e_get_module_info(const char *moduleToRetrieve, char *name,
                        size_t maxNameLength, uint64_t *loadBase,
                        uint64_t *size) {
  const char *modName = NULL;
  int result = -1;
  if (moduleToRetrieve) {
    modName = strrchr(moduleToRetrieve, '/');
    if (modName == NULL) {
      modName = name;
    } else {
      ++modName;
    }
  }
  FILE *maps = fopen("/proc/self/maps", "r");
  if (!maps) {
    return result;
  }
  size_t start, end;
  char executable;
  char line[256], path[128];
  while (fgets(line, sizeof(line), maps)) {
    if (sscanf(line, "%x-%x %*c%*c%c%*c %*x %*s %*d %127[^\n]", &start, &end,
               &executable, path) == 4) {
      if (modName) {
        if (executable == 'x' && strstr(path, modName) != NULL) {
          *loadBase = start;
          *size = end - start;
          strncpy(name, path, maxNameLength);
          result = 0;
          break;
        }
      } else {
        if (!executable) {
          continue;
        }
        *loadBase = start;
        *size = end - start;
        strncpy(name, path, maxNameLength);
        result = 0;
        break;
      }
    }
  }
  fclose(maps);
  return result;
}


x86 assembly (clang, -O0):
<s2e_get_module_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1e0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e4 <s2e_get_module_info+0x74>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d3 <s2e_get_module_info+0x63>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11df <s2e_get_module_info+0x6f>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11e4 <s2e_get_module_info+0x74>
lea    0xe15(%rip),%rdi
lea    0xe1e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1211 <s2e_get_module_info+0xa1>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1337 <s2e_get_module_info+0x1c7>
jmp    1216 <s2e_get_module_info+0xa6>
lea    -0x160(%rbp),%rdi
mov    -0x48(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1328 <s2e_get_module_info+0x1b8>
lea    -0x160(%rbp),%rdi
lea    -0x1e0(%rbp),%r9
lea    0xdc8(%rip),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
lea    -0x59(%rbp),%r8
call   1070 <sscanf@plt>
cmp    $0x4,%eax
jne    1323 <s2e_get_module_info+0x1b3>
cmpq   $0x0,-0x38(%rbp)
je     12d5 <s2e_get_module_info+0x165>
movsbl -0x59(%rbp),%eax
cmp    $0x78,%eax
jne    12d0 <s2e_get_module_info+0x160>
lea    -0x1e0(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     12d0 <s2e_get_module_info+0x160>
mov    -0x50(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x58(%rbp),%rcx
sub    -0x50(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x1e0(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x3c(%rbp)
jmp    1328 <s2e_get_module_info+0x1b8>
jmp    131e <s2e_get_module_info+0x1ae>
cmpb   $0x0,-0x59(%rbp)
jne    12e4 <s2e_get_module_info+0x174>
jmp    1216 <s2e_get_module_info+0xa6>
mov    -0x50(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x58(%rbp),%rcx
sub    -0x50(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x1e0(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x3c(%rbp)
jmp    1328 <s2e_get_module_info+0x1b8>
jmp    1323 <s2e_get_module_info+0x1b3>
jmp    1216 <s2e_get_module_info+0xa6>
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s2e_get_module_info>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1c8,%rsp
mov    %r8,%r12
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%r15
test   %rdi,%rdi
je     11a9 <s2e_get_module_info+0x39>
mov    $0x2f,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%rbp
test   %rax,%rax
cmove  %r15,%rbp
jmp    11ab <s2e_get_module_info+0x3b>
xor    %ebp,%ebp
lea    0xe4e(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12b9 <s2e_get_module_info+0x149>
mov    %rax,%rbx
lea    0xc0(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12aa <s2e_get_module_info+0x13a>
mov    %r13,0x10(%rsp)
mov    %r12,0x28(%rsp)
mov    %r15,0x30(%rsp)
mov    %r14,0x38(%rsp)
lea    0xc0(%rsp),%r12
lea    0x18(%rsp),%r14
lea    0xf(%rsp),%r15
lea    0x40(%rsp),%r13
jmp    1235 <s2e_get_module_info+0xc5>
test   %al,%al
jne    1275 <s2e_get_module_info+0x105>
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12aa <s2e_get_module_info+0x13a>
mov    %r12,%rdi
lea    0xdd3(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %r14,%rcx
mov    %r15,%r8
mov    %r13,%r9
call   1070 <sscanf@plt>
cmp    $0x4,%eax
jne    1220 <s2e_get_module_info+0xb0>
movzbl 0xf(%rsp),%eax
test   %rbp,%rbp
je     1215 <s2e_get_module_info+0xa5>
cmp    $0x78,%al
jne    1220 <s2e_get_module_info+0xb0>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     1220 <s2e_get_module_info+0xb0>
mov    0x20(%rsp),%rcx
mov    0x10(%rsp),%rax
mov    %rcx,(%rax)
mov    0x18(%rsp),%rax
sub    %rcx,%rax
mov    0x28(%rsp),%rcx
mov    %rax,(%rcx)
lea    0x40(%rsp),%rsi
mov    0x30(%rsp),%rdi
mov    0x38(%rsp),%rdx
call   1030 <strncpy@plt>
xor    %ebp,%ebp
jmp    12af <s2e_get_module_info+0x13f>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    12be <s2e_get_module_info+0x14e>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x1c8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [39 x i8] c"%x-%x %*c%*c%c%*c %*x %*s %*d %127[^\0A]\00"
define i32 @s2e_get_module_info(i8* %0, i8* %1, i64 %2, i64* %3, i64* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64*
%11 = alloca i64*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i64
%16 = alloca i64
%17 = alloca i8
%18 = alloca [256 x i8]
%19 = alloca [128 x i8]
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i64* %3, i64** %10
store i64* %4, i64** %11
store i8* null, i8** %12
store i32 -1, i32* %13
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %33
22: ; preds = %5
%23 = load i8*, i8** %7
%24 = call i8* @strrchr(i8* %23, i8 signext 47)
store i8* %24, i8** %12
%25 = load i8*, i8** %12
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i8*, i8** %8
store i8* %28, i8** %12
br label %32
29: ; preds = %22
%30 = load i8*, i8** %12
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %12
br label %32
32: ; preds = %29, %27
br label %33
33: ; preds = %32, %5
%34 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %14
%35 = load i32*, i32** %14
%36 = icmp ne i32* %35, null
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = load i32, i32* %13
store i32 %38, i32* %6
br label %95
39: ; preds = %33
br label %40
40: ; preds = %90, %77, %39
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %18, i64 0, i64 0
%42 = load i32*, i32** %14
%43 = call i64 @fgets(i8* %41, i32 256, i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %91
45: ; preds = %40
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %18, i64 0, i64 0
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %19, i64 0, i64 0
%48 = call i32 @sscanf(i8* %46, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i64* %15, i64* %16, i8* %17, i8* %47)
%49 = icmp eq i32 %48, 4
br i1 %49, label %50, label %90
50: ; preds = %45
%51 = load i8*, i8** %12
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %74
53: ; preds = %50
%54 = load i8, i8* %17
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 120
br i1 %56, label %57, label %73
57: ; preds = %53
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %19, i64 0, i64 0
%59 = load i8*, i8** %12
%60 = call i32* @strstr(i8* %58, i8* %59)
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %73
62: ; preds = %57
%63 = load i64, i64* %15
%64 = load i64*, i64** %10
store i64 %63, i64* %64
%65 = load i64, i64* %16
%66 = load i64, i64* %15
%67 = sub i64 %65, %66
%68 = load i64*, i64** %11
store i64 %67, i64* %68
%69 = load i8*, i8** %8
%70 = getelementptr inbounds [128 x i8], [128 x i8]* %19, i64 0, i64 0
%71 = load i64, i64* %9
%72 = call i32 @strncpy(i8* %69, i8* %70, i64 %71)
store i32 0, i32* %13
br label %91
73: ; preds = %57, %53
br label %89
74: ; preds = %50
%75 = load i8, i8* %17
%76 = icmp ne i8 %75, 0
br i1 %76, label %78, label %77
77: ; preds = %74
br label %40
78: ; preds = %74
%79 = load i64, i64* %15
%80 = load i64*, i64** %10
store i64 %79, i64* %80
%81 = load i64, i64* %16
%82 = load i64, i64* %15
%83 = sub i64 %81, %82
%84 = load i64*, i64** %11
store i64 %83, i64* %84
%85 = load i8*, i8** %8
%86 = getelementptr inbounds [128 x i8], [128 x i8]* %19, i64 0, i64 0
%87 = load i64, i64* %9
%88 = call i32 @strncpy(i8* %85, i8* %86, i64 %87)
store i32 0, i32* %13
br label %91
89: ; preds = %73
br label %90
90: ; preds = %89, %45
br label %40
91: ; preds = %78, %62, %40
%92 = load i32*, i32** %14
%93 = call i32 @fclose(i32* %92)
%94 = load i32, i32* %13
store i32 %94, i32* %6
br label %95
95: ; preds = %91, %37
%96 = load i32, i32* %6
ret i32 %96
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [39 x i8] c"%x-%x %*c%*c%c%*c %*x %*s %*d %127[^\0A]\00"
define i32 @s2e_get_module_info(i8* %0, i8* %1, i64 %2, i64* nocapture writeonly %3, i64* nocapture writeonly %4) {
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8
%9 = alloca [256 x i8]
%10 = alloca [128 x i8]
%11 = icmp eq i8* %0, null
br i1 %11, label %17, label %12
12: ; preds = %5
%13 = tail call i8* @strrchr(i8* %0, i8 signext 47)
%14 = icmp eq i8* %13, null
%15 = getelementptr inbounds i8, i8* %13, i64 1
%16 = select i1 %14, i8* %1, i8* %15
br label %17
17: ; preds = %12, %5
%18 = phi i8* [ null, %5 ], [ %16, %12 ]
%19 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %59, label %21
21: ; preds = %17
%22 = bitcast i64* %6 to i8*
%23 = bitcast i64* %7 to i8*
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%26 = call i64 @fgets(i8* %24, i32 256, i32* %19)
%27 = icmp eq i64 %26, 0
br i1 %27, label %56, label %28
28: ; preds = %21
%29 = icmp eq i8* %18, null
br label %30
30: ; preds = %28, %50
%31 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i64* %6, i64* %7, i8* %8, i8* %25)
%32 = icmp eq i32 %31, 4
br i1 %32, label %33, label %50
33: ; preds = %30
%34 = load i8, i8* %8
br i1 %29, label %44, label %35
35: ; preds = %33
%36 = icmp eq i8 %34, 120
br i1 %36, label %37, label %50
37: ; preds = %35
%38 = call i32* @strstr(i8* %25, i8* %18)
%39 = icmp eq i32* %38, null
br i1 %39, label %50, label %40
40: ; preds = %37
%41 = load i64, i64* %6
store i64 %41, i64* %3
%42 = load i64, i64* %7
%43 = sub i64 %42, %41
br label %53
44: ; preds = %33
%45 = icmp eq i8 %34, 0
br i1 %45, label %50, label %46
46: ; preds = %44
%47 = load i64, i64* %6
store i64 %47, i64* %3
%48 = load i64, i64* %7
%49 = sub i64 %48, %47
br label %53
50: ; preds = %30, %35, %37, %44
%51 = call i64 @fgets(i8* %24, i32 256, i32* %19)
%52 = icmp eq i64 %51, 0
br i1 %52, label %56, label %30
53: ; preds = %40, %46
%54 = phi i64 [ %49, %46 ], [ %43, %40 ]
store i64 %54, i64* %4
%55 = call i32 @strncpy(i8* %1, i8* %25, i64 %2)
br label %56
56: ; preds = %50, %53, %21
%57 = phi i32 [ -1, %21 ], [ 0, %53 ], [ -1, %50 ]
%58 = call i32 @fclose(i32* %19)
br label %59
59: ; preds = %17, %56
%60 = phi i32 [ %57, %56 ], [ -1, %17 ]
ret i32 %60
}