#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 */
scalar_t__ is_directory(char *);
char *malloc(int);
char *rindex(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *dirname(char *path) {
  if (is_directory(path)) {
    return path;
  } else {
    char *tmp = malloc(strlen(path) * (sizeof(char)));
    strcpy(tmp, path);
    char *ptr = rindex(tmp, '/');
    ptr[1] = '\0';
    return dirname(tmp);
  }
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <is_directory@plt>
cmp    $0x0,%rax
je     118c <dirname+0x2c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11da <dirname+0x7a>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <rindex@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <dirname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1070 <is_directory@plt>
test   %rax,%rax
je     1170 <dirname+0x20>
mov    %r14,%rbx
jmp    11ae <dirname+0x5e>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <rindex@plt>
movb   $0x0,0x1(%rax)
mov    %rbx,%rdi
call   1070 <is_directory@plt>
mov    %rbx,%r14
test   %rax,%rax
je     1170 <dirname+0x20>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @is_directory(i8* %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i8*, i8** %3
store i8* %10, i8** %2
br label %27
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = sext i32 %13 to i64
%15 = mul i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = load i8*, i8** %3
%20 = call i32 @strcpy(i8* %18, i8* %19)
%21 = load i8*, i8** %4
%22 = call i8* @rindex(i8* %21, i8 signext 47)
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8 0, i8* %24
%25 = load i8*, i8** %4
%26 = call i8* @dirname(i8* %25)
store i8* %26, i8** %2
br label %27
27: ; preds = %11, %9
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
define i8* @dirname(i8* %0) {
%2 = tail call i64 @is_directory(i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %13
4: ; preds = %1, %4
%5 = phi i8* [ %7, %4 ], [ %0, %1 ]
%6 = tail call i32 @strlen(i8* %5)
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call i32 @strcpy(i8* %7, i8* %5)
%9 = tail call i8* @rindex(i8* %7, i8 signext 47)
%10 = getelementptr inbounds i8, i8* %9, i64 1
store i8 0, i8* %10
%11 = tail call i64 @is_directory(i8* %7)
%12 = icmp eq i64 %11, 0
br i1 %12, label %4, label %13
13: ; preds = %4, %1
%14 = phi i8* [ %0, %1 ], [ %7, %4 ]
ret i8* %14
}