#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*/ dir;

/* Variables and functions */
char *FN_make_path(char *, char const *);
char *FN_normalize(char *);
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ getcwd(char *, int);
char *posix_search_in_path(char const *);
char *strdup(char const *);
char *posix_get_process_name(const char *argv) {
  char dir[PATH_MAX + 1], *path, *ret;
  if (argv[0] == '/')
    return strdup(argv);
  if (getcwd(dir, sizeof(dir)) == 0)
    return 0;
  path = FN_make_path(dir, argv);
  if (!access(path, R_OK)) {
    ret = FN_normalize(path);
    free(path);
    return ret;
  }
  free(path);
  return posix_search_in_path(argv);
}


x86 assembly (clang, -O0):
<posix_get_process_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11d8 <posix_get_process_name+0x68>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1278 <posix_get_process_name+0x108>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%rax
jne    1205 <posix_get_process_name+0x95>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1278 <posix_get_process_name+0x108>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <FN_make_path@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%eax
jne    125b <posix_get_process_name+0xeb>
mov    -0x28(%rbp),%rdi
call   1050 <FN_normalize@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1278 <posix_get_process_name+0x108>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <posix_search_in_path@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_get_process_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmpb   $0x2f,(%rdi)
jne    11b1 <posix_get_process_name+0x41>
mov    %r15,%rdi
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <strdup@plt>
mov    %rbx,%rdi
call   1060 <getcwd@plt>
test   %rax,%rax
je     11f6 <posix_get_process_name+0x86>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <FN_make_path@plt>
mov    %rax,%r14
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <access@plt>
test   %eax,%eax
je     11fa <posix_get_process_name+0x8a>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   1030 <posix_search_in_path@plt>
mov    %rax,%rbx
jmp    120d <posix_get_process_name+0x9d>
xor    %ebx,%ebx
jmp    120d <posix_get_process_name+0x9d>
mov    %r14,%rdi
call   1050 <FN_normalize@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@R_OK = global i32 0
define i8* @posix_get_process_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @PATH_MAX
%10 = add nsw i32 %9, 1
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 47
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i8*, i8** %3
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %2
store i32 1, i32* %8
br label %45
22: ; preds = %1
%23 = trunc i64 %11 to i32
%24 = call i64 @getcwd(i8* %13, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i8* null, i8** %2
store i32 1, i32* %8
br label %45
27: ; preds = %22
%28 = load i8*, i8** %3
%29 = call i8* @FN_make_path(i8* %13, i8* %28)
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = load i32, i32* @R_OK
%32 = call i32 @access(i8* %30, i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %27
%35 = load i8*, i8** %6
%36 = call i8* @FN_normalize(i8* %35)
store i8* %36, i8** %7
%37 = load i8*, i8** %6
%38 = call i32 @free(i8* %37)
%39 = load i8*, i8** %7
store i8* %39, i8** %2
store i32 1, i32* %8
br label %45
40: ; preds = %27
%41 = load i8*, i8** %6
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** %3
%44 = call i8* @posix_search_in_path(i8* %43)
store i8* %44, i8** %2
store i32 1, i32* %8
br label %45
45: ; preds = %40, %34, %26, %19
%46 = load i8*, i8** %4
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@R_OK = global i32 0
define i8* @posix_get_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 = load i8, i8* %0
%7 = icmp eq i8 %6, 47
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i8* @strdup(i8* %0)
br label %24
10: ; preds = %1
%11 = call i64 @getcwd(i8* %5, i32 %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %10
%14 = call i8* @FN_make_path(i8* %5, i8* %0)
%15 = load i32, i32* @R_OK
%16 = call i32 @access(i8* %14, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i8* @FN_normalize(i8* %14)
%20 = call i32 @free(i8* %14)
br label %24
21: ; preds = %13
%22 = call i32 @free(i8* %14)
%23 = call i8* @posix_search_in_path(i8* %0)
br label %24
24: ; preds = %10, %21, %18, %8
%25 = phi i8* [ %9, %8 ], [ %23, %21 ], [ %19, %18 ], [ null, %10 ]
ret i8* %25
}