#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 */
scalar_t__ PATH_MAX;
int getpid();
int readlink(char *, char *, scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strrchr(char *, char);
void get_my_path(char *path) {
  char proc[64];
  char *x;
  sprintf(proc, "/proc/%d/exe", getpid());
  int err = readlink(proc, path, PATH_MAX - 1);
  if (err <= 0) {
    path[0] = 0;
  } else {
    path[err] = 0;
    x = strrchr(path, '/');
    if (x)
      x[1] = 0;
  }
}


x86 assembly (clang, -O0):
<get_my_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%edx
lea    0xe98(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2e74(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <readlink@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jg     11a1 <get_my_path+0x61>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    11d7 <get_my_path+0x97>
mov    -0x8(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     11d2 <get_my_path+0x92>
mov    -0x58(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11d7 <get_my_path+0x97>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_my_path>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xea8(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1060 <sprintf@plt>
mov    0x2e84(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
test   %eax,%eax
jle    11a1 <get_my_path+0x61>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11a4 <get_my_path+0x64>
mov    %rax,%rbx
add    $0x1,%rbx
movb   $0x0,(%rbx)
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%d/exe\00"
@PATH_MAX = global i64 0
define void @get_my_path(i8* %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i64, i64* @PATH_MAX
%7 = add nsw i64 %6, -1
%8 = call i32 @readlink(i8* %3, i8* %0, i64 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
%13 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %13, i64 1
br label %17
17: ; preds = %1, %15
%18 = phi i8* [ %16, %15 ], [ %0, %1 ]
store i8 0, i8* %18
br label %19
19: ; preds = %17, %10
ret void
}