#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 ENOMEM;
int /*<<< orphan*/ assert(char **);
char *basename(char *);
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
int basename_alloc(const char *path, char **buf) {
  char *c, *bname;
  assert(buf);
  if (!(c = strdup(path)))
    return -ENOMEM;
  bname = basename(c);
  if (!(*buf = strdup(bname))) {
    free(c);
    return -ENOMEM;
  }
  free(c);
  return 0;
}


x86 assembly (clang, -O0):
<basename_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1183 <basename_alloc+0x43>
xor    %eax,%eax
mov    0x2e67(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d6 <basename_alloc+0x96>
mov    -0x20(%rbp),%rdi
call   1050 <basename@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strdup@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11c6 <basename_alloc+0x86>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    0x2e24(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d6 <basename_alloc+0x96>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basename_alloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1060 <strdup@plt>
test   %rax,%rax
je     1188 <basename_alloc+0x48>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <basename@plt>
mov    %rax,%rdi
call   1060 <strdup@plt>
mov    %rax,%r15
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
test   %r15,%r15
jne    1193 <basename_alloc+0x53>
mov    0x2e51(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@ENOMEM = global i32 0
define i32 @basename_alloc(i8* nocapture %0, i8** %1) {
%3 = tail call i32 @assert(i8** %1)
%4 = tail call i8* @strdup(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @ENOMEM
%8 = sub nsw i32 0, %7
br label %17
9: ; preds = %2
%10 = tail call i8* @basename(i8* %4)
%11 = tail call i8* @strdup(i8* %10)
store i8* %11, i8** %1
%12 = icmp eq i8* %11, null
%13 = tail call i32 @free(i8* %4)
br i1 %12, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @ENOMEM
%16 = sub nsw i32 0, %15
br label %17
17: ; preds = %9, %14, %6
%18 = phi i32 [ %16, %14 ], [ %8, %6 ], [ 0, %9 ]
ret i32 %18
}