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


x86 assembly (clang, -O0):
<get_program_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
lea    0xec0(%rip),%rdi
call   1030 <readlink@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1163 <get_program_name+0x43>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1170 <get_program_name+0x50>
movq   $0x0,-0x8(%rbp)
jmp    118e <get_program_name+0x6e>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_program_name>:
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>
test   %eax,%eax
js     115d <get_program_name+0x3d>
cmp    %ebp,%eax
jge    115d <get_program_name+0x3d>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <strrchr@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @get_program_name(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 %12
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)
br label %12
12: ; preds = %2, %8
%13 = phi i8* [ %11, %8 ], [ null, %2 ]
ret i8* %13
}