#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 *dir_name(char *);
char *executable_path();
int /*<<< orphan*/ free(char *);
int strlen(char *);
char *root_from_binary() {
  char *bin = executable_path();
  char *bin_dir = dir_name(bin);
  free(bin);
  if (bin_dir[strlen(bin_dir) - 1] == '/') {
    bin_dir[strlen(bin_dir) - 1] = '\0';
  }
  char *root_dir = dir_name(bin_dir);
  free(bin_dir);
  return root_dir;
}


x86 assembly (clang, -O0):
<root_from_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1040 <executable_path@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <dir_name@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11b4 <root_from_binary+0x74>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1060 <dir_name@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<root_from_binary>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <executable_path@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <dir_name@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    1181 <root_from_binary+0x41>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1060 <dir_name@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):
define i8* @root_from_binary() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = call i8* (...) @executable_path()
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = call i8* @dir_name(i8* %5)
store i8* %6, i8** %2
%7 = load i8*, i8** %1
%8 = call i32 @free(i8* %7)
%9 = load i8*, i8** %2
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %9, i64 %13
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 47
br i1 %17, label %18, label %25
18: ; preds = %0
%19 = load i8*, i8** %2
%20 = load i8*, i8** %2
%21 = call i32 @strlen(i8* %20)
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %19, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %18, %0
%26 = load i8*, i8** %2
%27 = call i8* @dir_name(i8* %26)
store i8* %27, i8** %3
%28 = load i8*, i8** %2
%29 = call i32 @free(i8* %28)
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
define i8* @root_from_binary() {
%1 = tail call i8* (...) @executable_path()
%2 = tail call i8* @dir_name(i8* %1)
%3 = tail call i32 @free(i8* %1)
%4 = tail call i32 @strlen(i8* %2)
%5 = add nsw i32 %4, -1
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %2, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 47
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = tail call i32 @strlen(i8* %2)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %2, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %10, %0
%16 = tail call i8* @dir_name(i8* %2)
%17 = tail call i32 @free(i8* %2)
ret i8* %16
}