#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void dirname(char *name) {
  int index = strlen(name) - 1;
  if ((strcmp(name, "/") == 0) || (strcmp(name, "//") == 0)) {
    fprintf(stdout, "/\n");
    return;
  }
  while (index && (name[index] == '/')) {
    name[index] = '\0';
    index--;
  }
  while ((name[index] != '/') && (index > 0))
    index--;
  if (!index) {
    strcpy(name, ".");
    strcat(name, "\0");
  } else {
    name[index] = '\0';
  }
  fprintf(stdout, "%s\n", name);
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     119f <dirname+0x4f>
mov    -0x8(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11bb <dirname+0x6b>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ab <dirname+0x15b>
jmp    11c0 <dirname+0x70>
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
je     11e4 <dirname+0x94>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    11f4 <dirname+0xa4>
jmp    120e <dirname+0xbe>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    11c0 <dirname+0x70>
jmp    1213 <dirname+0xc3>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0xe(%rbp)
je     1237 <dirname+0xe7>
cmpl   $0x0,-0xc(%rbp)
setg   %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
test   $0x1,%al
jne    1247 <dirname+0xf7>
jmp    1255 <dirname+0x105>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1213 <dirname+0xc3>
cmpl   $0x0,-0xc(%rbp)
jne    1284 <dirname+0x134>
mov    -0x8(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   1070 <strcat@plt>
jmp    1290 <dirname+0x140>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd64(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11d0 <dirname+0x80>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11d0 <dirname+0x80>
add    $0xffffffff,%ebp
je     11a2 <dirname+0x52>
movslq %ebp,%rax
nopl   (%rax)
cmpb   $0x2f,(%rbx,%rax,1)
jne    11a4 <dirname+0x54>
movb   $0x0,(%rbx,%rax,1)
add    $0xffffffffffffffff,%rax
test   %eax,%eax
jne    1190 <dirname+0x40>
xor    %eax,%eax
movslq %eax,%rcx
lea    0x1(%rcx),%eax
nopw   0x0(%rax,%rax,1)
lea    -0x1(%rcx),%rdx
add    $0xffffffff,%eax
cmpb   $0x2f,(%rbx,%rcx,1)
je     11c5 <dirname+0x75>
test   %rcx,%rcx
mov    %rdx,%rcx
jg     11b0 <dirname+0x60>
test   %eax,%eax
je     11ed <dirname+0x9d>
movb   $0x0,0x1(%rbx,%rdx,1)
jmp    120b <dirname+0xbb>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <fprintf@plt>
lea    0xe12(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe09(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <fprintf@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [3 x i8] c"//\00"
@stdout = global i32 0
@.str.2 = [3 x i8] c"/\0A\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [2 x i8] zeroinitializer
@.str.5 = [4 x i8] c"%s\0A\00"
define void @dirname(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %1
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %5
%9 = add i32 %2, -1
%10 = icmp eq i32 %9, 0
br i1 %10, label %25, label %11
11: ; preds = %8
%12 = sext i32 %9 to i64
br label %16
13: ; preds = %5, %1
%14 = load i32, i32* @stdout
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %48
16: ; preds = %11, %21
%17 = phi i64 [ %12, %11 ], [ %22, %21 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 47
br i1 %20, label %21, label %25
21: ; preds = %16
store i8 0, i8* %18
%22 = add nsw i64 %17, -1
%23 = trunc i64 %22 to i32
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %16
25: ; preds = %16, %21, %8
%26 = phi i64 [ 0, %8 ], [ 0, %21 ], [ %17, %16 ]
%27 = shl i64 %26, 32
%28 = ashr exact i64 %27, 32
br label %29
29: ; preds = %29, %25
%30 = phi i64 [ %36, %29 ], [ %28, %25 ]
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = load i8, i8* %31
%33 = icmp ne i8 %32, 47
%34 = icmp sgt i64 %30, 0
%35 = select i1 %33, i1 %34, i1 false
%36 = add nsw i64 %30, -1
br i1 %35, label %29, label %37
37: ; preds = %29
%38 = trunc i64 %30 to i32
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%42 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %45
43: ; preds = %37
%44 = getelementptr inbounds i8, i8* %0, i64 %30
store i8 0, i8* %44
br label %45
45: ; preds = %43, %40
%46 = load i32, i32* @stdout
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %48
48: ; preds = %45, %13
ret void
}