#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ debug(char *, char *, char *);
int /*<<< orphan*/ error(char *, char *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
int stream_open(FILE **stream, FILE *default_stream, char *filename,
                char *mode) {
  if (filename == NULL) {
    *stream = default_stream;
  } else {
    debug("Opening %s with mode %s.\n", filename, mode);
    *stream = fopen(filename, mode);
    if (*stream == NULL) {
      error("Could not open %s with mode %s.\n", filename, mode);
      return (-1);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<stream_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1163 <stream_open+0x33>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11c1 <stream_open+0x91>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe8e(%rip),%rdi
call   1030 <debug@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <fopen@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11bc <stream_open+0x8c>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe6f(%rip),%rdi
call   1040 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <stream_open+0x98>
jmp    11c1 <stream_open+0x91>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stream_open>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
test   %rdx,%rdx
je     1186 <stream_open+0x56>
mov    %rcx,%r14
mov    %rdx,%rbx
lea    0xeb6(%rip),%rdi
mov    %rdx,%rsi
mov    %rcx,%rdx
call   1030 <debug@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fopen@plt>
mov    %rax,%rcx
mov    %rax,(%r15)
xor    %eax,%eax
test   %rcx,%rcx
jne    118b <stream_open+0x5b>
lea    0xea6(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <error@plt>
mov    $0xffffffff,%eax
jmp    118b <stream_open+0x5b>
mov    %rsi,(%r15)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Opening %s with mode %s.\0A\00"
@.str.1 = [33 x i8] c"Could not open %s with mode %s.\0A\00"
define i32 @stream_open(i32** %0, i32* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
store i32** %0, i32*** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%10 = load i8*, i8** %8
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = load i32*, i32** %7
%14 = load i32**, i32*** %6
store i32* %13, i32** %14
br label %31
15: ; preds = %4
%16 = load i8*, i8** %8
%17 = load i8*, i8** %9
%18 = call i32 @debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %17)
%19 = load i8*, i8** %8
%20 = load i8*, i8** %9
%21 = call i32* @fopen(i8* %19, i8* %20)
%22 = load i32**, i32*** %6
store i32* %21, i32** %22
%23 = load i32**, i32*** %6
%24 = load i32*, i32** %23
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = load i8*, i8** %8
%28 = load i8*, i8** %9
%29 = call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %28)
store i32 -1, i32* %5
br label %32
30: ; preds = %15
br label %31
31: ; preds = %30, %12
store i32 0, i32* %5
br label %32
32: ; preds = %31, %26
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
@.str = [26 x i8] c"Opening %s with mode %s.\0A\00"
@.str.1 = [33 x i8] c"Could not open %s with mode %s.\0A\00"
define i32 @stream_open(i32** nocapture writeonly %0, i32* %1, i8* %2, i8* %3) {
%5 = icmp eq i8* %2, null
br i1 %5, label %6, label %7
6: ; preds = %4
store i32* %1, i32** %0
br label %13
7: ; preds = %4
%8 = tail call i32 @debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %3)
%9 = tail call i32* @fopen(i8* %2, i8* %3)
store i32* %9, i32** %0
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %3)
br label %13
13: ; preds = %6, %7, %11
%14 = phi i32 [ -1, %11 ], [ 0, %7 ], [ 0, %6 ]
ret i32 %14
}