#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 */
scalar_t__ strcmp(char *, char *);
int strlen(char *);
const char *my_dirname(char *path) {
  if (strcmp(path, ".") == 0) {
    return "..";
  } else if (strcmp(path, "/") == 0) {
    return "/";
  } else {
    int len = strlen(path);
    char *p = path + len - 1;
    while (p > path) {
      if (*p == '/') {
        break;
      }
      --p;
    }
    if (p > path) {
      *p = '\0';
      return path;
    } else if (*path == '/') {
      return "/";
    } else {
      return ".";
    }
  }
}


x86 assembly (clang, -O0):
<my_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeca(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1156 <my_dirname+0x36>
lea    0xeb3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <my_dirname+0x100>
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1180 <my_dirname+0x60>
lea    0xe8c(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <my_dirname+0x100>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    11d3 <my_dirname+0xb3>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11c2 <my_dirname+0xa2>
jmp    11d3 <my_dirname+0xb3>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    119f <my_dirname+0x7f>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    11f5 <my_dirname+0xd5>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <my_dirname+0x100>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1215 <my_dirname+0xf5>
lea    0xdf7(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <my_dirname+0x100>
lea    0xde5(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_dirname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xed2(%rip),%r14
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     118a <my_dirname+0x6a>
lea    0xec0(%rip),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11a4 <my_dirname+0x84>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %rbx,%rax
jbe    1193 <my_dirname+0x73>
lea    -0x1(%rax),%rcx
cmpb   $0x2f,(%rax)
mov    %rcx,%rax
jne    1170 <my_dirname+0x50>
movb   $0x0,0x1(%rcx)
mov    %rbx,%r15
jmp    11a4 <my_dirname+0x84>
lea    0xe6f(%rip),%r15
jmp    11a4 <my_dirname+0x84>
cmpb   $0x2f,(%rbx)
lea    0xe66(%rip),%rax
cmove  %rax,%r14
mov    %r14,%r15
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c"/\00"
define i8* @my_dirname(i8* %0) {
%2 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i64 %2, 0
br i1 %3, label %23, label %4
4: ; preds = %1
%5 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %23, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %0)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
br label %11
11: ; preds = %15, %7
%12 = phi i8* [ %10, %7 ], [ %13, %15 ]
%13 = getelementptr inbounds i8, i8* %12, i64 -1
%14 = icmp ugt i8* %13, %0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i8, i8* %13
%17 = icmp eq i8 %16, 47
br i1 %17, label %18, label %11
18: ; preds = %15
store i8 0, i8* %13
br label %23
19: ; preds = %11
%20 = load i8, i8* %0
%21 = icmp eq i8 %20, 47
%22 = select i1 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0)
br label %23
23: ; preds = %18, %19, %4, %1
%24 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %4 ], [ %0, %18 ], [ %22, %19 ]
ret i8* %24
}