#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 getpid();
unsigned int readlink(char *, char *, unsigned int);
char *realloc(char *, unsigned int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
const char *paths_exec_path(void) {
  static char *exec_path_buf;
  if (!exec_path_buf) {
    char file[512];
    unsigned size = 1;
    unsigned written = 0;
    snprintf(file, sizeof file, "/proc/%u/exe", getpid());
    do {
      size *= 2;
      exec_path_buf = realloc(exec_path_buf, size);
      if (!exec_path_buf ||
          ((int)(written = readlink(file, exec_path_buf, size)) == -1)) {
        free(exec_path_buf);
        exec_path_buf = "";
        break;
      }
    } while (written == size);
    if (exec_path_buf[0]) {
      exec_path_buf = realloc(exec_path_buf, written + 1);
      exec_path_buf[written] = '\0';
    }
  }
  return exec_path_buf;
}


x86 assembly (clang, -O0):
<paths_exec_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
cmpq   $0x0,0x2eed(%rip)
jne    1282 <paths_exec_path+0x132>
movl   $0x1,-0x204(%rbp)
movl   $0x0,-0x208(%rbp)
lea    -0x200(%rbp),%rax
mov    %rax,-0x210(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x210(%rbp),%rdi
mov    %eax,%ecx
mov    $0x200,%esi
lea    0xe59(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x204(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x204(%rbp)
mov    0x2e8e(%rip),%rdi
mov    -0x204(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,0x2e7c(%rip)
cmpq   $0x0,0x2e74(%rip)
je     120a <paths_exec_path+0xba>
lea    -0x200(%rbp),%rdi
mov    0x2e60(%rip),%rsi
mov    -0x204(%rbp),%edx
call   1040 <readlink@plt>
mov    %eax,-0x208(%rbp)
cmp    $0xffffffff,%eax
jne    1229 <paths_exec_path+0xd9>
mov    0x2e3f(%rip),%rdi
call   1030 <free@plt>
lea    0xdef(%rip),%rax
mov    %rax,0x2e2c(%rip)
jmp    1240 <paths_exec_path+0xf0>
jmp    122e <paths_exec_path+0xde>
mov    -0x208(%rbp),%eax
cmp    -0x204(%rbp),%eax
je     11ac <paths_exec_path+0x5c>
mov    0x2e09(%rip),%rax
cmpb   $0x0,(%rax)
je     127d <paths_exec_path+0x12d>
mov    0x2df9(%rip),%rdi
mov    -0x208(%rbp),%esi
add    $0x1,%esi
call   1070 <realloc@plt>
mov    %rax,0x2de4(%rip)
mov    0x2ddd(%rip),%rax
mov    -0x208(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    1282 <paths_exec_path+0x132>
mov    0x2dc7(%rip),%rax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<paths_exec_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    0x2eec(%rip),%rax
test   %rax,%rax
jne    123d <paths_exec_path+0xed>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe83(%rip),%rdx
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    0x2ebd(%rip),%rdi
mov    $0x2,%ebp
mov    $0x2,%esi
call   1070 <realloc@plt>
mov    %rax,0x2ea7(%rip)
test   %rax,%rax
je     11f9 <paths_exec_path+0xa9>
mov    %rsp,%r15
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%ebx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11fe <paths_exec_path+0xae>
mov    %eax,%ecx
cmp    %ebx,%eax
jne    124b <paths_exec_path+0xfb>
lea    (%rbx,%rbx,1),%ebp
mov    0x2e6c(%rip),%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,0x2e5e(%rip)
test   %rax,%rax
jne    11c0 <paths_exec_path+0x70>
jmp    120a <paths_exec_path+0xba>
xor    %r14d,%r14d
jmp    120a <paths_exec_path+0xba>
mov    $0xffffffff,%ebx
mov    0x2e46(%rip),%r14
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xdf3(%rip),%rax
mov    %rax,0x2e30(%rip)
cmpb   $0x0,(%rax)
je     123d <paths_exec_path+0xed>
lea    0x1(%rbx),%esi
mov    %rax,%rdi
call   1070 <realloc@plt>
mov    %rax,0x2e19(%rip)
mov    %ebx,%ecx
movb   $0x0,(%rax,%rcx,1)
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dfe(%rip),%rax
mov    %ecx,%ebx
cmpb   $0x0,(%rax)
jne    1225 <paths_exec_path+0xd5>
jmp    123d <paths_exec_path+0xed>

LLVM IR (-O0):
@paths_exec_path.exec_path_buf = internal global i8* null
@.str = [13 x i8] c"/proc/%u/exe\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @paths_exec_path() {
%1 = alloca [512 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = load i8*, i8** @paths_exec_path.exec_path_buf
%5 = icmp ne i8* %4, null
br i1 %5, label %47, label %6
6: ; preds = %0
store i32 1, i32* %2
store i32 0, i32* %3
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%8 = call i32 (...) @getpid()
%9 = call i32 @snprintf(i8* %7, i32 512, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %28, %6
%11 = load i32, i32* %2
%12 = mul i32 %11, 2
store i32 %12, i32* %2
%13 = load i8*, i8** @paths_exec_path.exec_path_buf
%14 = load i32, i32* %2
%15 = call i8* @realloc(i8* %13, i32 %14)
store i8* %15, i8** @paths_exec_path.exec_path_buf
%16 = load i8*, i8** @paths_exec_path.exec_path_buf
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %24
18: ; preds = %10
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%20 = load i8*, i8** @paths_exec_path.exec_path_buf
%21 = load i32, i32* %2
%22 = call i32 @readlink(i8* %19, i8* %20, i32 %21)
store i32 %22, i32* %3
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %18, %10
%25 = load i8*, i8** @paths_exec_path.exec_path_buf
%26 = call i32 @free(i8* %25)
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** @paths_exec_path.exec_path_buf
br label %32
27: ; preds = %18
br label %28
28: ; preds = %27
%29 = load i32, i32* %3
%30 = load i32, i32* %2
%31 = icmp eq i32 %29, %30
br i1 %31, label %10, label %32
32: ; preds = %28, %24
%33 = load i8*, i8** @paths_exec_path.exec_path_buf
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = icmp ne i8 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %32
%38 = load i8*, i8** @paths_exec_path.exec_path_buf
%39 = load i32, i32* %3
%40 = add i32 %39, 1
%41 = call i8* @realloc(i8* %38, i32 %40)
store i8* %41, i8** @paths_exec_path.exec_path_buf
%42 = load i8*, i8** @paths_exec_path.exec_path_buf
%43 = load i32, i32* %3
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %37, %32
br label %47
47: ; preds = %46, %0
%48 = load i8*, i8** @paths_exec_path.exec_path_buf
ret i8* %48
}

LLVM IR (-O2):
@paths_exec_path.exec_path_buf = internal unnamed_addr global i8* null
@.str = [13 x i8] c"/proc/%u/exe\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @paths_exec_path() {
%1 = alloca [512 x i8]
%2 = load i8*, i8** @paths_exec_path.exec_path_buf
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %43
4: ; preds = %0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%6 = tail call i32 (...) @getpid()
%7 = call i32 @snprintf(i8* %5, i32 512, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i8*, i8** @paths_exec_path.exec_path_buf
%9 = call i8* @realloc(i8* %8, i32 2)
store i8* %9, i8** @paths_exec_path.exec_path_buf
%10 = icmp eq i8* %9, null
br i1 %10, label %23, label %16
11: ; preds = %27
%12 = shl i32 %18, 1
%13 = load i8*, i8** @paths_exec_path.exec_path_buf
%14 = call i8* @realloc(i8* %13, i32 %12)
store i8* %14, i8** @paths_exec_path.exec_path_buf
%15 = icmp eq i8* %14, null
br i1 %15, label %23, label %16
16: ; preds = %4, %11
%17 = phi i8* [ %14, %11 ], [ %9, %4 ]
%18 = phi i32 [ %12, %11 ], [ 2, %4 ]
%19 = call i32 @readlink(i8* %5, i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i8*, i8** @paths_exec_path.exec_path_buf
br label %23
23: ; preds = %11, %21, %4
%24 = phi i8* [ null, %4 ], [ %22, %21 ], [ null, %11 ]
%25 = phi i32 [ 0, %4 ], [ -1, %21 ], [ %18, %11 ]
%26 = call i32 @free(i8* %24)
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** @paths_exec_path.exec_path_buf
br label %31
27: ; preds = %16
%28 = icmp eq i32 %19, %18
br i1 %28, label %11, label %29
29: ; preds = %27
%30 = load i8*, i8** @paths_exec_path.exec_path_buf
br label %31
31: ; preds = %29, %23
%32 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %23 ], [ %30, %29 ]
%33 = phi i32 [ %25, %23 ], [ %19, %29 ]
%34 = load i8, i8* %32
%35 = icmp eq i8 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %31
%37 = add i32 %33, 1
%38 = call i8* @realloc(i8* %32, i32 %37)
store i8* %38, i8** @paths_exec_path.exec_path_buf
%39 = zext i32 %33 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %36, %31
%42 = phi i8* [ %38, %36 ], [ %32, %31 ]
br label %43
43: ; preds = %41, %0
%44 = phi i8* [ %42, %41 ], [ %2, %0 ]
ret i8* %44
}