#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 /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char const *);
int strlen(char const *);
char *dirname(const char *path) {
  char *res = strdup(path);
  char *lastchar;
  strcpy(res, path);
  lastchar = res + strlen(path);
  while (*lastchar != '/' && lastchar != res) {
    --lastchar;
  }
  *lastchar = 0;
  if (lastchar == path) {
    free(res);
    return 0;
  }
  return res;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x29(%rbp)
je     11aa <dirname+0x6a>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11ba <dirname+0x7a>
jmp    11cb <dirname+0x8b>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1187 <dirname+0x47>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11f6 <dirname+0xb6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fe <dirname+0xbe>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1060 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rax,%r14,1),%rcx
add    $0x1,%rcx
nopl   0x0(%rax)
lea    -0x1(%rcx),%rdx
test   %rax,%rax
je     1186 <dirname+0x46>
add    $0xffffffffffffffff,%rax
cmpb   $0x2f,-0x1(%rcx)
mov    %rdx,%rcx
jne    1170 <dirname+0x30>
movb   $0x0,(%rdx)
cmp    %rbx,%rdx
jne    1199 <dirname+0x59>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%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 i8* @strdup(i8* %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = load i8*, i8** %3
%10 = call i32 @strcpy(i8* %8, i8* %9)
%11 = load i8*, i8** %4
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
store i8* %15, i8** %5
br label %16
16: ; preds = %27, %1
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 47
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = load i8*, i8** %4
%24 = icmp ne i8* %22, %23
br label %25
25: ; preds = %21, %16
%26 = phi i1 [ false, %16 ], [ %24, %21 ]
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 -1
store i8* %29, i8** %5
br label %16
30: ; preds = %25
%31 = load i8*, i8** %5
store i8 0, i8* %31
%32 = load i8*, i8** %5
%33 = load i8*, i8** %3
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i8*, i8** %4
%37 = call i32 @free(i8* %36)
store i8* null, i8** %2
br label %40
38: ; preds = %30
%39 = load i8*, i8** %4
store i8* %39, i8** %2
br label %40
40: ; preds = %38, %35
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
define i8* @dirname(i8* %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i32 @strcpy(i8* %2, i8* %0)
%4 = tail call i32 @strlen(i8* %0)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %2, i64 %5
br label %7
7: ; preds = %7, %1
%8 = phi i8* [ %6, %1 ], [ %13, %7 ]
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 47
%11 = icmp ne i8* %8, %2
%12 = and i1 %11, %10
%13 = getelementptr inbounds i8, i8* %8, i64 -1
br i1 %12, label %7, label %14
14: ; preds = %7
store i8 0, i8* %8
%15 = icmp eq i8* %8, %0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 @free(i8* %2)
br label %18
18: ; preds = %14, %16
%19 = phi i8* [ null, %16 ], [ %2, %14 ]
ret i8* %19
}