#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 /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int PATH_MAX;
int /*<<< orphan*/ SYS_readlink;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int syscall(int /*<<< orphan*/, char *, char *, int);
int main(int argc, char *argv[]) {
  char path[PATH_MAX];
  int status;
  bzero(path, sizeof(path));
  status = syscall(SYS_readlink, "/proc/self/exe", path, PATH_MAX);
  if (status < 0) {
    perror("readlink()");
    exit(EXIT_FAILURE);
  }
  if (status >= PATH_MAX)
    return 125;
  if (path[status] != '\0') {
    path[PATH_MAX - 1] = '\0';
    puts(path);
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%esi
call   1060 <bzero@plt>
mov    -0x30(%rbp),%rdx
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%ecx
lea    0xe4a(%rip),%rsi
call   1040 <syscall@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e2 <main+0x92>
lea    0xe40(%rip),%rdi
call   1050 <perror@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%eax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%eax
jl     120a <main+0xba>
movl   $0x7d,-0x4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1248 <main+0xf8>
mov    -0x30(%rbp),%rdi
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
cltq
movb   $0x0,(%rdi,%rax,1)
call   1030 <puts@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8f(%rip),%r15
mov    (%r15),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <bzero@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1040 <syscall@plt>
test   %eax,%eax
js     11b9 <main+0x69>
mov    (%r15),%ecx
cmp    %ecx,%eax
jl     11c7 <main+0x77>
mov    $0x7d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4f(%rip),%rdi
call   1050 <perror@plt>
jmp    11ec <main+0x9c>
mov    %eax,%eax
cmpb   $0x0,(%rbx,%rax,1)
jne    11dd <main+0x8d>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0xffffffff,%ecx
movb   $0x0,(%rbx,%rcx,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@SYS_readlink = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [11 x i8] c"readlink()\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = trunc i64 %10 to i32
%14 = call i32 @bzero(i8* %12, i32 %13)
%15 = load i32, i32* @SYS_readlink
%16 = load i32, i32* @PATH_MAX
%17 = call i32 @syscall(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i32, i32* %8
%26 = load i32, i32* @PATH_MAX
%27 = icmp sge i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24
store i32 125, i32* %3
%29 = load i8*, i8** %6
%30 = load i32, i32* %3
ret i32 %30
31: ; preds = %24
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %12, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %31
%39 = load i32, i32* @PATH_MAX
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %12, i64 %41
store i8 0, i8* %42
%43 = call i32 @puts(i8* %12)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %31
%47 = load i32, i32* @EXIT_SUCCESS
%48 = call i32 @exit(i32 %47)
unreachable
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@SYS_readlink = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [11 x i8] c"readlink()\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @bzero(i8* %5, i32 %3)
%7 = load i32, i32* @SYS_readlink
%8 = call i32 @syscall(i32 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* @PATH_MAX
%16 = icmp slt i32 %8, %15
br i1 %16, label %18, label %17
17: ; preds = %14
ret i32 125
18: ; preds = %14
%19 = zext i32 %8 to i64
%20 = getelementptr inbounds i8, i8* %5, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %18
%24 = add nsw i32 %15, -1
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %5, i64 %25
store i8 0, i8* %26
%27 = call i32 @puts(i8* %5)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %18
%31 = load i32, i32* @EXIT_SUCCESS
%32 = call i32 @exit(i32 %31)
unreachable
}