#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 *malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strrchr(char const *, char);
char *get_parent_dir(const char *file) {
  char *r, *c = strrchr(file, '/');
  if (c == NULL) {
    r = ".";
  } else if (c == file) {
    r = "/";
  } else {
    r = malloc(strlen(file) + 1);
    strcpy(r, file);
    c = strrchr(r, '/');
    *c = '\0';
  }
  return r;
}


x86 assembly (clang, -O0):
<get_parent_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1179 <get_parent_dir+0x39>
lea    0xe90(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11db <get_parent_dir+0x9b>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1197 <get_parent_dir+0x57>
lea    0xe74(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11d6 <get_parent_dir+0x96>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11db <get_parent_dir+0x9b>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_parent_dir>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     118c <get_parent_dir+0x4c>
cmp    %rbx,%rax
je     1195 <get_parent_dir+0x55>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
movb   $0x0,(%rax)
jmp    119c <get_parent_dir+0x5c>
lea    0xe6d(%rip),%r14
jmp    119c <get_parent_dir+0x5c>
lea    0xe66(%rip),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @get_parent_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i8* @strrchr(i8* %5, i8 signext 47)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3
br label %27
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = load i8*, i8** %2
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %26
15: ; preds = %10
%16 = load i8*, i8** %2
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %3
%20 = load i8*, i8** %3
%21 = load i8*, i8** %2
%22 = call i32 @strcpy(i8* %20, i8* %21)
%23 = load i8*, i8** %3
%24 = call i8* @strrchr(i8* %23, i8 signext 47)
store i8* %24, i8** %4
%25 = load i8*, i8** %4
store i8 0, i8* %25
br label %26
26: ; preds = %15, %14
br label %27
27: ; preds = %26, %9
%28 = load i8*, i8** %3
ret i8* %28
}

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