#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 readlink(char *, char *, int);
char *strrchr(char *, char);
char *get_program_path(char *buf, int count) {
  int i = 0;
  int retval = readlink("/proc/self/exe", buf, count - 1);
  if ((retval < 0 || retval >= count - 1)) {
    return NULL;
  }
  buf[retval] = '\0';
  char *end = strrchr(buf, '/');
  if (NULL == end)
    buf[0] = '\0';
  else
    *end = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<get_program_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
lea    0xeb9(%rip),%rdi
call   1030 <readlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     116a <get_program_path+0x4a>
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1177 <get_program_path+0x57>
movq   $0x0,-0x8(%rbp)
jmp    11bc <get_program_path+0x9c>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11ad <get_program_path+0x8d>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b4 <get_program_path+0x94>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_program_path>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0xffffffff,%ebp
lea    0xece(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <readlink@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
js     1168 <get_program_path+0x48>
cmp    %ebp,%ecx
jge    1168 <get_program_path+0x48>
mov    %ecx,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
cmove  %rbx,%rax
movb   $0x0,(%rax)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @get_program_path(i8* %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %3)
%5 = icmp sgt i32 %4, -1
%6 = icmp slt i32 %4, %3
%7 = and i1 %5, %6
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = zext i32 %4 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 0, i8* %10
%11 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%12 = icmp eq i8* %11, null
%13 = select i1 %12, i8* %0, i8* %11
store i8 0, i8* %13
br label %14
14: ; preds = %8, %2
%15 = phi i8* [ null, %2 ], [ %0, %8 ]
ret i8* %15
}