#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 */
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strrchr(char const *, char);
void chop_dirname(const char *name, char **dirname) {
  char *last_slash = strrchr(name, '/');
  if (!last_slash)
    last_slash = strrchr(name, '\\');
  if (!last_slash) {
    *dirname = malloc(1);
    **dirname = 0;
  } else {
    char *newdata = malloc(last_slash - name + 1);
    memcpy(newdata, name, last_slash - name);
    newdata[last_slash - name] = 0;
    *dirname = newdata;
  }
}


x86 assembly (clang, -O0):
<chop_dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116f <chop_dirname+0x3f>
mov    -0x8(%rbp),%rdi
mov    $0x5c,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119d <chop_dirname+0x6d>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
jmp    11ef <chop_dirname+0xbf>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chop_dirname>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1165 <chop_dirname+0x35>
mov    %r15,%rdi
mov    $0x5c,%esi
call   1030 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1194 <chop_dirname+0x64>
sub    %r15,%rbx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
mov    %r12,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
movb   $0x0,(%rax)
jmp    1188 <chop_dirname+0x58>

LLVM IR (-O0):
define void @chop_dirname(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8** %1, i8*** %4
%7 = load i8*, i8** %3
%8 = call i8* @strrchr(i8* %7, i8 signext 47)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i8* @strrchr(i8* %12, i8 signext 92)
store i8* %13, i8** %5
br label %14
14: ; preds = %11, %2
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = call i8* @malloc(i32 1)
%19 = load i8**, i8*** %4
store i8* %18, i8** %19
%20 = load i8**, i8*** %4
%21 = load i8*, i8** %20
store i8 0, i8* %21
br label %49
22: ; preds = %14
%23 = load i8*, i8** %5
%24 = load i8*, i8** %3
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
%28 = add nsw i64 %27, 1
%29 = trunc i64 %28 to i32
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = load i8*, i8** %3
%33 = load i8*, i8** %5
%34 = load i8*, i8** %3
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = trunc i64 %37 to i32
%39 = call i32 @memcpy(i8* %31, i8* %32, i32 %38)
%40 = load i8*, i8** %6
%41 = load i8*, i8** %5
%42 = load i8*, i8** %3
%43 = ptrtoint i8* %41 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = getelementptr inbounds i8, i8* %40, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %6
%48 = load i8**, i8*** %4
store i8* %47, i8** %48
br label %49
49: ; preds = %22, %17
ret void
}

LLVM IR (-O2):
define void @chop_dirname(i8* %0, i8** nocapture writeonly %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 92)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
store i8* %9, i8** %1
store i8 0, i8* %9
br label %20
10: ; preds = %2, %5
%11 = phi i8* [ %6, %5 ], [ %3, %2 ]
%12 = ptrtoint i8* %11 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = add i32 %15, 1
%17 = tail call i8* @malloc(i32 %16)
%18 = tail call i32 @memcpy(i8* %17, i8* %0, i32 %15)
%19 = getelementptr inbounds i8, i8* %17, i64 %14
store i8 0, i8* %19
store i8* %17, i8** %1
br label %20
20: ; preds = %10, %8
ret void
}