#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ __real_fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *__real_fopen(char *, char *);
int __real_fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int getProcExe(char *buf, int len) {
  int length;
  int pid = getpid();
  char file[64];
  FILE *infile;
  snprintf(file, 64, "/proc/%d/cmdline", pid);
  infile = __real_fopen(file, "r");
  if (infile != NULL) {
    if (!feof(infile)) {
      length = __real_fread(buf, 1, len, infile);
      if (length > 0) {
        __real_fclose(infile);
        return 0;
      }
    }
  }
  __real_fclose(infile);
  return -1;
}


x86 assembly (clang, -O0):
<getProcExe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x40,%esi
lea    0xe74(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1030 <__real_fopen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     1203 <getProcExe+0xa3>
mov    -0x68(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
jne    11fe <getProcExe+0x9e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x68(%rbp),%rcx
mov    $0x1,%esi
call   1080 <__real_fread@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11f9 <getProcExe+0x99>
mov    -0x68(%rbp),%rdi
call   1060 <__real_fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1213 <getProcExe+0xb3>
jmp    11fe <getProcExe+0x9e>
jmp    1203 <getProcExe+0xa3>
mov    -0x68(%rbp),%rdi
call   1060 <__real_fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getProcExe>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe84(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
lea    0xe7c(%rip),%rsi
mov    %rbp,%rdi
call   1030 <__real_fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11b1 <getProcExe+0x51>
mov    %rbp,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11c7 <getProcExe+0x67>
mov    %rbp,%rdi
call   1060 <__real_fclose@plt>
mov    $0xffffffff,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbp,%rcx
call   1080 <__real_fread@plt>
test   %eax,%eax
jle    11b1 <getProcExe+0x51>
mov    %rbp,%rdi
call   1060 <__real_fclose@plt>
xor    %eax,%eax
jmp    11be <getProcExe+0x5e>

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getProcExe(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
%9 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = call i32 (...) @getpid()
store i32 %10, i32* %7
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%12 = load i32, i32* %7
%13 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%15 = call i32* @__real_fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %9
%16 = load i32*, i32** %9
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %34
18: ; preds = %2
%19 = load i32*, i32** %9
%20 = call i32 @feof(i32* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %33, label %22
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = load i32*, i32** %9
%26 = call i32 @__real_fread(i8* %23, i32 1, i32 %24, i32* %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32*, i32** %9
%31 = call i32 @__real_fclose(i32* %30)
store i32 0, i32* %3
br label %37
32: ; preds = %22
br label %33
33: ; preds = %32, %18
br label %34
34: ; preds = %33, %2
%35 = load i32*, i32** %9
%36 = call i32 @__real_fclose(i32* %35)
store i32 -1, i32* %3
br label %37
37: ; preds = %34, %29
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getProcExe(i8* %0, i32 %1) {
%3 = alloca [64 x i8]
%4 = tail call i32 (...) @getpid()
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4)
%7 = call i32* @__real_fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %17, label %9
9: ; preds = %2
%10 = call i32 @feof(i32* %7)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = call i32 @__real_fread(i8* %0, i32 1, i32 %1, i32* %7)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @__real_fclose(i32* %7)
br label %19
17: ; preds = %9, %12, %2
%18 = call i32 @__real_fclose(i32* %7)
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ -1, %17 ], [ 0, %15 ]
ret i32 %20
}