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

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
int snprintf(char *, int, char *, int);
int sscanf(char *, char *, unsigned long *, unsigned long *, char *, char *);
scalar_t__ strstr(char *, char *);
int find_dynamo_library() {
  pid_t pid = getpid();
  char proc_pid_maps[64];
  FILE *maps;
  char line[4096];
  int count = 0;
  int n = snprintf(proc_pid_maps, sizeof(proc_pid_maps), "/proc/%d/maps", pid);
  if (n < 0 || n == sizeof(proc_pid_maps))
    assert(0);
  maps = fopen(proc_pid_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    $0x20a0,%rsp
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x1064(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe3e(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x1068(%rbp)
cmpl   $0x0,-0x1068(%rbp)
jl     11eb <find_dynamo_library+0x5b>
movslq -0x1068(%rbp),%rax
cmp    $0x40,%rax
jne    11f2 <find_dynamo_library+0x62>
xor    %edi,%edi
call   1070 <assert@plt>
lea    -0x50(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1224 <find_dynamo_library+0x94>
jmp    12cc <find_dynamo_library+0x13c>
lea    -0x1060(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    $0x1000,%esi
call   1060 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    124c <find_dynamo_library+0xbc>
jmp    12cc <find_dynamo_library+0x13c>
lea    -0x1060(%rbp),%rdi
lea    -0x1070(%rbp),%rdx
lea    -0x1078(%rbp),%rcx
lea    -0x1090(%rbp),%r8
lea    -0x2090(%rbp),%r9
lea    0xd9a(%rip),%rsi
call   1090 <sscanf@plt>
mov    %eax,-0x2094(%rbp)
cmpl   $0x4,-0x2094(%rbp)
jge    1295 <find_dynamo_library+0x105>
movb   $0x0,-0x2090(%rbp)
lea    -0x2090(%rbp),%rdi
lea    0xd91(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     12c7 <find_dynamo_library+0x137>
mov    -0x58(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <find_dynamo_library+0x14c>
jmp    1206 <find_dynamo_library+0x76>
mov    -0x58(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dynamo_library>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2068,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe51(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
test   %eax,%eax
js     11c9 <find_dynamo_library+0x39>
cmp    $0x40,%eax
jne    11d0 <find_dynamo_library+0x40>
xor    %edi,%edi
call   1070 <assert@plt>
lea    0xe37(%rip),%rsi
lea    0x20(%rsp),%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     120c <find_dynamo_library+0x7c>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x2068,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x1060(%rsp),%r14
mov    %rsp,%r13
lea    0x10(%rsp),%rbp
lea    0x60(%rsp),%r15
lea    0xe0c(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f0 <find_dynamo_library+0x60>
mov    %r14,%rdi
lea    0xdc1(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r13,%rcx
mov    %rbp,%r8
mov    %r15,%r9
call   1090 <sscanf@plt>
cmp    $0x3,%eax
jg     126c <find_dynamo_library+0xdc>
movb   $0x0,0x60(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
jne    128d <find_dynamo_library+0xfd>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     1230 <find_dynamo_library+0xa0>
jmp    11f0 <find_dynamo_library+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    11fa <find_dynamo_library+0x6a>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/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 [64 x i8]
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [16 x i8]
%11 = alloca [4096 x i8]
%12 = alloca i32
%13 = call i32 (...) @getpid()
store i32 %13, i32* %2
store i32 0, i32* %6
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%15 = load i32, i32* %2
%16 = call i32 @snprintf(i8* %14, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %0
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = icmp eq i64 %21, 64
br i1 %22, label %23, label %25
23: ; preds = %19, %0
%24 = call i32 @assert(i32 0)
br label %25
25: ; preds = %23, %19
%26 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %4
br label %28
28: ; preds = %57, %25
%29 = load i32*, i32** %4
%30 = call i32 @feof(i32* %29)
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
br i1 %32, label %33, label %58
33: ; preds = %28
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%35 = load i32*, i32** %4
%36 = call i32* @fgets(i8* %34, i32 4096, i32* %35)
%37 = icmp eq i32* null, %36
br i1 %37, label %38, label %39
38: ; preds = %33
br label %58
39: ; preds = %33
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%41 = bitcast i8** %8 to i64*
%42 = bitcast i8** %9 to i64*
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%45 = call i32 @sscanf(i8* %40, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64* %41, i64* %42, i8* %43, i8* %44)
store i32 %45, i32* %12
%46 = load i32, i32* %12
%47 = icmp slt i32 %46, 4
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
store i8 0, i8* %49
br label %50
50: ; preds = %48, %39
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%52 = call i64 @strstr(i8* %51, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i32*, i32** %4
%56 = call i32 @fclose(i32* %55)
store i32 1, i32* %1
br label %61
57: ; preds = %50
br label %28
58: ; preds = %38, %28
%59 = load i32*, i32** %4
%60 = call i32 @fclose(i32* %59)
store i32 0, i32* %1
br label %61
61: ; preds = %58, %54
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/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 [64 x i8]
%2 = alloca [4096 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [16 x i8]
%6 = alloca [4096 x i8]
%7 = tail call i32 (...) @getpid()
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%10 = call i32 @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%11 = icmp slt i32 %10, 0
%12 = icmp eq i32 %10, 64
%13 = or i1 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @assert(i32 0)
br label %16
16: ; preds = %0, %14
%17 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @feof(i32* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %43
20: ; preds = %16
%21 = bitcast i8** %3 to i8*
%22 = bitcast i8** %4 to i8*
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%25 = bitcast i8** %3 to i64*
%26 = bitcast i8** %4 to i64*
br label %27
27: ; preds = %20, %40
%28 = call i32* @fgets(i8* %9, i32 4096, i32* %17)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %31
30: ; preds = %27
br label %43
31: ; preds = %27
%32 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64* %25, i64* %26, i8* %23, i8* %24)
%33 = icmp slt i32 %32, 4
br i1 %33, label %34, label %35
34: ; preds = %31
store i8 0, i8* %24
br label %35
35: ; preds = %34, %31
%36 = call i64 @strstr(i8* %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @fclose(i32* %17)
br label %45
40: ; preds = %35
%41 = call i32 @feof(i32* %17)
%42 = icmp eq i32 %41, 0
br i1 %42, label %27, label %43
43: ; preds = %40, %16, %30
%44 = call i32 @fclose(i32* %17)
br label %45
45: ; preds = %38, %43
%46 = phi i32 [ 0, %43 ], [ 1, %38 ]
ret i32 %46
}