#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 *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int readlink(char *, char *, size_t);
scalar_t__ realloc(char *, size_t);
char *get_self_path() {
  char *exe = NULL;
  size_t size = 100;
  while (1) {
    exe = (char *)realloc(exe, size);
    if (!exe)
      return NULL;
    memset(exe, 0, size);
    int nchars = readlink("/proc/self/exe", exe, size);
    if (nchars < 0) {
      free(exe);
      return NULL;
    }
    if (nchars < size)
      return exe;
    size *= 2;
  }
}


x86 assembly (clang, -O0):
<get_self_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x64,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1181 <get_self_path+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11f3 <get_self_path+0xb3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe61(%rip),%rdi
call   1040 <readlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c7 <get_self_path+0x87>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f3 <get_self_path+0xb3>
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11e2 <get_self_path+0xa2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f3 <get_self_path+0xb3>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1158 <get_self_path+0x18>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_self_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
mov    $0x64,%ebx
mov    $0x64,%esi
xor    %edi,%edi
call   1060 <realloc@plt>
test   %rax,%rax
je     11be <get_self_path+0x7e>
mov    %rax,%r15
xor    %r14d,%r14d
lea    0xe92(%rip),%r12
xchg   %ax,%ax
mov    %r15,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
test   %eax,%eax
js     11ae <get_self_path+0x6e>
mov    %eax,%eax
cmp    %rax,%rbx
ja     11bb <get_self_path+0x7b>
add    %rbx,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1170 <get_self_path+0x30>
jmp    11be <get_self_path+0x7e>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    11be <get_self_path+0x7e>
mov    %r15,%r14
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"
define i8* @get_self_path() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
store i8* null, i8** %2
store i64 100, i64* %3
br label %5
5: ; preds = %0, %32
%6 = load i8*, i8** %2
%7 = load i64, i64* %3
%8 = call i64 @realloc(i8* %6, i64 %7)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %5
store i8* null, i8** %1
br label %35
13: ; preds = %5
%14 = load i8*, i8** %2
%15 = load i64, i64* %3
%16 = call i32 @memset(i8* %14, i32 0, i64 %15)
%17 = load i8*, i8** %2
%18 = load i64, i64* %3
%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* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = load i8*, i8** %2
%24 = call i32 @free(i8* %23)
store i8* null, i8** %1
br label %35
25: ; preds = %13
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = load i64, i64* %3
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i8*, i8** %2
store i8* %31, i8** %1
br label %35
32: ; preds = %25
%33 = load i64, i64* %3
%34 = mul i64 %33, 2
store i64 %34, i64* %3
br label %5
35: ; preds = %30, %22, %12
%36 = load i8*, i8** %1
ret i8* %36
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @get_self_path() {
%1 = tail call i64 @realloc(i8* null, i64 100)
%2 = icmp eq i64 %1, 0
br i1 %2, label %22, label %6
3: ; preds = %16
%4 = tail call i64 @realloc(i8* %9, i64 %19)
%5 = icmp eq i64 %4, 0
br i1 %5, label %22, label %6
6: ; preds = %0, %3
%7 = phi i64 [ %4, %3 ], [ %1, %0 ]
%8 = phi i64 [ %19, %3 ], [ 100, %0 ]
%9 = inttoptr i64 %7 to i8*
%10 = tail call i32 @memset(i8* %9, i32 0, i64 %8)
%11 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9, i64 %8)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = inttoptr i64 %7 to i8*
%15 = tail call i32 @free(i8* %14)
br label %22
16: ; preds = %6
%17 = zext i32 %11 to i64
%18 = icmp ugt i64 %8, %17
%19 = shl i64 %8, 1
br i1 %18, label %20, label %3
20: ; preds = %16
%21 = inttoptr i64 %7 to i8*
br label %22
22: ; preds = %3, %20, %0, %13
%23 = phi i8* [ null, %13 ], [ null, %0 ], [ %21, %20 ], [ null, %3 ]
ret i8* %23
}