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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
scalar_t__ memcmp(char *, char const *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int sscanf(char *, char *, size_t *, size_t *, char *, char *, char *, char *,
           size_t *);
int strlen(char const *);
size_t get_library_address(const char *libname) {
  char path[256];
  char buff[256];
  int len_libname = strlen(libname);
  FILE *file;
  size_t addr = 0;
  snprintf(path, sizeof path, "/proc/%d/smaps", getpid());
  file = fopen(path, "rt");
  if (file == NULL)
    return 0;
  while (fgets(buff, sizeof buff, file) != NULL) {
    int len = strlen(buff);
    if (len > 0 && buff[len - 1] == '\n') {
      buff[--len] = '\0';
    }
    if (len <= len_libname ||
        memcmp(buff + len - len_libname, libname, len_libname))
      continue;
    size_t start, end, offset;
    char flags[4];
    if (sscanf(buff, "%zx-%zx %c%c%c%c %zx", &start, &end, &flags[0], &flags[1],
               &flags[2], &flags[3], &offset) != 7)
      continue;
    if (flags[0] != 'r' || flags[2] != 'x') {
      continue;
    }
    addr = start - offset;
    break;
  }
  fclose(file);
  return addr;
}


x86 assembly (clang, -O0):
<get_library_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x214(%rbp)
movq   $0x0,-0x228(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x258(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x258(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe2d(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    120d <get_library_address+0x8d>
movq   $0x0,-0x8(%rbp)
jmp    1395 <get_library_address+0x215>
jmp    1212 <get_library_address+0x92>
lea    -0x210(%rbp),%rdi
mov    -0x220(%rbp),%rdx
mov    $0x100,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     137e <get_library_address+0x1fe>
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x22c(%rbp)
cmpl   $0x0,-0x22c(%rbp)
jle    1288 <get_library_address+0x108>
mov    -0x22c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1288 <get_library_address+0x108>
mov    -0x22c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x22c(%rbp)
cltq
movb   $0x0,-0x210(%rbp,%rax,1)
mov    -0x22c(%rbp),%eax
cmp    -0x214(%rbp),%eax
jle    12d3 <get_library_address+0x153>
lea    -0x210(%rbp),%rdi
movslq -0x22c(%rbp),%rax
add    %rax,%rdi
movslq -0x214(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x214(%rbp),%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     12d8 <get_library_address+0x158>
jmp    1212 <get_library_address+0x92>
lea    -0x210(%rbp),%rdi
lea    -0x24c(%rbp),%r8
lea    -0x24c(%rbp),%r9
add    $0x1,%r9
lea    -0x24c(%rbp),%r11
add    $0x2,%r11
lea    -0x24c(%rbp),%r10
add    $0x3,%r10
lea    0xd04(%rip),%rsi
lea    -0x238(%rbp),%rdx
lea    -0x240(%rbp),%rcx
lea    -0x248(%rbp),%rax
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1090 <sscanf@plt>
cmp    $0x7,%eax
je     1344 <get_library_address+0x1c4>
jmp    1212 <get_library_address+0x92>
movsbl -0x24c(%rbp),%eax
cmp    $0x72,%eax
jne    1364 <get_library_address+0x1e4>
movsbl -0x24a(%rbp),%eax
cmp    $0x78,%eax
je     1369 <get_library_address+0x1e9>
jmp    1212 <get_library_address+0x92>
mov    -0x238(%rbp),%rax
sub    -0x248(%rbp),%rax
mov    %rax,-0x228(%rbp)
mov    -0x220(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x228(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_library_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe54(%rip),%rdx
lea    0x130(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
lea    0xe45(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12f1 <get_library_address+0x171>
mov    %rax,%rbx
movslq %ebp,%r12
neg    %r12
xor    %eax,%eax
mov    %rax,0x10(%rsp)
lea    0x30(%rsp),%r13
jmp    120a <get_library_address+0x8a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2,%r14d
jne    12e4 <get_library_address+0x164>
mov    %r13,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12e4 <get_library_address+0x164>
mov    %r13,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    1242 <get_library_address+0xc2>
mov    %eax,%ecx
add    $0xffffffff,%ecx
cmpb   $0xa,0x30(%rsp,%rcx,1)
jne    1242 <get_library_address+0xc2>
movb   $0x0,0x30(%rsp,%rcx,1)
mov    %ecx,%eax
mov    $0x2,%r14d
cmp    %ebp,%eax
jle    1200 <get_library_address+0x80>
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x30,%rdi
add    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
jne    1200 <get_library_address+0x80>
sub    $0x8,%rsp
mov    %r13,%rdi
lea    0xd9c(%rip),%rsi
lea    0x28(%rsp),%rdx
lea    0x30(%rsp),%rcx
lea    0x14(%rsp),%r8
lea    0x15(%rsp),%r9
lea    0x20(%rsp),%rax
push   %rax
lea    0x1f(%rsp),%rax
push   %rax
lea    0x26(%rsp),%rax
push   %rax
call   1090 <sscanf@plt>
add    $0x20,%rsp
mov    $0x2,%r14d
cmp    $0x7,%eax
jne    1200 <get_library_address+0x80>
cmpb   $0x72,0xc(%rsp)
jne    1200 <get_library_address+0x80>
cmpb   $0x78,0xe(%rsp)
jne    1200 <get_library_address+0x80>
mov    0x20(%rsp),%rax
sub    0x18(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    $0x3,%r14d
jmp    1200 <get_library_address+0x80>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%r14
mov    %r14,%rax
add    $0x238,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%d/smaps\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [21 x i8] c"%zx-%zx %c%c%c%c %zx\00"
define i64 @get_library_address(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [256 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca [4 x i8]
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = tail call i32 @strlen(i8* %0)
%11 = tail call i32 (...) @getpid()
%12 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %71, label %15
15: ; preds = %1
%16 = sext i32 %10 to i64
%17 = sub nsw i64 0, %16
%18 = bitcast i64* %4 to i8*
%19 = bitcast i64* %5 to i8*
%20 = bitcast i64* %6 to i8*
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 1
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 2
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 3
br label %25
25: ; preds = %15, %64
%26 = phi i64 [ %65, %64 ], [ 0, %15 ]
%27 = call i32* @fgets(i8* %9, i32 256, i32* %13)
%28 = icmp eq i32* %27, null
br i1 %28, label %68, label %29
29: ; preds = %25
%30 = call i32 @strlen(i8* %9)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = add nsw i32 %30, -1
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 10
br i1 %37, label %38, label %39
38: ; preds = %32
store i8 0, i8* %35
br label %39
39: ; preds = %38, %32, %29
%40 = phi i32 [ %33, %38 ], [ %30, %32 ], [ %30, %29 ]
%41 = icmp sgt i32 %40, %10
br i1 %41, label %42, label %64
42: ; preds = %39
%43 = sext i32 %40 to i64
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %43
%45 = getelementptr inbounds i8, i8* %44, i64 %17
%46 = call i64 @memcmp(i8* %45, i8* %0, i32 %10)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %64
48: ; preds = %42
%49 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64* %4, i64* %5, i8* %21, i8* %22, i8* %23, i8* %24, i64* %6)
%50 = icmp eq i32 %49, 7
br i1 %50, label %51, label %61
51: ; preds = %48
%52 = load i8, i8* %21
%53 = icmp ne i8 %52, 114
%54 = load i8, i8* %23
%55 = icmp ne i8 %54, 120
%56 = select i1 %53, i1 true, i1 %55
br i1 %56, label %61, label %57
57: ; preds = %51
%58 = load i64, i64* %4
%59 = load i64, i64* %6
%60 = sub i64 %58, %59
br label %61
61: ; preds = %51, %48, %57
%62 = phi i64 [ %60, %57 ], [ %26, %48 ], [ %26, %51 ]
%63 = phi i32 [ 3, %57 ], [ 2, %48 ], [ 2, %51 ]
br label %64
64: ; preds = %39, %42, %61
%65 = phi i64 [ %62, %61 ], [ %26, %42 ], [ %26, %39 ]
%66 = phi i32 [ %63, %61 ], [ 2, %42 ], [ 2, %39 ]
%67 = icmp eq i32 %66, 2
br i1 %67, label %25, label %68
68: ; preds = %64, %25
%69 = phi i64 [ %65, %64 ], [ %26, %25 ]
%70 = call i32 @fclose(i32* %13)
br label %71
71: ; preds = %1, %68
%72 = phi i64 [ %69, %68 ], [ 0, %1 ]
ret i64 %72
}