#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 */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ logMallocError();
int /*<<< orphan*/ logSystemError(char *);
int readlink(char *, char *, size_t);
char *realloc(char *, size_t);
char *strdup(char *);
char *getProgramPath(void) {
  char *path = NULL;
  size_t size = 0X80;
  char *buffer = NULL;
  while (1) {
    {
      char *newBuffer = realloc(buffer, size <<= 1);
      if (!newBuffer) {
        logMallocError();
        break;
      }
      buffer = newBuffer;
    }
    {
      int length = readlink("/proc/self/exe", buffer, size);
      if (length == -1) {
        if (errno != ENOENT)
          logSystemError("readlink");
        break;
      }
      if (length < size) {
        buffer[length] = 0;
        if (!(path = strdup(buffer)))
          logMallocError();
        break;
      }
    }
  }
  if (buffer)
    free(buffer);
  return path;
}


x86 assembly (clang, -O0):
<getProgramPath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x8(%rbp)
movq   $0x80,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
shl    $0x1,%rsi
mov    %rsi,-0x10(%rbp)
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b0 <getProgramPath+0x50>
mov    $0x0,%al
call   1070 <logMallocError@plt>
jmp    1246 <getProgramPath+0xe6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe39(%rip),%rdi
call   1040 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1204 <getProgramPath+0xa4>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
je     11ff <getProgramPath+0x9f>
lea    0xe15(%rip),%rdi
call   1050 <logSystemError@plt>
jmp    1246 <getProgramPath+0xe6>
movslq -0x24(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1241 <getProgramPath+0xe1>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    123c <getProgramPath+0xdc>
mov    $0x0,%al
call   1070 <logMallocError@plt>
jmp    1246 <getProgramPath+0xe6>
jmp    1180 <getProgramPath+0x20>
cmpq   $0x0,-0x18(%rbp)
je     125a <getProgramPath+0xfa>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getProgramPath>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x80,%ebx
xor    %r15d,%r15d
lea    0xe89(%rip),%r14
nopw   0x0(%rax,%rax,1)
add    %rbx,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     11ca <getProgramPath+0x6a>
mov    %rax,%r12
mov    %r14,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11db <getProgramPath+0x7b>
cltq
mov    %r12,%r15
cmp    %rax,%rbx
jbe    1180 <getProgramPath+0x20>
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     1202 <getProgramPath+0xa2>
mov    %rax,%r14
jmp    120c <getProgramPath+0xac>
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <logMallocError@plt>
test   %r15,%r15
jne    120f <getProgramPath+0xaf>
jmp    1217 <getProgramPath+0xb7>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
je     11fd <getProgramPath+0x9d>
lea    0xe17(%rip),%rdi
call   1050 <logSystemError@plt>
xor    %r14d,%r14d
jmp    120c <getProgramPath+0xac>
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <logMallocError@plt>
mov    %r12,%r15
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [9 x i8] c"readlink\00"
define i8* @getProgramPath() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* null, i8** %1
store i64 128, i64* %2
store i8* null, i8** %3
br label %6
6: ; preds = %0, %45
%7 = load i8*, i8** %3
%8 = load i64, i64* %2
%9 = shl i64 %8, 1
store i64 %9, i64* %2
%10 = call i8* @realloc(i8* %7, i64 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %6
%14 = call i32 (...) @logMallocError()
br label %46
15: ; preds = %6
%16 = load i8*, i8** %4
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = load i64, i64* %2
%19 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %17, i64 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %29
22: ; preds = %15
%23 = load i64, i64* @errno
%24 = load i64, i64* @ENOENT
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @logSystemError(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
br label %46
29: ; preds = %15
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = load i64, i64* %2
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %45
34: ; preds = %29
%35 = load i8*, i8** %3
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %3
%40 = call i8* @strdup(i8* %39)
store i8* %40, i8** %1
%41 = icmp ne i8* %40, null
br i1 %41, label %44, label %42
42: ; preds = %34
%43 = call i32 (...) @logMallocError()
br label %44
44: ; preds = %42, %34
br label %46
45: ; preds = %29
br label %6
46: ; preds = %44, %28, %13
%47 = load i8*, i8** %3
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i8*, i8** %3
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %49, %46
%53 = load i8*, i8** %1
ret i8* %53
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [9 x i8] c"readlink\00"
define noalias i8* @getProgramPath() {
br label %1
1: ; preds = %16, %0
%2 = phi i8* [ null, %0 ], [ %5, %16 ]
%3 = phi i64 [ 128, %0 ], [ %4, %16 ]
%4 = shl i64 %3, 1
%5 = tail call i8* @realloc(i8* %2, i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %25, label %7
7: ; preds = %1
%8 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i64 %4)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %28, label %14
14: ; preds = %10
%15 = tail call i32 @logSystemError(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %28
16: ; preds = %7
%17 = sext i32 %8 to i64
%18 = icmp ugt i64 %4, %17
br i1 %18, label %19, label %1
19: ; preds = %16
%20 = getelementptr inbounds i8, i8* %5, i64 %17
store i8 0, i8* %20
%21 = tail call i8* @strdup(i8* %5)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = tail call i32 (...) @logMallocError()
br label %28
25: ; preds = %1
%26 = tail call i32 (...) @logMallocError()
%27 = icmp eq i8* %2, null
br i1 %27, label %32, label %28
28: ; preds = %14, %10, %23, %19, %25
%29 = phi i8* [ null, %25 ], [ %21, %19 ], [ null, %23 ], [ null, %10 ], [ null, %14 ]
%30 = phi i8* [ %2, %25 ], [ %5, %19 ], [ %5, %23 ], [ %5, %10 ], [ %5, %14 ]
%31 = tail call i32 @free(i8* %30)
br label %32
32: ; preds = %28, %25
%33 = phi i8* [ %29, %28 ], [ null, %25 ]
ret i8* %33
}