#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, scalar_t__);
int main(int argc, char **argv) {
  int fd;
  ssize_t read_bytes;
  ssize_t written_bytes;
  char buffer[64];
  if (argc < 2) {
    fprintf(stderr, "Too few arguments\n");
    exit(1);
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Cannot open file\n");
    exit(1);
  }
  while ((read_bytes = read(fd, buffer, 64)) > 0) {
    written_bytes = write(1, buffer, read_bytes);
    if (written_bytes != read_bytes) {
      fprintf(stderr, "Cannot write\n");
      exit(1);
    }
  }
  if (read_bytes < 0) {
    fprintf(stderr, "myread: Cannot read file\n");
    exit(1);
  }
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119f <main+0x3f>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e1 <main+0x81>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11e6 <main+0x86>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jle    124d <main+0xed>
lea    -0x70(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1248 <main+0xe8>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11e6 <main+0x86>
cmpq   $0x0,-0x20(%rbp)
jge    1277 <main+0x117>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
cmp    $0x1,%edi
jg     1182 <main+0x22>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
jmp    1201 <main+0xa1>
mov    0x8(%rsi),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
jns    11aa <main+0x4a>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
jmp    1201 <main+0xa1>
mov    %eax,%r14d
mov    %rsp,%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11ef <main+0x8f>
mov    %rax,%rbp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
je     11b0 <main+0x50>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
jmp    1201 <main+0xa1>
jns    1210 <main+0xb0>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Too few arguments\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"Cannot open file\0A\00"
@.str.2 = [14 x i8] c"Cannot write\0A\00"
@.str.3 = [26 x i8] c"myread: Cannot read file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca [64 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
br label %29
29: ; preds = %45, %28
%30 = load i32, i32* %6
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%32 = call i64 @read(i32 %30, i8* %31, i32 64)
store i64 %32, i64* %7
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %29
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%36 = load i64, i64* %7
%37 = call i64 @write(i32 1, i8* %35, i64 %36)
store i64 %37, i64* %8
%38 = load i64, i64* %8
%39 = load i64, i64* %7
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %45
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %34
br label %29
46: ; preds = %29
%47 = load i64, i64* %7
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %46
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
%56 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Too few arguments\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"Cannot open file\0A\00"
@.str.2 = [14 x i8] c"Cannot write\0A\00"
@.str.3 = [26 x i8] c"myread: Cannot read file\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10, %23
%21 = call i64 @read(i32 %14, i8* %4, i32 64)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = call i64 @write(i32 1, i8* %4, i64 %21)
%25 = icmp eq i64 %24, %21
br i1 %25, label %20, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = icmp slt i64 %21, 0
br i1 %31, label %32, label %36
32: ; preds = %30
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = tail call i32 @close(i32 %14)
%38 = tail call i32 @exit(i32 0)
unreachable
}