#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int getpid();
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
char *strrchr(char *, char);
char *fb_hGetExePath(char *dst, int maxlen) {
  char *p;
  char linkname[1024];
  struct stat finfo;
  int len;
  sprintf(linkname, "/proc/%d/exe", getpid());
  if ((stat(linkname, &finfo) == 0) &&
      ((len = readlink(linkname, dst, maxlen - 1)) > -1)) {
    dst[len] = '\0';
    p = strrchr(dst, '/');
    if (p == dst)
      *(p + 1) = '\0';
    else if (p)
      *p = '\0';
    else
      dst[0] = '\0';
  } else {
    p = 0;
  }
  return p;
}


x86 assembly (clang, -O0):
<fb_hGetExePath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%edx
lea    0xe79(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1232 <fb_hGetExePath+0xe2>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1030 <readlink@plt>
mov    %eax,-0x42c(%rbp)
cmp    $0xffffffff,%eax
jle    1232 <fb_hGetExePath+0xe2>
mov    -0x8(%rbp),%rax
movslq -0x42c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    120a <fb_hGetExePath+0xba>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    122d <fb_hGetExePath+0xdd>
cmpq   $0x0,-0x18(%rbp)
je     1221 <fb_hGetExePath+0xd1>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    1228 <fb_hGetExePath+0xd8>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    122d <fb_hGetExePath+0xdd>
jmp    123a <fb_hGetExePath+0xea>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fb_hGetExePath>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8d(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    11df <fb_hGetExePath+0x8f>
add    $0xffffffff,%r15d
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <readlink@plt>
test   %eax,%eax
js     11dd <fb_hGetExePath+0x8d>
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,%rbx
lea    0x1(%r14),%rax
test   %rbx,%rbx
mov    %rbx,%rcx
cmove  %r14,%rcx
cmp    %r14,%rbx
cmove  %rax,%rcx
movb   $0x0,(%rcx)
jmp    11df <fb_hGetExePath+0x8f>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [13 x i8] c"/proc/%d/exe\00"
define i8* @fb_hGetExePath(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = call i32 (...) @getpid()
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%13 = call i64 @stat(i8* %12, %struct.stat* %7)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %45
15: ; preds = %2
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 1
%20 = call i32 @readlink(i8* %16, i8* %17, i32 %19)
store i32 %20, i32* %8
%21 = icmp sgt i32 %20, -1
br i1 %21, label %22, label %45
22: ; preds = %15
%23 = load i8*, i8** %3
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %3
%28 = call i8* @strrchr(i8* %27, i8 signext 47)
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = load i8*, i8** %3
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 0, i8* %34
br label %44
35: ; preds = %22
%36 = load i8*, i8** %5
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i8*, i8** %5
store i8 0, i8* %39
br label %43
40: ; preds = %35
%41 = load i8*, i8** %3
%42 = getelementptr inbounds i8, i8* %41, i64 0
store i8 0, i8* %42
br label %43
43: ; preds = %40, %38
br label %44
44: ; preds = %43, %32
br label %46
45: ; preds = %15, %2
store i8* null, i8** %5
br label %46
46: ; preds = %45, %44
%47 = load i8*, i8** %5
ret i8* %47
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [13 x i8] c"/proc/%d/exe\00"
define i8* @fb_hGetExePath(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = tail call i32 (...) @getpid()
%8 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i64 @stat(i8* %5, %struct.stat* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %2
%12 = add nsw i32 %1, -1
%13 = call i32 @readlink(i8* %5, i8* %0, i32 %12)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = zext i32 %13 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
%18 = call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%19 = icmp eq i8* %18, %0
%20 = getelementptr inbounds i8, i8* %0, i64 1
%21 = icmp eq i8* %18, null
%22 = select i1 %21, i8* %0, i8* %18
%23 = select i1 %19, i8* %20, i8* %22
store i8 0, i8* %23
br label %24
24: ; preds = %15, %2, %11
%25 = phi i8* [ null, %11 ], [ null, %2 ], [ %18, %15 ]
ret i8* %25
}