#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 int ssize_t;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fatal(char *, int /*<<< orphan*/);
int /*<<< orphan*/ format(char *, int /*<<< orphan*/);
int readlink(int /*<<< orphan*/, char *, int);
char *xstrdup(char *);
char *get_exe(pid_t pid) {
  char buf[PATH_MAX];
  ssize_t n;
  n = readlink(format("/proc"
                      "/%d/exe",
                      pid),
               buf, sizeof(buf) - 1);
  if (n == -1)
    fatal("/proc"
          "/%d/exe"
          ": failed to read symlink",
          pid);
  buf[n] = '\0';
  return xstrdup(buf);
}


x86 assembly (clang, -O0):
<get_exe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x28(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe7b(%rip),%rdi
call   1050 <format@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edi
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1030 <readlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11bb <get_exe+0x7b>
mov    -0x4(%rbp),%esi
lea    0xe57(%rip),%rdi
call   1060 <fatal@plt>
mov    -0x30(%rbp),%rdi
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
call   1040 <xstrdup@plt>
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_exe>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e9c(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe8e(%rip),%rdi
mov    %r14d,%esi
call   1050 <format@plt>
lea    -0x1(%r15),%edx
mov    %eax,%edi
mov    %rbx,%rsi
call   1030 <readlink@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    119f <get_exe+0x5f>
lea    0xe76(%rip),%rdi
mov    %r14d,%esi
call   1060 <fatal@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1040 <xstrdup@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [37 x i8] c"/proc/%d/exe: failed to read symlink\00"
define i8* @get_exe(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %3
%9 = alloca i8, i64 %7
store i64 %7, i64* %4
%10 = load i32, i32* %2
%11 = call i32 @format(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = sub i64 %7, 1
%13 = trunc i64 %12 to i32
%14 = call i32 @readlink(i32 %11, i8* %9, i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32, i32* %2
%19 = call i32 @fatal(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %1
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %9, i64 %22
store i8 0, i8* %23
%24 = call i8* @xstrdup(i8* %9)
%25 = load i8*, i8** %3
ret i8* %24
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [37 x i8] c"/proc/%d/exe: failed to read symlink\00"
define i8* @get_exe(i32 %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32 @format(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = add i32 %2, -1
%7 = call i32 @readlink(i32 %5, i8* %4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @fatal(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %11
11: ; preds = %9, %1
%12 = sext i32 %7 to i64
%13 = getelementptr inbounds i8, i8* %4, i64 %12
store i8 0, i8* %13
%14 = call i8* @xstrdup(i8* %4)
ret i8* %14
}