#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 */
int find_last_path_separator(char const *);
scalar_t__ safe_malloc(int);
char *strdup(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *path_get_dirname(const char *path) {
  char *result;
  int i;
  i = find_last_path_separator(path);
  if (i == -1)
    return strdup(".");
  if (i == 0)
    return strdup("/");
  result = (char *)safe_malloc(i + 1);
  strncpy(result, path, i);
  result[i] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<path_get_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <find_last_path_separator@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1177 <path_get_dirname+0x37>
lea    0xe97(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11c9 <path_get_dirname+0x89>
cmpl   $0x0,-0x1c(%rbp)
jne    1196 <path_get_dirname+0x56>
lea    0xe7a(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11c9 <path_get_dirname+0x89>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1040 <safe_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_get_dirname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1050 <find_last_path_separator@plt>
test   %eax,%eax
je     1161 <path_get_dirname+0x21>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1172 <path_get_dirname+0x32>
lea    0xea1(%rip),%rdi
jmp    1168 <path_get_dirname+0x28>
lea    0xe9a(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strdup@plt>
lea    0x1(%rbx),%edi
call   1040 <safe_malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @path_get_dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @find_last_path_separator(i8* %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %2
br label %31
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %2
br label %31
17: ; preds = %12
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
%20 = call i64 @safe_malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = call i32 @strncpy(i8* %22, i8* %23, i32 %24)
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %4
store i8* %30, i8** %2
br label %31
31: ; preds = %17, %15, %10
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @path_get_dirname(i8* %0) {
%2 = tail call i32 @find_last_path_separator(i8* %0)
switch i32 %2, label %7 [
i32 -1, label %3
i32 0, label %5
]
3: ; preds = %1
%4 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %14
5: ; preds = %1
%6 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %14
7: ; preds = %1
%8 = add nuw nsw i32 %2, 1
%9 = tail call i64 @safe_malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @strncpy(i8* %10, i8* %0, i32 %2)
%12 = sext i32 %2 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %7, %5, %3
%15 = phi i8* [ %4, %3 ], [ %6, %5 ], [ %10, %7 ]
ret i8* %15
}