#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*/ memcpy(char *, char *, int);
int strlen(char *);
char *dirname(char *path) {
  static char dir[256] = {0};
  int i, len = strlen(path);
  for (i = len; i >= 0; --i) {
    if (path[i] == '/') {
      break;
    }
  }
  if (i == 0) {
    return "/";
  }
  if (i < 0) {
    return ".";
  }
  memcpy(dir, path, i);
  dir[i] = 0;
  return dir;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1175 <dirname+0x55>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1162 <dirname+0x42>
jmp    1175 <dirname+0x55>
jmp    1167 <dirname+0x47>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    113e <dirname+0x1e>
cmpl   $0x0,-0x14(%rbp)
jne    118f <dirname+0x6f>
lea    0xe7a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11d6 <dirname+0xb6>
cmpl   $0x0,-0x14(%rbp)
jge    11a9 <dirname+0x89>
lea    0xe62(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11d6 <dirname+0xb6>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0x2e89(%rip),%rdi
call   1040 <memcpy@plt>
movslq -0x14(%rbp),%rcx
lea    0x2e79(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e6e(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
lea    0xece(%rip),%rcx
test   %eax,%eax
js     1166 <dirname+0x46>
mov    %eax,%ebx
lea    0x2eff(%rip),%r14
add    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%edx
cmpb   $0x2f,(%r15,%rdx,1)
je     116b <dirname+0x4b>
lea    -0x1(%rax),%edx
add    $0xffffffffffffffff,%rbx
test   %eax,%eax
mov    %edx,%eax
jg     1150 <dirname+0x30>
mov    %rcx,%r14
jmp    1188 <dirname+0x68>
test   %eax,%eax
je     1181 <dirname+0x61>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx)
jmp    1188 <dirname+0x68>
lea    0xe78(%rip),%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@dirname.dir = internal global [256 x i8] zeroinitializer
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
define i8* @dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
store i32 %8, i32* %4
br label %9
9: ; preds = %22, %1
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %25
12: ; preds = %9
%13 = load i8*, i8** %3
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %20, label %21
20: ; preds = %12
br label %25
21: ; preds = %12
br label %22
22: ; preds = %21
%23 = load i32, i32* %4
%24 = add nsw i32 %23, -1
store i32 %24, i32* %4
br label %9
25: ; preds = %20, %9
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %40
29: ; preds = %25
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %40
33: ; preds = %29
%34 = load i8*, i8** %3
%35 = load i32, i32* %4
%36 = call i32 @memcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dir, i64 0, i64 0), i8* %34, i32 %35)
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [256 x i8], [256 x i8]* @dirname.dir, i64 0, i64 %38
store i8 0, i8* %39
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dir, i64 0, i64 0), i8** %2
br label %40
40: ; preds = %33, %32, %28
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
@dirname.dir = internal global [256 x i8] zeroinitializer
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
define i8* @dirname(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %19
4: ; preds = %1, %10
%5 = phi i32 [ %11, %10 ], [ %2, %1 ]
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 47
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = add nsw i32 %5, -1
%12 = icmp sgt i32 %5, 0
br i1 %12, label %4, label %19
13: ; preds = %4
%14 = icmp eq i32 %5, 0
br i1 %14, label %19, label %15
15: ; preds = %13
%16 = zext i32 %5 to i64
%17 = tail call i32 @memcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dir, i64 0, i64 0), i8* %0, i32 %5)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* @dirname.dir, i64 0, i64 %16
store i8 0, i8* %18
br label %19
19: ; preds = %10, %1, %13, %15
%20 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @dirname.dir, i64 0, i64 0), %15 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %10 ]
ret i8* %20
}