#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*/ assert(char *);
char *dirname(char *);
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
char *dirname_malloc(const char *path) {
  char *d, *dir, *dir2;
  d = strdup(path);
  if (!d)
    return NULL;
  dir = dirname(d);
  assert(dir);
  if (dir != d) {
    dir2 = strdup(dir);
    free(d);
    return dir2;
  }
  return dir;
}


x86 assembly (clang, -O0):
<dirname_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <dirname_malloc+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <dirname_malloc+0x80>
mov    -0x18(%rbp),%rdi
call   1040 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11b8 <dirname_malloc+0x78>
mov    -0x20(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c0 <dirname_malloc+0x80>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname_malloc>:
push   %r14
push   %rbx
push   %rax
call   1060 <strdup@plt>
test   %rax,%rax
je     1181 <dirname_malloc+0x41>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <dirname@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <assert@plt>
cmp    %rbx,%r14
je     1183 <dirname_malloc+0x43>
mov    %r14,%rdi
call   1060 <strdup@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
jmp    1186 <dirname_malloc+0x46>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @dirname_malloc(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strdup(i8* %7)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i8* null, i8** %2
br label %28
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = call i8* @dirname(i8* %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = call i32 @assert(i8* %15)
%17 = load i8*, i8** %5
%18 = load i8*, i8** %4
%19 = icmp ne i8* %17, %18
br i1 %19, label %20, label %26
20: ; preds = %12
%21 = load i8*, i8** %5
%22 = call i8* @strdup(i8* %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %4
%24 = call i32 @free(i8* %23)
%25 = load i8*, i8** %6
store i8* %25, i8** %2
br label %28
26: ; preds = %12
%27 = load i8*, i8** %5
store i8* %27, i8** %2
br label %28
28: ; preds = %26, %20, %11
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
define i8* @dirname_malloc(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = tail call i8* @dirname(i8* %2)
%6 = tail call i32 @assert(i8* %5)
%7 = icmp eq i8* %5, %2
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i8* @strdup(i8* %5)
%10 = tail call i32 @free(i8* %2)
br label %11
11: ; preds = %4, %1, %8
%12 = phi i8* [ %9, %8 ], [ null, %1 ], [ %2, %4 ]
ret i8* %12
}