#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
size_t readlink(char *, char *, size_t);
char *realloc(char *, size_t);
char *executable(void) {
  char *ptr, *buf = NULL;
  size_t len = 0;
  ssize_t cnt = 0;
  buf = NULL;
  do {
    len += 32;
    ptr = realloc(buf, len);
    if (ptr == NULL) {
      if (buf != NULL) {
        free(buf);
      }
      return NULL;
    }
    buf = ptr;
    cnt = readlink("/proc/self/exe", buf, len);
  } while (cnt >= 0 && (size_t)cnt == len);
  if (cnt >= 0) {
    buf[cnt] = '\0';
    return buf;
  }
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<executable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <executable+0x71>
cmpq   $0x0,-0x18(%rbp)
je     1194 <executable+0x64>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    121f <executable+0xef>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe48(%rip),%rdi
call   1040 <readlink@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jb     11df <executable+0xaf>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1158 <executable+0x28>
cmpq   $0x0,-0x28(%rbp)
jb     120e <executable+0xde>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121f <executable+0xef>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executable>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %r15d,%r15d
lea    0xebe(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x20,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     1184 <executable+0x54>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
mov    %r12,%r15
cmp    %rax,%rbx
je     1150 <executable+0x20>
movb   $0x0,(%r12,%rax,1)
jmp    1191 <executable+0x61>
test   %r15,%r15
je     1191 <executable+0x61>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%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* @executable() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i8* null, i8** %3
store i64 0, i64* %4
store i64 0, i64* %5
store i8* null, i8** %3
br label %6
6: ; preds = %33, %0
%7 = load i64, i64* %4
%8 = add i64 %7, 32
store i64 %8, i64* %4
%9 = load i8*, i8** %3
%10 = load i64, i64* %4
%11 = call i8* @realloc(i8* %9, i64 %10)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %21
14: ; preds = %6
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = call i32 @free(i8* %18)
br label %20
20: ; preds = %17, %14
store i8* null, i8** %1
br label %46
21: ; preds = %6
%22 = load i8*, i8** %2
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = load i64, i64* %4
%25 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %23, i64 %24)
store i64 %25, i64* %5
br label %26
26: ; preds = %21
%27 = load i64, i64* %5
%28 = icmp uge i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i64, i64* %5
%31 = load i64, i64* %4
%32 = icmp eq i64 %30, %31
br label %33
33: ; preds = %29, %26
%34 = phi i1 [ false, %26 ], [ %32, %29 ]
br i1 %34, label %6, label %35
35: ; preds = %33
%36 = load i64, i64* %5
%37 = icmp uge i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i8*, i8** %3
%40 = load i64, i64* %5
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %3
store i8* %42, i8** %1
br label %46
43: ; preds = %35
%44 = load i8*, i8** %3
%45 = call i32 @free(i8* %44)
store i8* null, i8** %1
br label %46
46: ; preds = %43, %38, %20
%47 = load i8*, i8** %1
ret i8* %47
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @executable() {
br label %1
1: ; preds = %11, %0
%2 = phi i8* [ null, %0 ], [ %5, %11 ]
%3 = phi i64 [ 0, %0 ], [ %4, %11 ]
%4 = add i64 %3, 32
%5 = tail call i8* @realloc(i8* %2, i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = icmp eq i8* %2, null
br i1 %8, label %16, label %9
9: ; preds = %7
%10 = tail call i32 @free(i8* %2)
br label %16
11: ; preds = %1
%12 = tail call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i64 %4)
%13 = icmp eq i64 %12, %4
br i1 %13, label %1, label %14
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %5, i64 %12
store i8 0, i8* %15
br label %16
16: ; preds = %7, %9, %14
ret i8* %5
}