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

/* Variables and functions */
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ errno;
int getpid();
int readlink(char *, char *, size_t);
scalar_t__ snprintf(char *, int, char *, int);
char *getexename(char *buf, size_t size) {
  char linkname[64];
  pid_t pid;
  int ret;
  pid = getpid();
  if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", pid) < 0) {
    abort();
  }
  ret = readlink(linkname, buf, size);
  if (ret == -1)
    return NULL;
  if (ret >= size) {
    errno = ERANGE;
    return NULL;
  }
  buf[ret] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<getexename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%ecx
mov    $0x40,%esi
lea    0xe93(%rip),%rdx
call   1060 <snprintf@plt>
cmp    $0x0,%rax
jge    1183 <getexename+0x43>
mov    $0x0,%al
call   1030 <abort@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <readlink@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0xffffffff,-0x68(%rbp)
jne    11ae <getexename+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    11ef <getexename+0xaf>
movslq -0x68(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11db <getexename+0x9b>
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0c(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11ef <getexename+0xaf>
mov    -0x10(%rbp),%rax
movslq -0x68(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getexename>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xea5(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
test   %rax,%rax
js     11ae <getexename+0x6e>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11a1 <getexename+0x61>
cltq
cmp    %r14,%rax
jae    118f <getexename+0x4f>
movb   $0x0,(%rbx,%rax,1)
jmp    11a3 <getexename+0x63>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    0x2e39(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%i/exe\00"
@ERANGE = global i32 0
@errno = global i32 0
define i8* @getexename(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [64 x i8]
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = call i32 (...) @getpid()
store i32 %9, i32* %7
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* %7
%12 = call i64 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (...) @abort()
unreachable
16: ; preds = %2
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%18 = load i8*, i8** %4
%19 = load i64, i64* %5
%20 = call i32 @readlink(i8* %17, i8* %18, i64 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %16
store i8* null, i8** %3
br label %37
24: ; preds = %16
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = load i64, i64* %5
%28 = icmp uge i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i32, i32* @ERANGE
store i32 %30, i32* @errno
store i8* null, i8** %3
br label %37
31: ; preds = %24
%32 = load i8*, i8** %4
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %4
store i8* %36, i8** %3
br label %37
37: ; preds = %31, %29, %23
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%i/exe\00"
@ERANGE = global i32 0
@errno = global i32 0
define i8* @getexename(i8* %0, i64 %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = call i64 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 (...) @abort()
unreachable
10: ; preds = %2
%11 = call i32 @readlink(i8* %4, i8* %0, i64 %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = sext i32 %11 to i64
%15 = icmp ult i64 %14, %1
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = load i32, i32* @ERANGE
store i32 %17, i32* @errno
br label %20
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %19
br label %20
20: ; preds = %10, %18, %16
%21 = phi i8* [ null, %16 ], [ %0, %18 ], [ null, %10 ]
ret i8* %21
}