#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *posix_get_process_name(char const *);
int readlink(char *, char *, int);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
char *get_current_process_name(const char *argv0) {
  char buf[PATH_MAX + 1];
  (void)argv0;
  memset(buf, 0, sizeof(buf));
  if (readlink("/proc/self/exe", buf, sizeof(buf)) == -1)
    return 0;
  if (strcmp(buf, "") != 0)
    return strdup(buf);
  return posix_get_process_name(argv0);
}


x86 assembly (clang, -O0):
<get_current_process_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x30(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
lea    0xe57(%rip),%rdi
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
jne    11cb <get_current_process_name+0x7b>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1212 <get_current_process_name+0xc2>
mov    -0x38(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11fe <get_current_process_name+0xae>
mov    -0x38(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1212 <get_current_process_name+0xc2>
mov    -0x10(%rbp),%rdi
call   1030 <posix_get_process_name@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_process_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2e8b(%rip),%rax
mov    (%rax),%ebx
add    $0x1,%ebx
mov    %rsp,%r12
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1050 <memset@plt>
lea    0xe6f(%rip),%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11c9 <get_current_process_name+0x79>
lea    0xe67(%rip),%rsi
mov    %r12,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11be <get_current_process_name+0x6e>
mov    %r12,%rdi
call   1070 <strdup@plt>
jmp    11c6 <get_current_process_name+0x76>
mov    %r14,%rdi
call   1030 <posix_get_process_name@plt>
mov    %rax,%r15
mov    %r15,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @get_current_process_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @PATH_MAX
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i8*, i8** %3
%13 = trunc i64 %9 to i32
%14 = call i32 @memset(i8* %11, i32 0, i32 %13)
%15 = trunc i64 %9 to i32
%16 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %6
br label %27
19: ; preds = %1
%20 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i8* @strdup(i8* %11)
store i8* %23, i8** %2
store i32 1, i32* %6
br label %27
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = call i8* @posix_get_process_name(i8* %25)
store i8* %26, i8** %2
store i32 1, i32* %6
br label %27
27: ; preds = %24, %22, %18
%28 = load i8*, i8** %4
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @get_current_process_name(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @memset(i8* %5, i32 0, i32 %3)
%7 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %16, label %9
9: ; preds = %1
%10 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i8* @strdup(i8* %5)
br label %16
14: ; preds = %9
%15 = call i8* @posix_get_process_name(i8* %0)
br label %16
16: ; preds = %1, %14, %12
%17 = phi i8* [ %13, %12 ], [ %15, %14 ], [ null, %1 ]
ret i8* %17
}