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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *memRealloc(char *, int);
int readlink(char *, char *, int);
char *utilExecutablePath() {
  static char *path = 0;
  int size = 16;
  int length = 0;
  if (!path) {
    do {
      path = memRealloc(path, sizeof(char) * (size *= 2));
      length = readlink("/proc/self/exe", path, size);
      if (length < 0)
        length = readlink("/proc/curproc/file", path, size);
    } while (length >= size);
    if (length > 0) {
      path[length] = '\0';
    } else {
      free(path);
      path = 0;
    }
  }
  return path;
}


x86 assembly (clang, -O0):
<utilExecutablePath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x10,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpq   $0x0,0x2ef2(%rip)
jne    1204 <utilExecutablePath+0xd4>
jmp    1159 <utilExecutablePath+0x29>
mov    0x2ee0(%rip),%rdi
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <memRealloc@plt>
mov    %rax,0x2ec3(%rip)
mov    0x2ebc(%rip),%rsi
mov    -0x4(%rbp),%edx
lea    0xe72(%rip),%rdi
call   1040 <readlink@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b9 <utilExecutablePath+0x89>
mov    0x2e99(%rip),%rsi
mov    -0x4(%rbp),%edx
lea    0xe5e(%rip),%rdi
call   1040 <readlink@plt>
mov    %eax,-0x8(%rbp)
jmp    11be <utilExecutablePath+0x8e>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1159 <utilExecutablePath+0x29>
cmpl   $0x0,-0x8(%rbp)
jle    11e8 <utilExecutablePath+0xb8>
mov    0x2e65(%rip),%rax
movslq -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ff <utilExecutablePath+0xcf>
mov    0x2e51(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2e41(%rip)
jmp    1204 <utilExecutablePath+0xd4>
mov    0x2e35(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utilExecutablePath>:
push   %r15
push   %r14
push   %rbx
mov    0x2f04(%rip),%rax
test   %rax,%rax
jne    11cb <utilExecutablePath+0x9b>
mov    $0x10,%ebx
xor    %edi,%edi
lea    0xead(%rip),%r14
lea    0xeb5(%rip),%r15
nopw   0x0(%rax,%rax,1)
add    %ebx,%ebx
mov    %ebx,%esi
call   1050 <memRealloc@plt>
mov    %rax,0x2ed0(%rip)
mov    %r14,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <readlink@plt>
mov    %eax,%ecx
test   %eax,%eax
jns    1196 <utilExecutablePath+0x66>
mov    0x2eb6(%rip),%rsi
mov    %r15,%rdi
mov    %ebx,%edx
call   1040 <readlink@plt>
mov    %eax,%ecx
cmp    %ebx,%ecx
jl     11a3 <utilExecutablePath+0x73>
mov    0x2e9f(%rip),%rdi
jmp    1160 <utilExecutablePath+0x30>
mov    0x2e96(%rip),%rax
test   %ecx,%ecx
jle    11b6 <utilExecutablePath+0x86>
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    11cb <utilExecutablePath+0x9b>
mov    %rax,%rdi
call   1030 <free@plt>
movq   $0x0,0x2e77(%rip)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@utilExecutablePath.path = internal global i8* null
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [19 x i8] c"/proc/curproc/file\00"
define i8* @utilExecutablePath() {
%1 = alloca i32
%2 = alloca i32
store i32 16, i32* %1
store i32 0, i32* %2
%3 = load i8*, i8** @utilExecutablePath.path
%4 = icmp ne i8* %3, null
br i1 %4, label %40, label %5
5: ; preds = %0
br label %6
6: ; preds = %24, %5
%7 = load i8*, i8** @utilExecutablePath.path
%8 = load i32, i32* %1
%9 = mul nsw i32 %8, 2
store i32 %9, i32* %1
%10 = sext i32 %9 to i64
%11 = mul i64 1, %10
%12 = trunc i64 %11 to i32
%13 = call i8* @memRealloc(i8* %7, i32 %12)
store i8* %13, i8** @utilExecutablePath.path
%14 = load i8*, i8** @utilExecutablePath.path
%15 = load i32, i32* %1
%16 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %6
%20 = load i8*, i8** @utilExecutablePath.path
%21 = load i32, i32* %1
%22 = call i32 @readlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %21)
store i32 %22, i32* %2
br label %23
23: ; preds = %19, %6
br label %24
24: ; preds = %23
%25 = load i32, i32* %2
%26 = load i32, i32* %1
%27 = icmp sge i32 %25, %26
br i1 %27, label %6, label %28
28: ; preds = %24
%29 = load i32, i32* %2
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i8*, i8** @utilExecutablePath.path
%33 = load i32, i32* %2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
br label %39
36: ; preds = %28
%37 = load i8*, i8** @utilExecutablePath.path
%38 = call i32 @free(i8* %37)
store i8* null, i8** @utilExecutablePath.path
br label %39
39: ; preds = %36, %31
br label %40
40: ; preds = %39, %0
%41 = load i8*, i8** @utilExecutablePath.path
ret i8* %41
}

LLVM IR (-O2):
@utilExecutablePath.path = internal unnamed_addr global i8* null
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [19 x i8] c"/proc/curproc/file\00"
define i8* @utilExecutablePath() {
%1 = load i8*, i8** @utilExecutablePath.path
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %26
3: ; preds = %0, %16
%4 = phi i8* [ %17, %16 ], [ null, %0 ]
%5 = phi i32 [ %6, %16 ], [ 16, %0 ]
%6 = shl nsw i32 %5, 1
%7 = tail call i8* @memRealloc(i8* %4, i32 %6)
store i8* %7, i8** @utilExecutablePath.path
%8 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %6)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i8*, i8** @utilExecutablePath.path
%12 = tail call i32 @readlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 %6)
br label %13
13: ; preds = %3, %10
%14 = phi i32 [ %12, %10 ], [ %8, %3 ]
%15 = icmp slt i32 %14, %6
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = load i8*, i8** @utilExecutablePath.path
br label %3
18: ; preds = %13
%19 = icmp sgt i32 %14, 0
%20 = load i8*, i8** @utilExecutablePath.path
br i1 %19, label %21, label %24
21: ; preds = %18
%22 = zext i32 %14 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
br label %26
24: ; preds = %18
%25 = tail call i32 @free(i8* %20)
store i8* null, i8** @utilExecutablePath.path
br label %26
26: ; preds = %21, %24, %0
%27 = phi i8* [ %20, %21 ], [ null, %24 ], [ %1, %0 ]
ret i8* %27
}