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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, unsigned long *, unsigned long *, char *, char *);
scalar_t__ strstr(char *, char *);
int find_dynamo_library() {
  FILE *maps;
  char line[4096];
  int count = 0;
  maps = fopen("/proc/self/maps", "r");
  while (!feof(maps)) {
    void *vm_start, *vm_end;
    char perm[16];
    char comment_buffer[4096];
    int len;
    if (NULL == fgets(line, sizeof(line), maps))
      break;
    len = sscanf(line,
                 sizeof(void *) == 4 ? "%08lx-%08lx %s %*x %*s %*u %4096s"
                                     : "%016lx-%016lx %s %*x %*s %*u %4096s",
                 (unsigned long *)&vm_start, (unsigned long *)&vm_end, perm,
                 comment_buffer);
    if (len < 4)
      comment_buffer[0] = '\0';
    if (strstr(comment_buffer, "dynamorio") != 0) {
      fclose(maps);
      return 1;
    }
  }
  fclose(maps);
  return 0;
}


x86 assembly (clang, -O0):
<find_dynamo_library>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
movl   $0x0,-0x1014(%rbp)
lea    0xe84(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11aa <find_dynamo_library+0x4a>
jmp    1252 <find_dynamo_library+0xf2>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11d2 <find_dynamo_library+0x72>
jmp    1252 <find_dynamo_library+0xf2>
lea    -0x1010(%rbp),%rdi
lea    -0x1020(%rbp),%rdx
lea    -0x1028(%rbp),%rcx
lea    -0x1040(%rbp),%r8
lea    -0x2040(%rbp),%r9
lea    0xe16(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,-0x2044(%rbp)
cmpl   $0x4,-0x2044(%rbp)
jge    121b <find_dynamo_library+0xbb>
movb   $0x0,-0x2040(%rbp)
lea    -0x2040(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     124d <find_dynamo_library+0xed>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1262 <find_dynamo_library+0x102>
jmp    118c <find_dynamo_library+0x2c>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dynamo_library>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2028,%rsp
lea    0xe88(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11af <find_dynamo_library+0x4f>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x2028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x1020(%rsp),%r14
mov    %rsp,%r13
lea    0x10(%rsp),%rbp
lea    0x20(%rsp),%r15
lea    0xe6b(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1193 <find_dynamo_library+0x33>
mov    %r14,%rdi
lea    0xe23(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r13,%rcx
mov    %rbp,%r8
mov    %r15,%r9
call   1060 <sscanf@plt>
cmp    $0x3,%eax
jg     120c <find_dynamo_library+0xac>
movb   $0x0,0x20(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
jne    122d <find_dynamo_library+0xcd>
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11d0 <find_dynamo_library+0x70>
jmp    1193 <find_dynamo_library+0x33>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
jmp    119d <find_dynamo_library+0x3d>

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [36 x i8] c"%016lx-%016lx %s %*x %*s %*u %4096s\00"
@.str.3 = [10 x i8] c"dynamorio\00"
define i32 @find_dynamo_library() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [16 x i8]
%8 = alloca [4096 x i8]
%9 = alloca i32
store i32 0, i32* %4
%10 = 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* %10, i32** %2
br label %11
11: ; preds = %40, %0
%12 = load i32*, i32** %2
%13 = call i32 @feof(i32* %12)
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br i1 %15, label %16, label %41
16: ; preds = %11
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%18 = load i32*, i32** %2
%19 = call i32* @fgets(i8* %17, i32 4096, i32* %18)
%20 = icmp eq i32* null, %19
br i1 %20, label %21, label %22
21: ; preds = %16
br label %41
22: ; preds = %16
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%24 = bitcast i8** %5 to i64*
%25 = bitcast i8** %6 to i64*
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%28 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64* %24, i64* %25, i8* %26, i8* %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = icmp slt i32 %29, 4
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
store i8 0, i8* %32
br label %33
33: ; preds = %31, %22
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%35 = call i64 @strstr(i8* %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32*, i32** %2
%39 = call i32 @fclose(i32* %38)
store i32 1, i32* %1
br label %44
40: ; preds = %33
br label %11
41: ; preds = %21, %11
%42 = load i32*, i32** %2
%43 = call i32 @fclose(i32* %42)
store i32 0, i32* %1
br label %44
44: ; preds = %41, %37
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [36 x i8] c"%016lx-%016lx %s %*x %*s %*u %4096s\00"
@.str.3 = [10 x i8] c"dynamorio\00"
define i32 @find_dynamo_library() {
%1 = alloca [4096 x i8]
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [16 x i8]
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%7 = 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))
%8 = call i32 @feof(i32* %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %33
10: ; preds = %0
%11 = bitcast i8** %2 to i8*
%12 = bitcast i8** %3 to i8*
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%15 = bitcast i8** %2 to i64*
%16 = bitcast i8** %3 to i64*
br label %17
17: ; preds = %10, %30
%18 = call i32* @fgets(i8* %6, i32 4096, i32* %7)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17
br label %33
21: ; preds = %17
%22 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64* %15, i64* %16, i8* %13, i8* %14)
%23 = icmp slt i32 %22, 4
br i1 %23, label %24, label %25
24: ; preds = %21
store i8 0, i8* %14
br label %25
25: ; preds = %24, %21
%26 = call i64 @strstr(i8* %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @fclose(i32* %7)
br label %35
30: ; preds = %25
%31 = call i32 @feof(i32* %7)
%32 = icmp eq i32 %31, 0
br i1 %32, label %17, label %33
33: ; preds = %30, %0, %20
%34 = call i32 @fclose(i32* %7)
br label %35
35: ; preds = %28, %33
%36 = phi i32 [ 0, %33 ], [ 1, %28 ]
ret i32 %36
}