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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, long *);
int /*<<< orphan*/ *strstr(char *, char *);
int checkloaded(pid_t pid, char *libname) {
  FILE *fp;
  char filename[30];
  char line[850];
  long addr;
  char perms[5];
  char *modulePath;
  sprintf(filename, "/proc/%d/maps", pid);
  fp = fopen(filename, "r");
  if (fp == NULL)
    exit(1);
  while (fgets(line, 850, fp) != NULL) {
    sscanf(line, "%lx-%*lx %*s %*s %*s %*d", &addr);
    if (strstr(line, libname) != NULL) {
      fclose(fp);
      return 1;
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<checkloaded>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3c0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe70(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11be <checkloaded+0x4e>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11c3 <checkloaded+0x53>
lea    -0x3a0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x352,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1230 <checkloaded+0xc0>
lea    -0x3a0(%rbp),%rdi
lea    0xe20(%rip),%rsi
lea    -0x3a8(%rbp),%rdx
call   1050 <sscanf@plt>
lea    -0x3a0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     122b <checkloaded+0xbb>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1240 <checkloaded+0xd0>
jmp    11c3 <checkloaded+0x53>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x3c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkloaded>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x390,%rsp
mov    %rsi,%r14
mov    %edi,%edx
lea    0xe74(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe6e(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1227 <checkloaded+0xb7>
mov    %rax,%r15
lea    0x30(%rsp),%rbx
lea    0xe54(%rip),%r12
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x352,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     120a <checkloaded+0x9a>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <sscanf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11d0 <checkloaded+0x60>
mov    $0x1,%ebx
jmp    120c <checkloaded+0x9c>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x390,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"%lx-%*lx %*s %*s %*s %*d\00"
define i32 @checkloaded(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [30 x i8]
%8 = alloca [850 x i8]
%9 = alloca i64
%10 = alloca [5 x i8]
%11 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* %4
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
br label %22
22: ; preds = %37, %21
%23 = getelementptr inbounds [850 x i8], [850 x i8]* %8, i64 0, i64 0
%24 = load i32*, i32** %6
%25 = call i32* @fgets(i8* %23, i32 850, i32* %24)
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %38
27: ; preds = %22
%28 = getelementptr inbounds [850 x i8], [850 x i8]* %8, i64 0, i64 0
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64* %9)
%30 = getelementptr inbounds [850 x i8], [850 x i8]* %8, i64 0, i64 0
%31 = load i8*, i8** %5
%32 = call i32* @strstr(i8* %30, i8* %31)
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
store i32 1, i32* %3
br label %41
37: ; preds = %27
br label %22
38: ; preds = %22
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
store i32 0, i32* %3
br label %41
41: ; preds = %38, %34
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"%lx-%*lx %*s %*s %*s %*d\00"
define i32 @checkloaded(i32 %0, i8* nocapture %1) {
%3 = alloca [30 x i8]
%4 = alloca [850 x i8]
%5 = alloca i64
%6 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [850 x i8], [850 x i8]* %4, i64 0, i64 0
%8 = bitcast i64* %5 to i8*
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2, %17
%15 = call i32* @fgets(i8* %7, i32 850, i32* %10)
%16 = icmp eq i32* %15, null
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64* %5)
%19 = call i32* @strstr(i8* %7, i8* dereferenceable(1) %1)
%20 = icmp eq i32* %19, null
br i1 %20, label %14, label %21
21: ; preds = %14, %17
%22 = phi i32 [ 1, %17 ], [ 0, %14 ]
%23 = call i32 @fclose(i32* %10)
ret i32 %22
}