#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 const *, char *);
int /*<<< orphan*/ write_to_log(char *, char const *);
int open_files(FILE **input, FILE **output, const char *input_name,
               const char *output_name) {
  *input = fopen(input_name, "r");
  if (!*input) {
    write_to_log("Error: unable to open input file: %s\n", input_name);
    return -1;
  }
  *output = fopen(output_name, "w");
  if (!*output) {
    write_to_log("Error: unable to open output file: %s\n", output_name);
    fclose(*input);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<open_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xead(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    118c <open_files+0x5c>
mov    -0x20(%rbp),%rsi
lea    0xe87(%rip),%rdi
call   1040 <write_to_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <open_files+0xb3>
mov    -0x28(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11dc <open_files+0xac>
mov    -0x28(%rbp),%rsi
lea    0xe6b(%rip),%rdi
call   1040 <write_to_log@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <open_files+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_files>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xeb5(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     1192 <open_files+0x62>
lea    0xec5(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
mov    %rax,%rcx
mov    %rax,(%r15)
xor    %eax,%eax
test   %rcx,%rcx
jne    11a6 <open_files+0x76>
lea    0xeab(%rip),%rdi
mov    %r14,%rsi
call   1040 <write_to_log@plt>
mov    (%r12),%rdi
call   1030 <fclose@plt>
jmp    11a1 <open_files+0x71>
lea    0xe69(%rip),%rdi
mov    %rbx,%rsi
call   1040 <write_to_log@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [38 x i8] c"Error: unable to open input file: %s\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [39 x i8] c"Error: unable to open output file: %s\0A\00"
define i32 @open_files(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 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = load i32**, i32*** %6
store i32* %11, i32** %12
%13 = load i32**, i32*** %6
%14 = load i32*, i32** %13
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = load i8*, i8** %8
%18 = call i32 @write_to_log(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 -1, i32* %5
br label %33
19: ; preds = %4
%20 = load i8*, i8** %9
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32**, i32*** %7
store i32* %21, i32** %22
%23 = load i32**, i32*** %7
%24 = load i32*, i32** %23
%25 = icmp ne i32* %24, null
br i1 %25, label %32, label %26
26: ; preds = %19
%27 = load i8*, i8** %9
%28 = call i32 @write_to_log(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = load i32**, i32*** %6
%30 = load i32*, i32** %29
%31 = call i32 @fclose(i32* %30)
store i32 -1, i32* %5
br label %33
32: ; preds = %19
store i32 0, i32* %5
br label %33
33: ; preds = %32, %26, %16
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [38 x i8] c"Error: unable to open input file: %s\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [39 x i8] c"Error: unable to open output file: %s\0A\00"
define i32 @open_files(i32** nocapture %0, i32** nocapture writeonly %1, i8* %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %0
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @write_to_log(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %16
9: ; preds = %4
%10 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %10, i32** %1
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = tail call i32 @write_to_log(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%14 = load i32*, i32** %0
%15 = tail call i32 @fclose(i32* %14)
br label %16
16: ; preds = %9, %12, %7
%17 = phi i32 [ -1, %12 ], [ -1, %7 ], [ 0, %9 ]
ret i32 %17
}