#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 *path_final_slash(char *);
char *strcpy(char *, char *);
char *dirname(char *path) {
  char *sl;
  if (!path || *path == '\0') {
    return ".";
  }
  sl = path_final_slash(path);
  if (!sl) {
    if (!*path)
      return strcpy(path, "/");
    return strcpy(path, ".");
  }
  if (path == sl) {
    *(sl + 1) = '\0';
  } else {
    *sl = '\0';
  }
  return path;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1147 <dirname+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1157 <dirname+0x37>
lea    0xeb2(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11d8 <dirname+0xb8>
mov    -0x10(%rbp),%rdi
call   1040 <path_final_slash@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <dirname+0x8e>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1195 <dirname+0x75>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
jmp    11d8 <dirname+0xb8>
mov    -0x10(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
jmp    11d8 <dirname+0xb8>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11c9 <dirname+0xa9>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11d0 <dirname+0xb0>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %rbx
lea    0xee8(%rip),%rax
test   %rdi,%rdi
je     1156 <dirname+0x46>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1156 <dirname+0x46>
mov    %rbx,%rdi
call   1030 <path_final_slash@plt>
test   %rax,%rax
je     113c <dirname+0x2c>
cmp    %rbx,%rax
je     1148 <dirname+0x38>
movb   $0x0,(%rax)
jmp    1153 <dirname+0x43>
cmpb   $0x0,(%rbx)
je     114e <dirname+0x3e>
movw   $0x2e,(%rbx)
jmp    1153 <dirname+0x43>
movb   $0x0,0x1(%rbx)
jmp    1153 <dirname+0x43>
movw   $0x2f,(%rbx)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %7, %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %39
13: ; preds = %7
%14 = load i8*, i8** %3
%15 = call i8* @path_final_slash(i8* %14)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %28, label %18
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = call i8* @strcpy(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %2
br label %39
25: ; preds = %18
%26 = load i8*, i8** %3
%27 = call i8* @strcpy(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %27, i8** %2
br label %39
28: ; preds = %13
%29 = load i8*, i8** %3
%30 = load i8*, i8** %4
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 0, i8* %34
br label %37
35: ; preds = %28
%36 = load i8*, i8** %4
store i8 0, i8* %36
br label %37
37: ; preds = %35, %32
%38 = load i8*, i8** %3
store i8* %38, i8** %2
br label %39
39: ; preds = %37, %25, %22, %12
%40 = load i8*, i8** %2
ret i8* %40
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i8* @dirname(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %20, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = tail call i8* @path_final_slash(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
%12 = bitcast i8* %0 to i16*
br i1 %11, label %13, label %14
13: ; preds = %9
store i16 47, i16* %12
br label %20
14: ; preds = %9
store i16 46, i16* %12
br label %20
15: ; preds = %6
%16 = icmp eq i8* %7, %0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = getelementptr inbounds i8, i8* %0, i64 1
store i8 0, i8* %18
br label %20
19: ; preds = %15
store i8 0, i8* %7
br label %20
20: ; preds = %17, %19, %1, %3, %14, %13
%21 = phi i8* [ %0, %14 ], [ %0, %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %1 ], [ %0, %19 ], [ %0, %17 ]
ret i8* %21
}