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

/* Variables and functions */
int /*<<< orphan*/ readlink(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
char const *get_base_path(char const *argv0) {
  static char base_path[512];
  char *e;
  strncpy(base_path, argv0, sizeof(base_path));
  e = strrchr(base_path, '/');
  if (!e) {
    readlink("/proc/self/exe", base_path, sizeof(base_path));
    e = strrchr(base_path, '/');
  }
  if (e) {
    ;
    e[1] = 0;
  } else {
    ;
  }
  return base_path;
}


x86 assembly (clang, -O0):
<get_base_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0x2f09(%rip),%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    0x2ef8(%rip),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119e <get_base_path+0x6e>
lea    0xe88(%rip),%rdi
lea    0x2ed1(%rip),%rsi
mov    $0x200,%edx
call   1040 <readlink@plt>
lea    0x2ec0(%rip),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b6 <get_base_path+0x86>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11bb <get_base_path+0x8b>
jmp    11bb <get_base_path+0x8b>
lea    0x2e8e(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_base_path>:
push   %rbx
mov    %rdi,%rsi
lea    0x2f15(%rip),%rbx
mov    %rbx,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
jne    1187 <get_base_path+0x57>
lea    0xe9f(%rip),%rdi
lea    0x2ee8(%rip),%rbx
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <readlink@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     118b <get_base_path+0x5b>
movb   $0x0,0x1(%rax)
lea    0x2ebe(%rip),%rax
pop    %rbx
ret

LLVM IR (-O0):
@get_base_path.base_path = internal global [512 x i8] zeroinitializer
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @get_base_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @strncpy(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0), i8* %4, i32 512)
%6 = call i8* @strrchr(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0), i8 signext 47)
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0), i32 512)
%11 = call i8* @strrchr(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0), i8 signext 47)
store i8* %11, i8** %3
br label %12
12: ; preds = %9, %1
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8 0, i8* %17
br label %19
18: ; preds = %12
br label %19
19: ; preds = %18, %15
ret i8* getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0)
}

LLVM IR (-O2):
@get_base_path.base_path = internal global [512 x i8] zeroinitializer
@.str = [15 x i8] c"/proc/self/exe\00"
define i8* @get_base_path(i8* %0) {
%2 = tail call i32 @strncpy(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0), i8* %0, i32 512)
%3 = tail call i8* @strrchr(i8* dereferenceable(1) getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0), i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0), i32 512)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0), i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1, %5
%10 = phi i8* [ %7, %5 ], [ %3, %1 ]
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %11
br label %12
12: ; preds = %5, %9
ret i8* getelementptr inbounds ([512 x i8], [512 x i8]* @get_base_path.base_path, i64 0, i64 0)
}