#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 */

/* Variables and functions */
int /*<<< orphan*/ assign_wrkmem(unsigned char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int /*<<< orphan*/ write(int, ...);
int main(void) {
  int fd;
  unsigned char buffer[2048];
  int count;
  unsigned char wrkmem[65536];
  assign_wrkmem(wrkmem);
  write(1, "dcload console test program\n\n", 30);
  fd = open("console-test.c", 0);
  if (fd == -1) {
    write(1, "open failed!\n", 14);
    exit(1);
  }
  write(1, "Contents of console-test.c:\n\n", 30);
  count = read(fd, buffer, 2048);
  if (count == -1) {
    write(1, "read failed!\n", 14);
    exit(1);
  }
  write(1, buffer, count);
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10820,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10820(%rbp),%rdi
call   1060 <assign_wrkmem@plt>
mov    $0x1,%edi
lea    0xe76(%rip),%rsi
mov    $0x1e,%edx
mov    $0x0,%al
call   1030 <write@plt>
lea    0xe81(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d3 <main+0x73>
mov    $0x1,%edi
lea    0xe70(%rip),%rsi
mov    $0xe,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
lea    0xe5c(%rip),%rsi
mov    $0x1e,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1050 <read@plt>
mov    %eax,-0x814(%rbp)
cmpl   $0xffffffff,-0x814(%rbp)
jne    1234 <main+0xd4>
mov    $0x1,%edi
lea    0xe3b(%rip),%rsi
mov    $0xe,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x810(%rbp),%rsi
mov    -0x814(%rbp),%edx
mov    $0x1,%edi
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10800,%rsp
lea    0x800(%rsp),%rdi
call   1060 <assign_wrkmem@plt>
lea    0xe84(%rip),%rsi
mov    $0x1,%edi
mov    $0x1e,%edx
xor    %eax,%eax
call   1030 <write@plt>
lea    0xe8a(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11a9 <main+0x49>
lea    0xe86(%rip),%rsi
jmp    11de <main+0x7e>
mov    %eax,%ebx
lea    0xe89(%rip),%rsi
mov    $0x1,%edi
mov    $0x1e,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x800,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11f9 <main+0x99>
lea    0xe7b(%rip),%rsi
mov    $0x1,%edi
mov    $0xe,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"dcload console test program\0A\0A\00"
@.str.1 = [15 x i8] c"console-test.c\00"
@.str.2 = [14 x i8] c"open failed!\0A\00"
@.str.3 = [30 x i8] c"Contents of console-test.c:\0A\0A\00"
@.str.4 = [14 x i8] c"read failed!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2048 x i8]
%4 = alloca i32
%5 = alloca [65536 x i8]
store i32 0, i32* %1
%6 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
%7 = call i32 @assign_wrkmem(i8* %6)
%8 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 30)
%9 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 14)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 30)
%17 = load i32, i32* %2
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 2048)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 14)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%27 = load i32, i32* %4
%28 = call i32 (i32, ...) @write(i32 1, i8* %26, i32 %27)
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
%31 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [30 x i8] c"dcload console test program\0A\0A\00"
@.str.1 = [15 x i8] c"console-test.c\00"
@.str.2 = [14 x i8] c"open failed!\0A\00"
@.str.3 = [30 x i8] c"Contents of console-test.c:\0A\0A\00"
@.str.4 = [14 x i8] c"read failed!\0A\00"
define i32 @main() {
%1 = alloca [2048 x i8]
%2 = alloca [65536 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%5 = call i32 @assign_wrkmem(i8* %4)
%6 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 30)
%7 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 14)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 30)
%14 = call i32 @read(i32 %7, i8* %3, i32 2048)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 14)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = call i32 (i32, ...) @write(i32 1, i8* %3, i32 %14)
%21 = call i32 @close(i32 %7)
%22 = call i32 @exit(i32 0)
unreachable
}