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

/* Variables and functions */
scalar_t__ db_add_file_open(unsigned int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *malloc(int);
int /*<<< orphan*/ path_is_dir(char *);
char *read_line(char *, size_t *, int /*<<< orphan*/ *);
int snprintf(char *, int, char const *const, int /*<<< orphan*/);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long *, unsigned long *,
                          char *, unsigned long *, unsigned int *,
                          unsigned int *, unsigned long *, char *);
scalar_t__ strncmp(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int trace_add_files_from_proc(unsigned int process, pid_t tid,
                              const char *binary) {
  FILE *fp;
  char dummy;
  char *line = NULL;
  size_t length = 0;
  char previous_path[4096] = "";
  const char *const fmt = "/proc/%d/maps";
  int len = snprintf(&dummy, 1, fmt, tid);
  char *procfile = malloc(len + 1);
  snprintf(procfile, len + 1, fmt, tid);
  fp = fopen(procfile, "r");
  while ((line = read_line(line, &length, fp)) != NULL) {
    unsigned long int addr_start, addr_end;
    char perms[5];
    unsigned long int offset;
    unsigned int dev_major, dev_minor;
    unsigned long int inode;
    char pathname[4096];
    sscanf(line, "%lx-%lx %4s %lx %x:%x %lu %s", &addr_start, &addr_end, perms,
           &offset, &dev_major, &dev_minor, &inode, pathname);
    if (inode > 0) {
      if (strncmp(pathname, binary, 4096) != 0 &&
          strncmp(previous_path, pathname, 4096) != 0) {
        if (db_add_file_open(process, pathname, 0x01, path_is_dir(pathname)) !=
            0)
          return -1;
        strncpy(previous_path, pathname, 4096);
      }
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<trace_add_files_from_proc>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x20b8,%rsp
mov    %edi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x1040(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1090 <memset@plt>
lea    0xe10(%rip),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x14(%rbp),%ecx
lea    -0x29(%rbp),%rdi
mov    $0x1,%esi
lea    0xdf6(%rip),%rdx
call   1080 <snprintf@plt>
mov    %eax,-0x104c(%rbp)
mov    -0x104c(%rbp),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x1058(%rbp)
mov    -0x1058(%rbp),%rdi
mov    -0x104c(%rbp),%esi
add    $0x1,%esi
mov    -0x14(%rbp),%ecx
lea    0xdbc(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x1058(%rbp),%rdi
lea    0xdb7(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    -0x40(%rbp),%rsi
call   10c0 <read_line@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     139d <trace_add_files_from_proc+0x1ed>
mov    -0x38(%rbp),%rdi
lea    -0x106d(%rbp),%r8
lea    -0x2090(%rbp),%rax
lea    0xd78(%rip),%rsi
lea    -0x1060(%rbp),%rdx
lea    -0x1068(%rbp),%rcx
lea    -0x1078(%rbp),%r9
lea    -0x107c(%rbp),%rbx
lea    -0x1080(%rbp),%r11
lea    -0x1088(%rbp),%r10
mov    %rbx,(%rsp)
mov    %r11,0x8(%rsp)
mov    %r10,0x10(%rsp)
mov    %rax,0x18(%rsp)
call   10b0 <sscanf@plt>
cmpq   $0x0,-0x1088(%rbp)
jbe    1398 <trace_add_files_from_proc+0x1e8>
lea    -0x2090(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     1393 <trace_add_files_from_proc+0x1e3>
lea    -0x1040(%rbp),%rdi
lea    -0x2090(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     1393 <trace_add_files_from_proc+0x1e3>
mov    -0x10(%rbp),%eax
mov    %eax,-0x209c(%rbp)
lea    -0x2090(%rbp),%rax
mov    %rax,-0x2098(%rbp)
lea    -0x2090(%rbp),%rdi
call   1060 <path_is_dir@plt>
mov    -0x209c(%rbp),%edi
mov    -0x2098(%rbp),%rsi
mov    %eax,%ecx
mov    $0x1,%edx
call   1030 <db_add_file_open@plt>
cmp    $0x0,%rax
je     137b <trace_add_files_from_proc+0x1cb>
movl   $0xffffffff,-0xc(%rbp)
jmp    13ad <trace_add_files_from_proc+0x1fd>
lea    -0x1040(%rbp),%rdi
lea    -0x2090(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <strncpy@plt>
jmp    1398 <trace_add_files_from_proc+0x1e8>
jmp    1260 <trace_add_files_from_proc+0xb0>
mov    -0x28(%rbp),%rdi
call   1070 <fclose@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20b8,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<trace_add_files_from_proc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2058,%rsp
mov    %rdx,0x18(%rsp)
mov    %esi,%r15d
mov    %edi,0xc(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x50(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1090 <memset@plt>
lea    0xe12(%rip),%r14
lea    0xb(%rsp),%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %r15d,%ecx
call   1080 <snprintf@plt>
lea    0x1(%rax),%ebp
mov    %ebp,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %r15d,%ecx
call   1080 <snprintf@plt>
lea    0xde7(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <fopen@plt>
mov    %rax,%r13
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    %rax,%rdx
call   10c0 <read_line@plt>
test   %rax,%rax
je     1331 <trace_add_files_from_proc+0x181>
mov    %rax,%rbx
lea    0x1050(%rsp),%rbp
lea    0x33(%rsp),%r14
lea    0x38(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    128a <trace_add_files_from_proc+0xda>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   10c0 <read_line@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1331 <trace_add_files_from_proc+0x181>
mov    %rbx,%rdi
lea    0xd7c(%rip),%rsi
lea    0x48(%rsp),%rdx
lea    0x40(%rsp),%rcx
mov    %r14,%r8
mov    %r15,%r9
push   %rbp
lea    0x28(%rsp),%rax
push   %rax
lea    0x38(%rsp),%rax
push   %rax
lea    0x44(%rsp),%rax
push   %rax
call   10b0 <sscanf@plt>
add    $0x20,%rsp
cmpq   $0x0,0x20(%rsp)
je     1270 <trace_add_files_from_proc+0xc0>
mov    %rbp,%rdi
mov    0x18(%rsp),%rsi
mov    $0x1000,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     1270 <trace_add_files_from_proc+0xc0>
lea    0x50(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     1270 <trace_add_files_from_proc+0xc0>
mov    %rbp,%rdi
call   1060 <path_is_dir@plt>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    $0x1,%edx
mov    %eax,%ecx
call   1030 <db_add_file_open@plt>
test   %rax,%rax
jne    134d <trace_add_files_from_proc+0x19d>
lea    0x50(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1040 <strncpy@plt>
jmp    1270 <trace_add_files_from_proc+0xc0>
mov    %r13,%rdi
call   1070 <fclose@plt>
xor    %eax,%eax
add    $0x2058,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    133b <trace_add_files_from_proc+0x18b>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"%lx-%lx %4s %lx %x:%x %lu %s\00"
define i32 @trace_add_files_from_proc(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [4096 x i8]
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i64
%18 = alloca [5 x i8]
%19 = alloca i64
%20 = alloca i32
%21 = alloca i32
%22 = alloca i64
%23 = alloca [4096 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* null, i8** %10
store i64 0, i64* %11
%24 = bitcast [4096 x i8]* %12 to i8*
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %13
%25 = load i32, i32* %6
%26 = call i32 @snprintf(i8* %9, i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i32 0, i32 0), i32 %25)
store i32 %26, i32* %14
%27 = load i32, i32* %14
%28 = add nsw i32 %27, 1
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %15
%30 = load i8*, i8** %15
%31 = load i32, i32* %14
%32 = add nsw i32 %31, 1
%33 = load i32, i32* %6
%34 = call i32 @snprintf(i8* %30, i32 %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i32 0, i32 0), i32 %33)
%35 = load i8*, i8** %15
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %36, i32** %8
br label %37
37: ; preds = %72, %3
%38 = load i8*, i8** %10
%39 = load i32*, i32** %8
%40 = call i8* @read_line(i8* %38, i64* %11, i32* %39)
store i8* %40, i8** %10
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %73
42: ; preds = %37
%43 = load i8*, i8** %10
%44 = getelementptr inbounds [5 x i8], [5 x i8]* %18, i64 0, i64 0
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%46 = call i32 @sscanf(i8* %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64* %16, i64* %17, i8* %44, i64* %19, i32* %20, i32* %21, i64* %22, i8* %45)
%47 = load i64, i64* %22
%48 = icmp ugt i64 %47, 0
br i1 %48, label %49, label %72
49: ; preds = %42
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%51 = load i8*, i8** %7
%52 = call i64 @strncmp(i8* %50, i8* %51, i32 4096)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %71
54: ; preds = %49
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%57 = call i64 @strncmp(i8* %55, i8* %56, i32 4096)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %71
59: ; preds = %54
%60 = load i32, i32* %5
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%63 = call i32 @path_is_dir(i8* %62)
%64 = call i64 @db_add_file_open(i32 %60, i8* %61, i32 1, i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %59
store i32 -1, i32* %4
br label %76
67: ; preds = %59
%68 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%70 = call i32 @strncpy(i8* %68, i8* %69, i32 4096)
br label %71
71: ; preds = %67, %54, %49
br label %72
72: ; preds = %71, %42
br label %37
73: ; preds = %37
%74 = load i32*, i32** %8
%75 = call i32 @fclose(i32* %74)
store i32 0, i32* %4
br label %76
76: ; preds = %73, %66
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"%lx-%lx %4s %lx %x:%x %lu %s\00"
define i32 @trace_add_files_from_proc(i32 %0, i32 %1, i8* %2) {
%4 = alloca i8
%5 = alloca i64
%6 = alloca [4096 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca [5 x i8]
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca [4096 x i8]
%15 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%17 = call i32 @snprintf(i8* %4, i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %1)
%18 = add nsw i32 %17, 1
%19 = call i8* @malloc(i32 %18)
%20 = call i32 @snprintf(i8* %19, i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %1)
%21 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = call i8* @read_line(i8* null, i64* %5, i32* %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %53, label %24
24: ; preds = %3
%25 = bitcast i64* %7 to i8*
%26 = bitcast i64* %8 to i8*
%27 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%28 = bitcast i64* %10 to i8*
%29 = bitcast i32* %11 to i8*
%30 = bitcast i32* %12 to i8*
%31 = bitcast i64* %13 to i8*
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %14, i64 0, i64 0
br label %33
33: ; preds = %24, %50
%34 = phi i8* [ %22, %24 ], [ %51, %50 ]
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64* %7, i64* %8, i8* %27, i64* %10, i32* %11, i32* %12, i64* %13, i8* %32)
%36 = load i64, i64* %13
%37 = icmp eq i64 %36, 0
br i1 %37, label %50, label %38
38: ; preds = %33
%39 = call i64 @strncmp(i8* %32, i8* %2, i32 4096)
%40 = icmp eq i64 %39, 0
br i1 %40, label %50, label %41
41: ; preds = %38
%42 = call i64 @strncmp(i8* %16, i8* %32, i32 4096)
%43 = icmp eq i64 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %41
%45 = call i32 @path_is_dir(i8* %32)
%46 = call i64 @db_add_file_open(i32 %0, i8* %32, i32 1, i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = call i32 @strncpy(i8* %16, i8* %32, i32 4096)
br label %50
50: ; preds = %33, %48, %41, %38
%51 = call i8* @read_line(i8* %34, i64* %5, i32* %21)
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %33
53: ; preds = %50, %3
%54 = call i32 @fclose(i32* %21)
br label %56
55: ; preds = %44
br label %56
56: ; preds = %55, %53
%57 = phi i32 [ 0, %53 ], [ -1, %55 ]
ret i32 %57
}