#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*/ fatal(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strrchr(char const *, char);
extern char *dirname(const char *s) {
  char *p = strrchr(s, '/');
  if (!p)
    s = "./";
  int len = (p ? p - s : 1);
  char *t = (char *)malloc(len + 1);
  if (!t)
    fatal(0, "dirname(): malloc() failed");
  memcpy(t, s, len);
  *(t + len) = 0;
  return t;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <dirname+0x34>
lea    0xe90(%rip),%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1193 <dirname+0x53>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    11a1 <dirname+0x61>
mov    $0x1,%eax
mov    %rax,-0x28(%rbp)
jmp    11a1 <dirname+0x61>
mov    -0x28(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d0 <dirname+0x90>
xor    %edi,%edi
lea    0xe38(%rip),%rsi
call   1060 <fatal@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
lea    0xea4(%rip),%r14
cmovne %rbx,%r14
mov    %eax,%ecx
sub    %r14d,%ecx
test   %rax,%rax
mov    $0x1,%ebx
cmovne %ecx,%ebx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    118e <dirname+0x4e>
lea    0xe7c(%rip),%rsi
xor    %edi,%edi
call   1060 <fatal@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [27 x i8] c"dirname(): malloc() failed\00"
define i8* @dirname(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
%4 = select i1 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %4 to i64
%7 = sub i64 %5, %6
%8 = trunc i64 %7 to i32
%9 = select i1 %3, i32 1, i32 %8
%10 = add nsw i32 %9, 1
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 @fatal(i32 0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = tail call i32 @memcpy(i8* %12, i8* %4, i32 %9)
%18 = sext i32 %9 to i64
%19 = getelementptr inbounds i8, i8* %12, i64 %18
store i8 0, i8* %19
ret i8* %12
}