#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 EINVAL;
int /*<<< orphan*/ PATH_MAX;
char *dirname(char *);
int errno;
size_t readlink(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int get_current_exec_path(char *exec_path, size_t path_buff_len) {
  int ret_error;
  if (path_buff_len > 0) {
    char exec_path_buff[PATH_MAX + 1];
    size_t chars_written;
    chars_written = readlink("/proc/self/exe", exec_path_buff, PATH_MAX);
    if (chars_written != -1) {
      char *exec_dir;
      exec_path_buff[chars_written] = '\0';
      exec_dir = dirname(exec_path_buff);
      if (path_buff_len > strlen(exec_dir) + 1) {
        strcpy(exec_path, exec_dir);
        strcat(exec_path, "/");
        ret_error = 0;
      } else {
        exec_path[0] = '\0';
        ret_error = EINVAL;
      }
    } else {
      exec_path[0] = '\0';
      ret_error = errno;
    }
  } else
    ret_error = EINVAL;
  return (ret_error);
}


x86 assembly (clang, -O0):
<get_current_exec_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    1259 <get_current_exec_path+0x109>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x40(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
lea    0xe56(%rip),%rdi
call   1040 <readlink@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
je     123a <get_current_exec_path+0xea>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
call   1060 <dirname@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jbe    1222 <get_current_exec_path+0xd2>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1070 <strcat@plt>
movl   $0x0,-0x14(%rbp)
jmp    1235 <get_current_exec_path+0xe5>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    124d <get_current_exec_path+0xfd>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
jmp    1265 <get_current_exec_path+0x115>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_exec_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rsi,%rsi
je     11dc <get_current_exec_path+0x8c>
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%r12
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rax),%edx
lea    0xe71(%rip),%rdi
mov    %rbx,%rsi
call   1040 <readlink@plt>
cmp    $0xffffffffffffffff,%rax
je     11e7 <get_current_exec_path+0x97>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1060 <dirname@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r15,%rax
jae    11f4 <get_current_exec_path+0xa4>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xe3f(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcat@plt>
xor    %eax,%eax
jmp    1201 <get_current_exec_path+0xb1>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
jmp    1204 <get_current_exec_path+0xb4>
movb   $0x0,(%r14)
mov    0x2de6(%rip),%rax
jmp    11ff <get_current_exec_path+0xaf>
movb   $0x0,(%r14)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [2 x i8] c"/\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @get_current_exec_path(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%10 = load i64, i64* %4
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %49
12: ; preds = %2
%13 = load i32, i32* @PATH_MAX
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = load i32, i32* @PATH_MAX
%19 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = icmp ne i64 %20, -1
br i1 %21, label %22, label %43
22: ; preds = %12
%23 = load i64, i64* %8
%24 = getelementptr inbounds i8, i8* %17, i64 %23
store i8 0, i8* %24
%25 = call i8* @dirname(i8* %17)
store i8* %25, i8** %9
%26 = load i64, i64* %4
%27 = load i8*, i8** %9
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = icmp ugt i64 %26, %30
br i1 %31, label %32, label %38
32: ; preds = %22
%33 = load i8*, i8** %3
%34 = load i8*, i8** %9
%35 = call i32 @strcpy(i8* %33, i8* %34)
%36 = load i8*, i8** %3
%37 = call i32 @strcat(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %42
38: ; preds = %22
%39 = load i8*, i8** %3
%40 = getelementptr inbounds i8, i8* %39, i64 0
store i8 0, i8* %40
%41 = load i32, i32* @EINVAL
store i32 %41, i32* %5
br label %42
42: ; preds = %38, %32
br label %47
43: ; preds = %12
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i64 0
store i8 0, i8* %45
%46 = load i32, i32* @errno
store i32 %46, i32* %5
br label %47
47: ; preds = %43, %42
%48 = load i8*, i8** %6
br label %51
49: ; preds = %2
%50 = load i32, i32* @EINVAL
store i32 %50, i32* %5
br label %51
51: ; preds = %49, %47
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [2 x i8] c"/\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @get_current_exec_path(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %29, label %4
4: ; preds = %2
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = load i32, i32* @PATH_MAX
%11 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %10)
%12 = icmp eq i64 %11, -1
br i1 %12, label %25, label %13
13: ; preds = %4
%14 = getelementptr inbounds i8, i8* %9, i64 %11
store i8 0, i8* %14
%15 = call i8* @dirname(i8* %9)
%16 = call i32 @strlen(i8* %15)
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = icmp ult i64 %18, %1
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = call i32 @strcpy(i8* %0, i8* %15)
%22 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %27
23: ; preds = %13
store i8 0, i8* %0
%24 = load i32, i32* @EINVAL
br label %27
25: ; preds = %4
store i8 0, i8* %0
%26 = load i32, i32* @errno
br label %27
27: ; preds = %20, %23, %25
%28 = phi i32 [ %26, %25 ], [ 0, %20 ], [ %24, %23 ]
br label %31
29: ; preds = %2
%30 = load i32, i32* @EINVAL
br label %31
31: ; preds = %29, %27
%32 = phi i32 [ %28, %27 ], [ %30, %29 ]
ret i32 %32
}