#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 */
size_t strlen(char *);
const char *dirfile(char *path, char **filename) {
  size_t len = strlen(path);
  while (len > 0 && path[len - 1] == '/')
    path[--len] = '\0';
  while (len > 0 && path[len - 1] != '/')
    len--;
  if (len == 0) {
    *filename = path;
    return ".";
  }
  *filename = path + len;
  path[len - 1] = '\0';
  if (*path)
    return path;
  return "/";
}


x86 assembly (clang, -O0):
<dirfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jbe    1156 <dirfile+0x46>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1166 <dirfile+0x56>
jmp    1183 <dirfile+0x73>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x20(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
jmp    112d <dirfile+0x1d>
jmp    1188 <dirfile+0x78>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x22(%rbp)
jbe    11b1 <dirfile+0xa1>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    11c1 <dirfile+0xb1>
jmp    11d2 <dirfile+0xc2>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1188 <dirfile+0x78>
cmpq   $0x0,-0x20(%rbp)
jne    11f8 <dirfile+0xe8>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
lea    0xe11(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    123c <dirfile+0x12c>
mov    -0x10(%rbp),%rcx
add    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1231 <dirfile+0x121>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123c <dirfile+0x12c>
lea    0xdca(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirfile>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
je     1142 <dirfile+0x32>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x2f,-0x1(%rbx,%rax,1)
jne    1144 <dirfile+0x34>
movb   $0x0,-0x1(%rbx,%rax,1)
add    $0xffffffffffffffff,%rax
jne    1130 <dirfile+0x20>
xor    %eax,%eax
mov    %rax,%rcx
nopw   0x0(%rax,%rax,1)
sub    $0x1,%rcx
jb     117f <dirfile+0x6f>
cmpb   $0x2f,-0x1(%rbx,%rax,1)
mov    %rcx,%rax
jne    1150 <dirfile+0x40>
lea    (%rbx,%rcx,1),%rax
add    $0x1,%rax
mov    %rax,(%r14)
movb   $0x0,(%rbx,%rcx,1)
cmpb   $0x0,(%rbx)
lea    0xe89(%rip),%rax
cmovne %rbx,%rax
jmp    1189 <dirfile+0x79>
mov    %rbx,(%r14)
lea    0xe77(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @dirfile(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%7 = load i8*, i8** %4
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %6
br label %9
9: ; preds = %22, %2
%10 = load i64, i64* %6
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i8*, i8** %4
%14 = load i64, i64* %6
%15 = sub i64 %14, 1
%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 label %20
20: ; preds = %12, %9
%21 = phi i1 [ false, %9 ], [ %19, %12 ]
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = load i8*, i8** %4
%24 = load i64, i64* %6
%25 = add i64 %24, -1
store i64 %25, i64* %6
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
br label %9
27: ; preds = %20
br label %28
28: ; preds = %41, %27
%29 = load i64, i64* %6
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = load i64, i64* %6
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 47
br label %39
39: ; preds = %31, %28
%40 = phi i1 [ false, %28 ], [ %38, %31 ]
br i1 %40, label %41, label %44
41: ; preds = %39
%42 = load i64, i64* %6
%43 = add i64 %42, -1
store i64 %43, i64* %6
br label %28
44: ; preds = %39
%45 = load i64, i64* %6
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i8*, i8** %4
%49 = load i8**, i8*** %5
store i8* %48, i8** %49
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3
br label %65
50: ; preds = %44
%51 = load i8*, i8** %4
%52 = load i64, i64* %6
%53 = getelementptr inbounds i8, i8* %51, i64 %52
%54 = load i8**, i8*** %5
store i8* %53, i8** %54
%55 = load i8*, i8** %4
%56 = load i64, i64* %6
%57 = sub i64 %56, 1
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %4
%60 = load i8, i8* %59
%61 = icmp ne i8 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %50
%63 = load i8*, i8** %4
store i8* %63, i8** %3
br label %65
64: ; preds = %50
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %65
65: ; preds = %64, %62, %47
%66 = load i8*, i8** %3
ret i8* %66
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @dirfile(i8* %0, i8** nocapture writeonly %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %2, %11
%6 = phi i64 [ %7, %11 ], [ %3, %2 ]
%7 = add i64 %6, -1
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 47
br i1 %10, label %11, label %13
11: ; preds = %5
store i8 0, i8* %8
%12 = icmp eq i64 %7, 0
br i1 %12, label %13, label %5
13: ; preds = %5, %11, %2
%14 = phi i64 [ 0, %2 ], [ %6, %5 ], [ 0, %11 ]
br label %15
15: ; preds = %13, %18
%16 = phi i64 [ %19, %18 ], [ %14, %13 ]
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = add i64 %16, -1
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 47
br i1 %22, label %24, label %15
23: ; preds = %15
store i8* %0, i8** %1
br label %30
24: ; preds = %18
%25 = getelementptr inbounds i8, i8* %0, i64 %19
%26 = getelementptr inbounds i8, i8* %0, i64 %16
store i8* %26, i8** %1
store i8 0, i8* %25
%27 = load i8, i8* %0
%28 = icmp eq i8 %27, 0
%29 = select i1 %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %0
br label %30
30: ; preds = %24, %23
%31 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %23 ], [ %29, %24 ]
ret i8* %31
}