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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
scalar_t__ strcmp(char const *, char *);
void read_input(const char *path, char *buffer, uint32_t buffersize,
                uint32_t *length) {
  FILE *file = 0;
  if (path) {
    if (strcmp(path, "-") == 0)
      file = stdin;
    else
      file = fopen(path, "rb");
    if (!file) {
      fprintf(stderr, "Failed to open %s for reading\n", path);
      *length = 0;
      return;
    }
    uint32_t l = 0;
    while (!feof(file)) {
      size_t num_read = fread((void *)&buffer[l], 1, 64, file);
      l += num_read;
      if (l > buffersize) {
        *length = 0xFFFFFFFF;
        fclose(file);
        return;
      }
    }
    *length = l;
    buffer[buffersize - 1] = 0;
    buffer[l] = 0;
    fclose(file);
  }
}


x86 assembly (clang, -O0):
<read_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     12ae <read_input+0x14e>
mov    -0x8(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11b8 <read_input+0x58>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11cc <read_input+0x6c>
mov    -0x8(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1200 <read_input+0xa0>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
jmp    12ae <read_input+0x14e>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1226 <read_input+0xc6>
jmp    127e <read_input+0x11e>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x40,%edx
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1279 <read_input+0x119>
mov    -0x20(%rbp),%rax
mov    $0xffffffff,%ecx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12ae <read_input+0x14e>
jmp    1208 <read_input+0xa8>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_input>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1212 <read_input+0xb2>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
lea    0xe79(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11e4 <read_input+0x84>
lea    0xe6a(%rip),%rsi
mov    %r13,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbp
test   %rbp,%rbp
je     11f3 <read_input+0x93>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1221 <read_input+0xc1>
lea    (%r12,%rbx,1),%rdi
mov    $0x1,%esi
mov    $0x40,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
add    %rax,%rbx
cmp    %r15,%rbx
jbe    11b0 <read_input+0x50>
mov    $0xffffffff,%eax
mov    %rax,(%r14)
jmp    122f <read_input+0xcf>
mov    0x2dfd(%rip),%rax
mov    (%rax),%rbp
test   %rbp,%rbp
jne    11a8 <read_input+0x48>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    %r13,%rdx
call   1060 <fprintf@plt>
movq   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,(%r14)
movb   $0x0,-0x1(%r15,%r12,1)
movb   $0x0,(%r12,%rbx,1)
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"Failed to open %s for reading\0A\00"
define void @read_input(i8* %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64* %3, i64** %8
store i32* null, i32** %9
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %66
14: ; preds = %4
%15 = load i8*, i8** %5
%16 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32*, i32** @stdin
store i32* %19, i32** %9
br label %23
20: ; preds = %14
%21 = load i8*, i8** %5
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %9
br label %23
23: ; preds = %20, %18
%24 = load i32*, i32** %9
%25 = icmp ne i32* %24, null
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %5
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i64*, i64** %8
store i64 0, i64* %30
br label %66
31: ; preds = %23
store i64 0, i64* %10
br label %32
32: ; preds = %53, %31
%33 = load i32*, i32** %9
%34 = call i32 @feof(i32* %33)
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
br i1 %36, label %37, label %54
37: ; preds = %32
%38 = load i8*, i8** %6
%39 = load i64, i64* %10
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = load i32*, i32** %9
%42 = call i64 @fread(i8* %40, i32 1, i32 64, i32* %41)
store i64 %42, i64* %11
%43 = load i64, i64* %11
%44 = load i64, i64* %10
%45 = add i64 %44, %43
store i64 %45, i64* %10
%46 = load i64, i64* %10
%47 = load i64, i64* %7
%48 = icmp ugt i64 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %37
%50 = load i64*, i64** %8
store i64 4294967295, i64* %50
%51 = load i32*, i32** %9
%52 = call i32 @fclose(i32* %51)
br label %66
53: ; preds = %37
br label %32
54: ; preds = %32
%55 = load i64, i64* %10
%56 = load i64*, i64** %8
store i64 %55, i64* %56
%57 = load i8*, i8** %6
%58 = load i64, i64* %7
%59 = sub i64 %58, 1
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 0, i8* %60
%61 = load i8*, i8** %6
%62 = load i64, i64* %10
%63 = getelementptr inbounds i8, i8* %61, i64 %62
store i8 0, i8* %63
%64 = load i32*, i32** %9
%65 = call i32 @fclose(i32* %64)
br label %66
66: ; preds = %26, %49, %54, %4
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"Failed to open %s for reading\0A\00"
define void @read_input(i8* %0, i8* nocapture %1, i64 %2, i64* nocapture writeonly %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %35, label %6
6: ; preds = %4
%7 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = load i32*, i32** @stdin
br label %13
11: ; preds = %6
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %9
%14 = phi i32* [ %10, %9 ], [ %12, %11 ]
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0)
store i64 0, i64* %3
br label %35
19: ; preds = %13, %23
%20 = phi i64 [ %26, %23 ], [ 0, %13 ]
%21 = tail call i32 @feof(i32* %14)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = getelementptr inbounds i8, i8* %1, i64 %20
%25 = tail call i64 @fread(i8* %24, i32 1, i32 64, i32* %14)
%26 = add i64 %25, %20
%27 = icmp ugt i64 %26, %2
br i1 %27, label %28, label %19
28: ; preds = %23
store i64 4294967295, i64* %3
%29 = tail call i32 @fclose(i32* %14)
br label %35
30: ; preds = %19
store i64 %20, i64* %3
%31 = add i64 %2, -1
%32 = getelementptr inbounds i8, i8* %1, i64 %31
store i8 0, i8* %32
%33 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 0, i8* %33
%34 = tail call i32 @fclose(i32* %14)
br label %35
35: ; preds = %30, %28, %4, %16
ret void
}