#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 */
struct stack {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ dtree_error_from_errno();
char *file_path_from_stack(struct stack **, char const *);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ free(void *);
FILE *path_fopen(struct stack **path, const char *fname, const char *mode) {
  const char *fpath = file_path_from_stack(path, fname);
  if (fpath == NULL)
    goto clean_and_exit;
  FILE *file = fopen(fpath, mode);
  if (file == NULL)
    goto clean_and_exit;
  free((void *)fpath);
  return file;
clean_and_exit:
  dtree_error_from_errno();
  if (fpath != NULL)
    free((void *)fpath);
  return NULL;
}


x86 assembly (clang, -O0):
<path_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <file_path_from_stack@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1175 <path_fopen+0x35>
jmp    11ac <path_fopen+0x6c>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1196 <path_fopen+0x56>
jmp    11ac <path_fopen+0x6c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cf <path_fopen+0x8f>
mov    $0x0,%al
call   1060 <dtree_error_from_errno@plt>
cmpq   $0x0,-0x28(%rbp)
je     11c7 <path_fopen+0x87>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_fopen>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
call   1040 <file_path_from_stack@plt>
test   %rax,%rax
je     1169 <path_fopen+0x29>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1175 <path_fopen+0x35>
mov    %rax,%r14
jmp    117f <path_fopen+0x3f>
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <dtree_error_from_errno@plt>
jmp    1187 <path_fopen+0x47>
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <dtree_error_from_errno@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stack = type { i32 }
define i32* @path_fopen(%struct.stack** %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca %struct.stack**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
store %struct.stack** %0, %struct.stack*** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load %struct.stack**, %struct.stack*** %5
%11 = load i8*, i8** %6
%12 = call i8* @file_path_from_stack(%struct.stack** %10, i8* %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
br label %27
16: ; preds = %3
%17 = load i8*, i8** %8
%18 = load i8*, i8** %7
%19 = call i32* @fopen(i8* %17, i8* %18)
store i32* %19, i32** %9
%20 = load i32*, i32** %9
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %16
br label %27
23: ; preds = %16
%24 = load i8*, i8** %8
%25 = call i32 @free(i8* %24)
%26 = load i32*, i32** %9
store i32* %26, i32** %4
br label %35
27: ; preds = %22, %15
%28 = call i32 (...) @dtree_error_from_errno()
%29 = load i8*, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
br label %34
34: ; preds = %31, %27
store i32* null, i32** %4
br label %35
35: ; preds = %34, %23
%36 = load i32*, i32** %4
ret i32* %36
}

LLVM IR (-O2):
%struct.stack = type { i32 }
define noalias i32* @path_fopen(%struct.stack** %0, i8* %1, i8* nocapture %2) {
%4 = tail call i8* @file_path_from_stack(%struct.stack** %0, i8* %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (...) @dtree_error_from_errno()
br label %16
8: ; preds = %3
%9 = tail call i32* @fopen(i8* %4, i8* %2)
%10 = icmp eq i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @free(i8* %4)
br label %16
13: ; preds = %8
%14 = tail call i32 (...) @dtree_error_from_errno()
%15 = tail call i32 @free(i8* %4)
br label %16
16: ; preds = %6, %13, %11
%17 = phi i32* [ %9, %11 ], [ null, %13 ], [ null, %6 ]
ret i32* %17
}