#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*/ dief(char *, ...);
scalar_t__ errno;
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strrchr(char const *, char);
char *dirname(const char *path) {
  const char *last_slash = strrchr(path, '/');
  char *ret;
  if (last_slash == NULL) {
    errno = 0;
    dief("dirname: no slash in given path:%s", path);
  }
  if ((ret = malloc(last_slash + 1 - path)) == NULL)
    dief("no memory");
  memcpy(ret, path, last_slash - path);
  ret[last_slash - path] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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    1189 <dirname+0x49>
mov    0x2e70(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <dief@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11bb <dirname+0x7b>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <dief@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1179 <dirname+0x39>
mov    0x2e7f(%rip),%rax
movq   $0x0,(%rax)
lea    0xe91(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <dief@plt>
mov    %r15d,%edi
sub    %r14d,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    119d <dirname+0x5d>
lea    0xe8d(%rip),%rdi
xor    %eax,%eax
call   1040 <dief@plt>
sub    %r14,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@errno = global i64 0
@.str = [35 x i8] c"dirname: no slash in given path:%s\00"
@.str.1 = [10 x i8] c"no memory\00"
define i8* @dirname(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
store i64 0, i64* @errno
%5 = tail call i32 (i8*, ...) @dief(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %6
6: ; preds = %4, %1
%7 = getelementptr inbounds i8, i8* %2, i64 1
%8 = ptrtoint i8* %7 to i64
%9 = ptrtoint i8* %0 to i64
%10 = sub i64 %8, %9
%11 = trunc i64 %10 to i32
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 (i8*, ...) @dief(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %6
%17 = ptrtoint i8* %2 to i64
%18 = sub i64 %17, %9
%19 = trunc i64 %18 to i32
%20 = tail call i32 @memcpy(i8* %12, i8* %0, i32 %19)
%21 = getelementptr inbounds i8, i8* %12, i64 %18
store i8 0, i8* %21
ret i8* %12
}