#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 last_slash(char const *, int *, int *);
int nth_slash(char const *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int dirname(const char *path, char *dir) {
  int len, found = 0, slash_index = 0;
  slash_index = last_slash(path, &found, &len);
  if (found == 0)
    strcpy(dir, ".");
  else if (path[slash_index + 1] == 0 && found == 1)
    strcpy(dir, "/");
  else if (path[slash_index + 1] == 0 && found > 1) {
    slash_index = nth_slash(path, found - 1);
    strncpy(dir, path, slash_index);
    dir[slash_index] = 0;
  } else {
    strncpy(dir, path, slash_index);
    dir[slash_index] = 0;
  }
  return slash_index;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <last_slash@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1191 <dirname+0x51>
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1247 <dirname+0x107>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11ca <dirname+0x8a>
cmpl   $0x1,-0x18(%rbp)
jne    11ca <dirname+0x8a>
mov    -0x10(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1242 <dirname+0x102>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1221 <dirname+0xe1>
cmpl   $0x1,-0x18(%rbp)
jle    1221 <dirname+0xe1>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
sub    $0x1,%esi
call   1050 <nth_slash@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    123d <dirname+0xfd>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1242 <dirname+0x102>
jmp    1247 <dirname+0x107>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movl   $0x0,(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1060 <last_slash@plt>
mov    %eax,%ebp
mov    (%rsp),%esi
test   %esi,%esi
je     1184 <dirname+0x44>
movslq %ebp,%r15
mov    0x1(%r15,%rbx,1),%al
test   %al,%al
jne    11a0 <dirname+0x60>
cmp    $0x1,%esi
jne    11a0 <dirname+0x60>
lea    0xe80(%rip),%rsi
jmp    118b <dirname+0x4b>
lea    0xe75(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %al,%al
jne    11cd <dirname+0x8d>
cmp    $0x2,%esi
jl     11cd <dirname+0x8d>
add    $0xffffffff,%esi
mov    %rbx,%rdi
call   1050 <nth_slash@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
jmp    1193 <dirname+0x53>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%r14,%r15,1)
jmp    1193 <dirname+0x53>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @dirname(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load i8*, i8** %3
%9 = call i32 @last_slash(i8* %8, i32* %6, i32* %5)
store i32 %9, i32* %7
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @strcpy(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %66
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = load i32, i32* %7
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %15
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %4
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %65
30: ; preds = %24, %15
%31 = load i8*, i8** %3
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %55
39: ; preds = %30
%40 = load i32, i32* %6
%41 = icmp sgt i32 %40, 1
br i1 %41, label %42, label %55
42: ; preds = %39
%43 = load i8*, i8** %3
%44 = load i32, i32* %6
%45 = sub nsw i32 %44, 1
%46 = call i32 @nth_slash(i8* %43, i32 %45)
store i32 %46, i32* %7
%47 = load i8*, i8** %4
%48 = load i8*, i8** %3
%49 = load i32, i32* %7
%50 = call i32 @strncpy(i8* %47, i8* %48, i32 %49)
%51 = load i8*, i8** %4
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
br label %64
55: ; preds = %39, %30
%56 = load i8*, i8** %4
%57 = load i8*, i8** %3
%58 = load i32, i32* %7
%59 = call i32 @strncpy(i8* %56, i8* %57, i32 %58)
%60 = load i8*, i8** %4
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
br label %64
64: ; preds = %55, %42
br label %65
65: ; preds = %64, %27
br label %66
66: ; preds = %65, %12
%67 = load i32, i32* %7
ret i32 %67
}

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