#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 *);
size_t fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ init_empty_tape(char **, unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t);
void init_data_tape(char **tape, unsigned int length, char *datafile) {
  init_empty_tape(tape, length);
  FILE *filehandle = fopen(datafile, "r");
  if (filehandle == NULL) {
    perror("Error opening data file for tape");
    exit(-1);
  }
  size_t bytes = fread(*tape, sizeof(char), length, filehandle);
  printf("Read %zd bytes of data to the tape\n", bytes);
  if (ferror(filehandle)) {
    perror("Error reading tape data");
    fclose(filehandle);
    exit(-1);
  }
  fclose(filehandle);
}


x86 assembly (clang, -O0):
<init_data_tape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1070 <init_empty_tape@plt>
mov    -0x18(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d4 <init_data_tape+0x54>
lea    0xe3d(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe28(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1232 <init_data_tape+0xb2>
lea    0xe2d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_data_tape>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
call   1070 <init_empty_tape@plt>
lea    0xe67(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e5 <init_data_tape+0x65>
mov    %rax,%rbx
mov    0x0(%rbp),%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
lea    0xe5f(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11fb <init_data_tape+0x7b>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <fclose@plt>
lea    0xe16(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xe45(%rip),%rdi
call   1090 <perror@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"Error opening data file for tape\00"
@.str.2 = [36 x i8] c"Read %zd bytes of data to the tape\0A\00"
@.str.3 = [24 x i8] c"Error reading tape data\00"
define void @init_data_tape(i8** %0, i32 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i8**, i8*** %4
%10 = load i32, i32* %5
%11 = call i32 @init_empty_tape(i8** %9, i32 %10)
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %3
%20 = load i8**, i8*** %4
%21 = load i8*, i8** %20
%22 = load i32, i32* %5
%23 = load i32*, i32** %7
%24 = call i64 @fread(i8* %21, i32 1, i32 %22, i32* %23)
store i64 %24, i64* %8
%25 = load i64, i64* %8
%26 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64 %25)
%27 = load i32*, i32** %7
%28 = call i64 @ferror(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %19
%31 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32*, i32** %7
%33 = call i32 @fclose(i32* %32)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %19
%36 = load i32*, i32** %7
%37 = call i32 @fclose(i32* %36)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"Error opening data file for tape\00"
@.str.2 = [36 x i8] c"Read %zd bytes of data to the tape\0A\00"
@.str.3 = [24 x i8] c"Error reading tape data\00"
define void @init_data_tape(i8** %0, i32 %1, i8* nocapture %2) {
%4 = tail call i32 @init_empty_tape(i8** %0, i32 %1)
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %3
%11 = load i8*, i8** %0
%12 = tail call i64 @fread(i8* %11, i32 1, i32 %1, i32* %5)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64 %12)
%14 = tail call i64 @ferror(i32* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @fclose(i32* %5)
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %10
%21 = tail call i32 @fclose(i32* %5)
ret void
}