#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int open_file_handles(int argc, char *argv[], FILE **fh_in, FILE **fh_out) {
  FILE *in = NULL;
  FILE *out = NULL;
  switch (argc) {
  case 4:
    out = fopen(argv[3], "wb");
    if (!out) {
      perror("* Output fopen() error");
      return 1;
    }
  case 3:
    in = fopen(argv[2], "rb");
    if (!in) {
      perror("* Input fopen() error");
      (void)fclose(out);
      return 1;
    }
  }
  if (in)
    *fh_in = in;
  if (out)
    *fh_out = out;
  return 0;
}


x86 assembly (clang, -O0):
<open_file_handles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x3,%eax
je     11bc <open_file_handles+0x8c>
jmp    116b <open_file_handles+0x3b>
mov    -0x34(%rbp),%eax
sub    $0x4,%eax
jne    1205 <open_file_handles+0xd5>
jmp    117c <open_file_handles+0x4c>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe75(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b7 <open_file_handles+0x87>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1238 <open_file_handles+0x108>
jmp    11bc <open_file_handles+0x8c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe4f(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1200 <open_file_handles+0xd0>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1238 <open_file_handles+0x108>
jmp    1205 <open_file_handles+0xd5>
cmpq   $0x0,-0x28(%rbp)
je     121b <open_file_handles+0xeb>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x30(%rbp)
je     1231 <open_file_handles+0x101>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file_handles>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
xor    %r12d,%r12d
cmp    $0x3,%edi
je     117b <open_file_handles+0x4b>
cmp    $0x4,%edi
jne    119e <open_file_handles+0x6e>
mov    0x18(%rbx),%rdi
lea    0xea6(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    117e <open_file_handles+0x4e>
lea    0xe95(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%r12d
jmp    119e <open_file_handles+0x6e>
xor    %r13d,%r13d
mov    0x10(%rbx),%rdi
lea    0xe91(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     11ab <open_file_handles+0x7b>
mov    %rax,(%r15)
test   %r13,%r13
je     119e <open_file_handles+0x6e>
mov    %r13,(%r14)
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe6b(%rip),%rdi
call   1050 <perror@plt>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    $0x1,%r12d
jmp    119e <open_file_handles+0x6e>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [23 x i8] c"* Output fopen() error\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [22 x i8] c"* Input fopen() error\00"
define i32 @open_file_handles(i32 %0, i8** %1, i32** %2, i32** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32**
%9 = alloca i32**
%10 = alloca i32*
%11 = alloca i32*
store i32 %0, i32* %6
store i8** %1, i8*** %7
store i32** %2, i32*** %8
store i32** %3, i32*** %9
store i32* null, i32** %10
store i32* null, i32** %11
%12 = load i32, i32* %6
switch i32 %12, label %35 [
i32 4, label %13
i32 3, label %23
]
13: ; preds = %4
%14 = load i8**, i8*** %7
%15 = getelementptr inbounds i8*, i8** %14, i64 3
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %11
%18 = load i32*, i32** %11
%19 = icmp ne i32* %18, null
br i1 %19, label %22, label %20
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %5
br label %48
22: ; preds = %13
br label %23
23: ; preds = %4, %22
%24 = load i8**, i8*** %7
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %27, i32** %10
%28 = load i32*, i32** %10
%29 = icmp ne i32* %28, null
br i1 %29, label %34, label %30
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32*, i32** %11
%33 = call i32 @fclose(i32* %32)
store i32 1, i32* %5
br label %48
34: ; preds = %23
br label %35
35: ; preds = %34, %4
%36 = load i32*, i32** %10
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32*, i32** %10
%40 = load i32**, i32*** %8
store i32* %39, i32** %40
br label %41
41: ; preds = %38, %35
%42 = load i32*, i32** %11
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32*, i32** %11
%46 = load i32**, i32*** %9
store i32* %45, i32** %46
br label %47
47: ; preds = %44, %41
store i32 0, i32* %5
br label %48
48: ; preds = %47, %30, %20
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [23 x i8] c"* Output fopen() error\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [22 x i8] c"* Input fopen() error\00"
define i32 @open_file_handles(i32 %0, i8** nocapture %1, i32** nocapture writeonly %2, i32** nocapture writeonly %3) {
switch i32 %0, label %24 [
i32 4, label %5
i32 3, label %12
]
5: ; preds = %4
%6 = getelementptr inbounds i8*, i8** %1, i64 3
%7 = load i8*, i8** %6
%8 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %24
12: ; preds = %5, %4
%13 = phi i32* [ null, %4 ], [ %8, %5 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @fclose(i32* %13)
br label %24
21: ; preds = %12
store i32* %16, i32** %2
%22 = icmp eq i32* %13, null
br i1 %22, label %24, label %23
23: ; preds = %21
store i32* %13, i32** %3
br label %24
24: ; preds = %4, %21, %23, %18, %10
%25 = phi i32 [ 1, %18 ], [ 1, %10 ], [ 0, %23 ], [ 0, %21 ], [ 0, %4 ]
ret i32 %25
}