#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*/ free(char *);
char *realloc(char *, int);
char *strdup(char *);
int strlen(char *);
char *dirs_dirname(char *filename) {
  char *result = strdup(filename);
  bool found = false;
  for (size_t i = strlen(result); i > 0; i--) {
    if (result[i - 1] == '/' || result[i - 1] == '\\') {
      result[i] = '\0';
      found = true;
      result = realloc(result, (strlen(result) + 1) * sizeof(char));
      break;
    }
  }
  if (!found) {
    free(result);
    result = strdup("./");
  }
  return result;
}


x86 assembly (clang, -O0):
<dirs_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1203 <dirs_dirname+0xc3>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11ac <dirs_dirname+0x6c>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5c,%eax
jne    11ed <dirs_dirname+0xad>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
jmp    1203 <dirs_dirname+0xc3>
jmp    11f2 <dirs_dirname+0xb2>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    116f <dirs_dirname+0x2f>
cmpl   $0x0,-0x14(%rbp)
jne    1226 <dirs_dirname+0xe6>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xde3(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirs_dirname>:
push   %rbx
call   1060 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
mov    %rcx,%rax
nopw   0x0(%rax,%rax,1)
sub    $0x1,%rax
jb     1191 <dirs_dirname+0x51>
movzbl -0x1(%rbx,%rcx,1),%edx
cmp    $0x5c,%dl
je     1178 <dirs_dirname+0x38>
mov    %rax,%rcx
cmp    $0x2f,%dl
jne    1160 <dirs_dirname+0x20>
movb   $0x0,0x1(%rbx,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%esi
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <realloc@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe60(%rip),%rdi
pop    %rbx
jmp    1060 <strdup@plt>

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

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