#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 getpid();
int readlink(char *, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
void get_my_path(char *exe, size_t maxLen) {
  char proc[64];
  snprintf(proc, sizeof proc, "/proc/%d/exe", getpid());
  int err = readlink(proc, exe, maxLen - 1);
  if (err > 0) {
    exe[err] = '\0';
  } else {
    exe[0] = '\0';
  }
}


x86 assembly (clang, -O0):
<get_my_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xe9f(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <readlink@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jle    1199 <get_my_path+0x69>
mov    -0x8(%rbp),%rax
movslq -0x54(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a0 <get_my_path+0x70>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_my_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %r12d,%r12d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xeae(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
add    $0xffffffffffffffff,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <readlink@plt>
test   %eax,%eax
cmovns %eax,%r12d
movb   $0x0,(%r14,%r12,1)
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%d/exe\00"
define void @get_my_path(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [64 x i8]
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = call i32 (...) @getpid()
%9 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%11 = load i8*, i8** %3
%12 = load i64, i64* %4
%13 = sub i64 %12, 1
%14 = call i32 @readlink(i8* %10, i8* %11, i64 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %25
22: ; preds = %2
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 0
store i8 0, i8* %24
br label %25
25: ; preds = %22, %17
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%d/exe\00"
define void @get_my_path(i8* nocapture %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 i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = add i64 %1, -1
%8 = call i32 @readlink(i8* %4, i8* %0, i64 %7)
%9 = icmp sgt i32 %8, 0
%10 = select i1 %9, i32 %8, i32 0
%11 = zext i32 %10 to i64
%12 = getelementptr i8, i8* %0, i64 %11
store i8 0, i8* %12
ret void
}