#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 */
char *absolute_path(char *);
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
char *strrchr(char *, char);
char *get_exec_path(const char *argv0) {
  char *p = strdup(argv0);
  char *e = strrchr(p, '/');
  if (e)
    *e = 0;
  else {
    free(p);
    p = strdup(".");
  }
  char *argv0_path = absolute_path(p);
  free(p);
  return argv0_path;
}


x86 assembly (clang, -O0):
<get_exec_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1182 <get_exec_path+0x42>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    119b <get_exec_path+0x5b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xe6e(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <absolute_path@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_exec_path>:
push   %r14
push   %rbx
push   %rax
call   1060 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1163 <get_exec_path+0x23>
movb   $0x0,(%rax)
jmp    117a <get_exec_path+0x3a>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe8e(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,%rbx
mov    %rbx,%rdi
call   1050 <absolute_path@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i8* @get_exec_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @strdup(i8* %6)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i8*, i8** %4
store i8 0, i8* %13
br label %18
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32 @free(i8* %15)
%17 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %3
br label %18
18: ; preds = %14, %12
%19 = load i8*, i8** %3
%20 = call i8* @absolute_path(i8* %19)
store i8* %20, i8** %5
%21 = load i8*, i8** %3
%22 = call i32 @free(i8* %21)
%23 = load i8*, i8** %5
ret i8* %23
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i8* @get_exec_path(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %6, label %5
5: ; preds = %1
store i8 0, i8* %3
br label %9
6: ; preds = %1
%7 = tail call i32 @free(i8* %2)
%8 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %6, %5
%10 = phi i8* [ %2, %5 ], [ %8, %6 ]
%11 = tail call i8* @absolute_path(i8* %10)
%12 = tail call i32 @free(i8* %10)
ret i8* %11
}