#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_hGetExeName(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 != 0)
      ++p;
    else
      p = dst;
  } else {
    p = 0;
  }
  return p;
}


x86 assembly (clang, -O0):
<fb_hGetExeName>:
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    1218 <fb_hGetExeName+0xc8>
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    1218 <fb_hGetExeName+0xc8>
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)
cmpq   $0x0,-0x18(%rbp)
je     120b <fb_hGetExeName+0xbb>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1213 <fb_hGetExeName+0xc3>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1220 <fb_hGetExeName+0xd0>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fb_hGetExeName>:
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    11cf <fb_hGetExeName+0x7f>
add    $0xffffffff,%r15d
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <readlink@plt>
test   %eax,%eax
js     11cd <fb_hGetExeName+0x7d>
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %r14,%rbx
jmp    11cf <fb_hGetExeName+0x7f>
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_hGetExeName(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 %37
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 %37
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 = icmp ne i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %5
br label %36
34: ; preds = %22
%35 = load i8*, i8** %3
store i8* %35, i8** %5
br label %36
36: ; preds = %34, %31
br label %38
37: ; preds = %15, %2
store i8* null, i8** %5
br label %38
38: ; preds = %37, %36
%39 = load i8*, i8** %5
ret i8* %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [13 x i8] c"/proc/%d/exe\00"
define i8* @fb_hGetExeName(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 %22
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 %22
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, null
%20 = getelementptr inbounds i8, i8* %18, i64 1
%21 = select i1 %19, i8* %0, i8* %20
br label %22
22: ; preds = %15, %2, %11
%23 = phi i8* [ null, %11 ], [ null, %2 ], [ %21, %15 ]
ret i8* %23
}