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

/* 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/file", getpid());
  int err = readlink(proc, exe, maxLen - 1);
  exe[err > 0 ? err : 0] = '\0';
}


x86 assembly (clang, -O0):
<get_my_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x68(%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)
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpl   $0x0,-0x54(%rbp)
jle    119b <get_my_path+0x6b>
mov    -0x54(%rbp),%eax
mov    %eax,-0x6c(%rbp)
jmp    11a5 <get_my_path+0x75>
xor    %eax,%eax
mov    %eax,-0x6c(%rbp)
jmp    11a5 <get_my_path+0x75>
mov    -0x60(%rbp),%rax
mov    -0x6c(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x70,%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 = [14 x i8] c"/proc/%d/file\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 ([14 x i8], [14 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 i8*, i8** %3
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = load i32, i32* %6
br label %21
20: ; preds = %2
br label %21
21: ; preds = %20, %18
%22 = phi i32 [ %19, %18 ], [ 0, %20 ]
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %15, i64 %23
store i8 0, i8* %24
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/file\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 ([14 x i8], [14 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 inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
ret void
}