#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 */
size_t PATH_MAX;
int /*<<< orphan*/ assert(int);
int readlink(char *, char *, size_t);
char *realloc(char *, size_t);
char *find_mypath(void) {
  size_t size = 100;
  char *buf = NULL;
  while (1) {
    int r;
    buf = realloc(buf, size);
    assert(buf != NULL);
    r = readlink("/proc/self/exe", buf, size);
    if (r < size) {
      buf[r] = '\0';
      return buf;
    }
    size += 100;
    assert(size < PATH_MAX);
  }
}


x86 assembly (clang, -O0):
<find_mypath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x64,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xe86(%rip),%rdi
call   1030 <readlink@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    11a6 <find_mypath+0x76>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x8(%rbp),%rax
add    $0x64,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
jmp    1148 <find_mypath+0x18>

x86 assembly (clang, -O2):
<find_mypath>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x64,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
lea    0xe96(%rip),%rdi
mov    $0x64,%edx
mov    %r14,%rsi
call   1030 <readlink@plt>
cmp    $0x64,%eax
jae    1180 <find_mypath+0x40>
cltq
jmp    11e7 <find_mypath+0xa7>
mov    $0xc8,%ebx
mov    0x2e64(%rip),%r12
lea    0xe6d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edi,%edi
cmp    (%r12),%rbx
setb   %dil
call   1040 <assert@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <readlink@plt>
cltq
lea    0x64(%rbx),%rcx
cmp    %rax,%rbx
mov    %rcx,%rbx
jbe    11a0 <find_mypath+0x60>
movb   $0x0,(%r14,%rax,1)
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"
@PATH_MAX = global i64 0
define i8* @find_mypath() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i32
store i64 100, i64* %1
store i8* null, i8** %2
br label %4
4: ; preds = %0, %25
%5 = load i8*, i8** %2
%6 = load i64, i64* %1
%7 = call i8* @realloc(i8* %5, i64 %6)
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %2
%13 = load i64, i64* %1
%14 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %12, i64 %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = load i64, i64* %1
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %4
%20 = load i8*, i8** %2
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** %2
ret i8* %24
25: ; preds = %4
%26 = load i64, i64* %1
%27 = add i64 %26, 100
store i64 %27, i64* %1
%28 = load i64, i64* %1
%29 = load i64, i64* @PATH_MAX
%30 = icmp ult i64 %28, %29
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
br label %4
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@PATH_MAX = global i64 0
define noalias i8* @find_mypath() {
%1 = tail call dereferenceable_or_null(100) i8* @malloc(i64 100)
%2 = icmp ne i8* %1, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %1, i64 100)
%6 = sext i32 %5 to i64
%7 = icmp ult i32 %5, 100
br i1 %7, label %23, label %8
8: ; preds = %0, %8
%9 = phi i8* [ %16, %8 ], [ %1, %0 ]
%10 = phi i64 [ %11, %8 ], [ 100, %0 ]
%11 = add i64 %10, 100
%12 = load i64, i64* @PATH_MAX
%13 = icmp ult i64 %11, %12
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i8* @realloc(i8* %9, i64 %11)
%17 = icmp ne i8* %16, null
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %16, i64 %11)
%21 = sext i32 %20 to i64
%22 = icmp ugt i64 %11, %21
br i1 %22, label %23, label %8
23: ; preds = %8, %0
%24 = phi i8* [ %1, %0 ], [ %16, %8 ]
%25 = phi i64 [ %6, %0 ], [ %21, %8 ]
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 0, i8* %26
ret i8* %24
}