#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 *);
char *malloc(int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *dirname(const char *path) {
  char *newpath;
  const char *slash;
  int length;
  slash = basename(path);
  if (slash == path) {
    path = ".";
    length = 1;
  } else {
    while (*--slash == '/' && slash > path)
      ;
    length = slash - path + 1;
  }
  newpath = malloc(length + 1);
  if (newpath == 0)
    return 0;
  strncpy(newpath, path, length);
  newpath[length] = 0;
  return newpath;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <basename@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    116e <dirname+0x3e>
lea    0xea2(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11c9 <dirname+0x99>
jmp    1173 <dirname+0x43>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movsbl -0x1(%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x25(%rbp)
jne    11a2 <dirname+0x72>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
seta   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11b2 <dirname+0x82>
jmp    11b7 <dirname+0x87>
jmp    1173 <dirname+0x43>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <dirname+0xc0>
movq   $0x0,-0x8(%rbp)
jmp    1214 <dirname+0xe4>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <basename@plt>
cmp    %r14,%rax
je     1166 <dirname+0x36>
mov    %eax,%ebx
sub    %r14d,%ebx
add    $0x1,%ebx
add    $0xffffffffffffffff,%rax
xchg   %ax,%ax
add    $0xffffffff,%ebx
cmpb   $0x2f,(%rax)
jne    1172 <dirname+0x42>
lea    -0x1(%rax),%rcx
cmp    %r14,%rax
mov    %rcx,%rax
ja     1150 <dirname+0x20>
jmp    1172 <dirname+0x42>
mov    $0x1,%ebx
lea    0xe8e(%rip),%r14
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1197 <dirname+0x67>
mov    %r15,%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"
define i8* @dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @basename(i8* %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = load i8*, i8** %3
%11 = icmp eq i8* %9, %10
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3
store i32 1, i32* %6
br label %35
13: ; preds = %1
br label %14
14: ; preds = %26, %13
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i32 -1
store i8* %16, i8** %5
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i8*, i8** %5
%22 = load i8*, i8** %3
%23 = icmp ugt i8* %21, %22
br label %24
24: ; preds = %20, %14
%25 = phi i1 [ false, %14 ], [ %23, %20 ]
br i1 %25, label %26, label %27
26: ; preds = %24
br label %14
27: ; preds = %24
%28 = load i8*, i8** %5
%29 = load i8*, i8** %3
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = add nsw i64 %32, 1
%34 = trunc i64 %33 to i32
store i32 %34, i32* %6
br label %35
35: ; preds = %27, %12
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
%38 = call i8* @malloc(i32 %37)
store i8* %38, i8** %4
%39 = load i8*, i8** %4
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %42
41: ; preds = %35
store i8* null, i8** %2
br label %52
42: ; preds = %35
%43 = load i8*, i8** %4
%44 = load i8*, i8** %3
%45 = load i32, i32* %6
%46 = call i32 @strncpy(i8* %43, i8* %44, i32 %45)
%47 = load i8*, i8** %4
%48 = load i32, i32* %6
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %4
store i8* %51, i8** %2
br label %52
52: ; preds = %42, %41
%53 = load i8*, i8** %2
ret i8* %53
}

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