#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 *);
size_t strlen(char *);
const char *my_dirname(char *path) {
  if (strcmp(path, ".") == 0) {
    return "..";
  } else if (strcmp(path, "/") == 0) {
    return "/";
  } else {
    size_t 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    121e <my_dirname+0xfe>
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    121e <my_dirname+0xfe>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    11d1 <my_dirname+0xb1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11c0 <my_dirname+0xa0>
jmp    11d1 <my_dirname+0xb1>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    119d <my_dirname+0x7d>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    11f3 <my_dirname+0xd3>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121e <my_dirname+0xfe>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1213 <my_dirname+0xf3>
lea    0xdf9(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    121e <my_dirname+0xfe>
lea    0xde7(%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>
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
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 i64
%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 %49
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 %49
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %4
%18 = load i8*, i8** %3
%19 = load i64, i64* %4
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 -1
store i8* %21, i8** %5
br label %22
22: ; preds = %32, %15
%23 = load i8*, i8** %5
%24 = load i8*, i8** %3
%25 = icmp ugt i8* %23, %24
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 47
br i1 %30, label %31, label %32
31: ; preds = %26
br label %35
32: ; preds = %26
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i32 -1
store i8* %34, i8** %5
br label %22
35: ; preds = %31, %22
%36 = load i8*, i8** %5
%37 = load i8*, i8** %3
%38 = icmp ugt i8* %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i8*, i8** %5
store i8 0, i8* %40
%41 = load i8*, i8** %3
store i8* %41, i8** %2
br label %49
42: ; preds = %35
%43 = load i8*, i8** %3
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 47
br i1 %46, label %47, label %48
47: ; preds = %42
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %49
48: ; preds = %42
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %49
49: ; preds = %48, %47, %39, %14, %9
%50 = load i8*, i8** %2
ret i8* %50
}

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 %22, 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 %22, label %7
7: ; preds = %4
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = getelementptr inbounds i8, i8* %0, i64 %8
br label %10
10: ; preds = %14, %7
%11 = phi i8* [ %9, %7 ], [ %12, %14 ]
%12 = getelementptr inbounds i8, i8* %11, i64 -1
%13 = icmp ugt i8* %12, %0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 47
br i1 %16, label %17, label %10
17: ; preds = %14
store i8 0, i8* %12
br label %22
18: ; preds = %10
%19 = load i8, i8* %0
%20 = icmp eq i8 %19, 47
%21 = select i1 %20, 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 %22
22: ; preds = %17, %18, %4, %1
%23 = 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, %17 ], [ %21, %18 ]
ret i8* %23
}