#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
void save_tape(char *tape, unsigned int tape_length, char *file) {
  FILE *filehandle = fopen(file, "w");
  if (filehandle == NULL) {
    perror("Error opening tape output file");
    exit(-1);
  }
  fwrite(tape, sizeof(char), tape_length, filehandle);
  if (ferror(filehandle)) {
    perror("Error writing tape data");
    fclose(filehandle);
    exit(-1);
  }
  fclose(filehandle);
}


x86 assembly (clang, -O0):
<save_tape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a8 <save_tape+0x48>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11ef <save_tape+0x8f>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_tape>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xe8f(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11ad <save_tape+0x4d>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11c3 <save_tape+0x63>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [31 x i8] c"Error opening tape output file\00"
@.str.2 = [24 x i8] c"Error writing tape data\00"
define void @save_tape(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i8*, i8** %6
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = load i32, i32* %5
%18 = load i32*, i32** %7
%19 = call i32 @fwrite(i8* %16, i32 1, i32 %17, i32* %18)
%20 = load i32*, i32** %7
%21 = call i64 @ferror(i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32*, i32** %7
%26 = call i32 @fclose(i32* %25)
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %15
%29 = load i32*, i32** %7
%30 = call i32 @fclose(i32* %29)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [31 x i8] c"Error opening tape output file\00"
@.str.2 = [24 x i8] c"Error writing tape data\00"
define void @save_tape(i8* nocapture %0, i32 %1, i8* nocapture %2) {
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %3
%10 = tail call i32 @fwrite(i8* %0, i32 1, i32 %1, i32* %4)
%11 = tail call i64 @ferror(i32* %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @fclose(i32* %4)
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %9
%18 = tail call i32 @fclose(i32* %4)
ret void
}