#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 PATH_MAX;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ g_setjmp(int /*<<< orphan*/, char *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ *);
int getpid();
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int self_get_path(char *out) {
  g_setjmp(0, "self_get_path", NULL, NULL);
  char path[PATH_MAX];
  path[0] = 0x0;
  int r;
  snprintf(path, PATH_MAX, "/proc/%d/exe", getpid());
  if (access(path, R_OK)) {
    snprintf(path, PATH_MAX, "/compat/linux/proc/%d/exe", getpid());
  } else {
    goto read;
  }
  if (access(path, R_OK)) {
    snprintf(path, PATH_MAX, "/proc/%d/file", getpid());
  }
read:
  if ((r = readlink(path, out, PATH_MAX)) < 1) {
    return 0;
  }
  out[r] = 0x0;
  return 0;
}


x86 assembly (clang, -O0):
<self_get_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9d(%rip),%rsi
xor    %edi,%edi
mov    %edi,%ecx
mov    %rcx,%rdx
call   1030 <g_setjmp@plt>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x30(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
lea    0xe58(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     1211 <self_get_path+0xc1>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    %eax,%ecx
lea    0xe07(%rip),%rdx
call   1060 <snprintf@plt>
jmp    1216 <self_get_path+0xc6>
jmp    125f <self_get_path+0x10f>
mov    -0x30(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     125a <self_get_path+0x10a>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    %eax,%ecx
lea    0xdd3(%rip),%rdx
call   1060 <snprintf@plt>
jmp    125f <self_get_path+0x10f>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
call   1040 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x1,%eax
jge    1294 <self_get_path+0x144>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    12ae <self_get_path+0x15e>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<self_get_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe98(%rip),%rsi
xor    %edi,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <g_setjmp@plt>
mov    0x2e76(%rip),%r12
mov    (%r12),%r15d
mov    %rsp,%rax
mov    %r15,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rax,%rbx
sub    %rcx,%rbx
mov    %rbx,%rsp
neg    %rcx
movb   $0x0,(%rax,%rcx,1)
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe71(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    0x2e1a(%rip),%r13
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     121e <self_get_path+0xce>
mov    (%r12),%r15d
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe2d(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     121e <self_get_path+0xce>
mov    (%r12),%r15d
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe17(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <readlink@plt>
test   %eax,%eax
jle    1238 <self_get_path+0xe8>
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"self_get_path\00"
@PATH_MAX = global i32 0
@.str.1 = [13 x i8] c"/proc/%d/exe\00"
@R_OK = global i32 0
@.str.2 = [26 x i8] c"/compat/linux/proc/%d/exe\00"
@.str.3 = [14 x i8] c"/proc/%d/file\00"
define i32 @self_get_path(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = call i32 @g_setjmp(i32 0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* null, i32* null)
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
%14 = load i32, i32* @PATH_MAX
%15 = call i32 (...) @getpid()
%16 = call i32 @snprintf(i8* %12, i32 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @R_OK
%18 = call i64 @access(i8* %12, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %1
%21 = load i32, i32* @PATH_MAX
%22 = call i32 (...) @getpid()
%23 = call i32 @snprintf(i8* %12, i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %25
24: ; preds = %1
br label %34
25: ; preds = %20
%26 = load i32, i32* @R_OK
%27 = call i64 @access(i8* %12, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @PATH_MAX
%31 = call i32 (...) @getpid()
%32 = call i32 @snprintf(i8* %12, i32 %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %29, %25
br label %34
34: ; preds = %33, %24
%35 = load i8*, i8** %3
%36 = load i32, i32* @PATH_MAX
%37 = call i32 @readlink(i8* %12, i8* %35, i32 %36)
store i32 %37, i32* %6
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 0, i32* %2
store i32 1, i32* %7
br label %45
40: ; preds = %34
%41 = load i8*, i8** %3
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 0, i8* %44
store i32 0, i32* %2
store i32 1, i32* %7
br label %45
45: ; preds = %40, %39
%46 = load i8*, i8** %4
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@.str = [14 x i8] c"self_get_path\00"
@PATH_MAX = global i32 0
@.str.1 = [13 x i8] c"/proc/%d/exe\00"
@R_OK = global i32 0
@.str.2 = [26 x i8] c"/compat/linux/proc/%d/exe\00"
@.str.3 = [14 x i8] c"/proc/%d/file\00"
define i32 @self_get_path(i8* nocapture %0) {
%2 = tail call i32 @g_setjmp(i32 0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* null, i32* null)
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
store i8 0, i8* %5
%6 = tail call i32 (...) @getpid()
%7 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @R_OK
%9 = call i64 @access(i8* %5, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %1
%12 = load i32, i32* @PATH_MAX
%13 = call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %5, i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @R_OK
%16 = call i64 @access(i8* %5, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = load i32, i32* @PATH_MAX
%20 = call i32 (...) @getpid()
%21 = call i32 @snprintf(i8* %5, i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %11, %18, %1
%23 = load i32, i32* @PATH_MAX
%24 = call i32 @readlink(i8* %5, i8* %0, i32 %23)
%25 = icmp slt i32 %24, 1
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = zext i32 %24 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %22, %26
ret i32 0
}