#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*/);
int /*<<< orphan*/ putchar(char);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int fd;
  ssize_t ret;
  char ch;
  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 ((ret = read(fd, &ch, 1)) > 0) {
    putchar(ch);
  }
  if (ret < 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    $0x30,%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    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jle    1213 <main+0xb3>
movsbl -0x21(%rbp),%edi
call   1030 <putchar@plt>
jmp    11e6 <main+0x86>
cmpq   $0x0,-0x20(%rbp)
jge    123d <main+0xdd>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%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   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     117b <main+0x1b>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
jmp    11f3 <main+0x93>
mov    0x8(%rsi),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
jns    11a3 <main+0x43>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
jmp    11f3 <main+0x93>
mov    %eax,%ebx
lea    0x7(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11de <main+0x7e>
lea    0x7(%rsp),%r14
movsbl 0x7(%rsp),%edi
call   1030 <putchar@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jg     11c0 <main+0x60>
test   %rax,%rax
jns    1202 <main+0xa2>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %ebx,%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 = [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 i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %15
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %15
br label %28
28: ; preds = %32, %27
%29 = load i32, i32* %6
%30 = call i64 @read(i32 %29, i8* %8, i32 1)
store i64 %30, i64* %7
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8, i8* %8
%34 = call i32 @putchar(i8 signext %33)
br label %28
35: ; preds = %28
%36 = load i64, i64* %7
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = 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 = [26 x i8] c"myread: Cannot read file\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = call i64 @read(i32 %13, i8* %3, i32 1)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %22, label %27
18: ; preds = %9
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %15, %22
%23 = load i8, i8* %3
%24 = tail call i32 @putchar(i8 signext %23)
%25 = call i64 @read(i32 %13, i8* %3, i32 1)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %22, label %27
27: ; preds = %22, %15
%28 = phi i64 [ %16, %15 ], [ %25, %22 ]
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = tail call i32 @close(i32 %13)
%36 = tail call i32 @exit(i32 0)
unreachable
}