#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*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ dtree_error_from_errno();
char *file_path_from_stack(struct stack **, char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ *opendir(char const *);
DIR *opendir_on_stack(struct stack **path) {
  const char *fpath = file_path_from_stack(path, "");
  if (fpath == NULL)
    goto clean_and_exit;
  DIR *dir = opendir(fpath);
  if (dir == NULL)
    goto clean_and_exit;
  free((void *)fpath);
  return dir;
clean_and_exit:
  dtree_error_from_errno();
  if (fpath != NULL)
    free((void *)fpath);
  return NULL;
}


x86 assembly (clang, -O0):
<opendir_on_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1050 <file_path_from_stack@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1170 <opendir_on_stack+0x30>
jmp    11a3 <opendir_on_stack+0x63>
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118d <opendir_on_stack+0x4d>
jmp    11a3 <opendir_on_stack+0x63>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c6 <opendir_on_stack+0x86>
mov    $0x0,%al
call   1060 <dtree_error_from_errno@plt>
cmpq   $0x0,-0x18(%rbp)
je     11be <opendir_on_stack+0x7e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opendir_on_stack>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rsi
call   1050 <file_path_from_stack@plt>
test   %rax,%rax
je     116a <opendir_on_stack+0x2a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1176 <opendir_on_stack+0x36>
mov    %rax,%r14
jmp    1180 <opendir_on_stack+0x40>
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <dtree_error_from_errno@plt>
jmp    1188 <opendir_on_stack+0x48>
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 }
@.str = [1 x i8] zeroinitializer
define i32* @opendir_on_stack(%struct.stack** %0) {
%2 = alloca i32*
%3 = alloca %struct.stack**
%4 = alloca i8*
%5 = alloca i32*
store %struct.stack** %0, %struct.stack*** %3
%6 = load %struct.stack**, %struct.stack*** %3
%7 = call i8* @file_path_from_stack(%struct.stack** %6, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
br label %21
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %11
br label %21
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = call i32 @free(i8* %18)
%20 = load i32*, i32** %5
store i32* %20, i32** %2
br label %29
21: ; preds = %16, %10
%22 = call i32 (...) @dtree_error_from_errno()
%23 = load i8*, i8** %4
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
br label %28
28: ; preds = %25, %21
store i32* null, i32** %2
br label %29
29: ; preds = %28, %17
%30 = load i32*, i32** %2
ret i32* %30
}

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