#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ read_line(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(char *, char const *);
int get_module_path(const char *module, char *path) {
  pid_t pid = getpid();
  char maps[512];
  sprintf(maps, "/proc/%i/maps", pid);
  FILE *fd = fopen(maps, "r");
  char line[512];
  while (read_line(fd, line)) {
    if (strstr(line, module) != NULL) {
      strcpy(path, strchr(line, '/'));
      fclose(fd);
      return 1;
    }
    memset(line, 0, 512);
  }
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<get_module_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe42(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x228(%rbp)
mov    -0x228(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1080 <read_line@plt>
cmp    $0x0,%rax
je     126e <get_module_path+0xde>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1256 <get_module_path+0xc6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x430(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    -0x228(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1281 <get_module_path+0xf1>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
jmp    11dd <get_module_path+0x4d>
mov    -0x228(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_module_path>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4b(%rip),%rsi
lea    0x200(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
lea    0xe40(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%r12
mov    %rsp,%rsi
mov    %rax,%rdi
call   1080 <read_line@plt>
test   %rax,%rax
je     123d <get_module_path+0xad>
mov    %rsp,%rbx
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r15,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
jne    1221 <get_module_path+0x91>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <read_line@plt>
test   %rax,%rax
jne    11f0 <get_module_path+0x60>
jmp    123d <get_module_path+0xad>
mov    %rsp,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    $0x1,%ebp
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%i/maps\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_module_path(i8* nocapture %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = alloca [512 x i8]
%5 = tail call i32 (...) @getpid()
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5)
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%10 = call i64 @read_line(i32* %8, i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %2, %18
%13 = call i32* @strstr(i8* %9, i8* dereferenceable(1) %0)
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = call i32 @strchr(i8* %9, i8 signext 47)
%17 = call i32 @strcpy(i8* %1, i32 %16)
br label %22
18: ; preds = %12
%19 = call i32 @memset(i8* %9, i32 0, i32 512)
%20 = call i64 @read_line(i32* %8, i8* %9)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %12
22: ; preds = %18, %2, %15
%23 = phi i32 [ 1, %15 ], [ 0, %2 ], [ 0, %18 ]
%24 = call i32 @fclose(i32* %8)
ret i32 %23
}