#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 *basename(char const *);
int /*<<< orphan*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ perror(char *);
char *dirname(const char *path) {
  const char *base = basename(path);
  char *rval;
  while (base > path && ((base[-1]) == '/'))
    base--;
  if (!(rval = malloc(base - path + 1))) {
    perror("malloc");
    exit(1);
  }
  memcpy(rval, path, base - path);
  rval[base - path] = '\0';
  return rval;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <basename@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
jbe    118d <dirname+0x3d>
mov    -0x10(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    119d <dirname+0x4d>
jmp    11ae <dirname+0x5e>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1169 <dirname+0x19>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e8 <dirname+0x98>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <basename@plt>
mov    %rax,%rcx
sub    %r14,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rcx,%rbx
cmp    %r14,%rax
jbe    1186 <dirname+0x36>
cmpb   $0x2f,-0x1(%rax)
lea    -0x1(%rax),%rax
lea    -0x1(%rbx),%rcx
je     1170 <dirname+0x20>
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b1 <dirname+0x61>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
define i8* @dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i8* @basename(i8* %5)
store i8* %6, i8** %3
br label %7
7: ; preds = %19, %1
%8 = load i8*, i8** %3
%9 = load i8*, i8** %2
%10 = icmp ugt i8* %8, %9
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 -1
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 47
br label %17
17: ; preds = %11, %7
%18 = phi i1 [ false, %7 ], [ %16, %11 ]
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i32 -1
store i8* %21, i8** %3
br label %7
22: ; preds = %17
%23 = load i8*, i8** %3
%24 = load i8*, i8** %2
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
%28 = add nsw i64 %27, 1
%29 = trunc i64 %28 to i32
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %4
%31 = icmp ne i8* %30, null
br i1 %31, label %35, label %32
32: ; preds = %22
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %22
%36 = load i8*, i8** %4
%37 = load i8*, i8** %2
%38 = load i8*, i8** %3
%39 = load i8*, i8** %2
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = trunc i64 %42 to i32
%44 = call i32 @memcpy(i8* %36, i8* %37, i32 %43)
%45 = load i8*, i8** %4
%46 = load i8*, i8** %3
%47 = load i8*, i8** %2
%48 = ptrtoint i8* %46 to i64
%49 = ptrtoint i8* %47 to i64
%50 = sub i64 %48, %49
%51 = getelementptr inbounds i8, i8* %45, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %4
ret i8* %52
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
define i8* @dirname(i8* %0) {
%2 = tail call i8* @basename(i8* %0)
br label %3
3: ; preds = %6, %1
%4 = phi i8* [ %2, %1 ], [ %7, %6 ]
%5 = icmp ugt i8* %4, %0
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %4, i64 -1
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 47
br i1 %9, label %3, label %10
10: ; preds = %3, %6
%11 = ptrtoint i8* %4 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = add i32 %14, 1
%16 = tail call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%22 = tail call i32 @memcpy(i8* %16, i8* %0, i32 %14)
%23 = getelementptr inbounds i8, i8* %16, i64 %13
store i8 0, i8* %23
ret i8* %16
}