#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;
scalar_t__ 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 = (char *)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 i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = call i32 (i8*, ...) @dief(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %12
%26 = load i8*, i8** %4
%27 = load i8*, i8** %2
%28 = load i8*, i8** %3
%29 = load i8*, i8** %2
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = trunc i64 %32 to i32
%34 = call i32 @memcpy(i8* %26, i8* %27, i32 %33)
%35 = load i8*, i8** %4
%36 = load i8*, i8** %3
%37 = load i8*, i8** %2
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = getelementptr inbounds i8, i8* %35, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %4
ret i8* %42
}

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 i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %17
15: ; preds = %6
%16 = tail call i32 (i8*, ...) @dief(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %6
%18 = ptrtoint i8* %2 to i64
%19 = sub i64 %18, %9
%20 = trunc i64 %19 to i32
%21 = tail call i32 @memcpy(i8* %13, i8* %0, i32 %20)
%22 = getelementptr inbounds i8, i8* %13, i64 %19
store i8 0, i8* %22
ret i8* %13
}