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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ STDOUT_FILENO;
int atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int fd;
  char buf[1024];
  int nRead, nWrite;
  if (argc != 2) {
    printf("Bad arguments\n");
    exit(1);
  }
  fd = atoi(argv[1]);
  while ((nRead = read(fd, buf, 1024)) != 0) {
    if (nRead > 0) {
      ssize_t nWritten = 0;
      while (nWritten < nRead) {
        nWrite = write(STDOUT_FILENO, buf + nWritten, nRead - nWritten);
        if (nWrite < 0) {
          perror("printf failed");
          exit(1);
        }
        nWritten += nWrite;
      }
    } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
      perror("read failed");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1199 <main+0x39>
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
je     12d3 <main+0x173>
cmpl   $0x0,-0x424(%rbp)
jle    1265 <main+0x105>
movl   $0x0,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x424(%rbp),%eax
jge    1260 <main+0x100>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
movslq -0x42c(%rbp),%rax
add    %rax,%rsi
mov    -0x424(%rbp),%edx
sub    -0x42c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jge    1249 <main+0xe9>
lea    0xdd5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x428(%rbp),%eax
add    -0x42c(%rbp),%eax
mov    %eax,-0x42c(%rbp)
jmp    11e3 <main+0x83>
jmp    12ce <main+0x16e>
mov    0x2d6c(%rip),%rax
mov    (%rax),%rax
mov    0x2d42(%rip),%rcx
cmp    (%rcx),%rax
je     12c9 <main+0x169>
mov    0x2d52(%rip),%rax
mov    (%rax),%rax
mov    0x2d40(%rip),%rcx
cmp    (%rcx),%rax
je     12c9 <main+0x169>
mov    0x2d38(%rip),%rax
mov    (%rax),%rax
mov    0x2d1e(%rip),%rcx
cmp    (%rcx),%rax
je     12c9 <main+0x169>
lea    0xd63(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12ce <main+0x16e>
jmp    11a9 <main+0x49>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
cmp    $0x2,%edi
jne    125b <main+0xfb>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1231 <main+0xd1>
mov    %eax,%ebp
mov    0x2e4a(%rip),%rbx
mov    %rsp,%r14
mov    0x2e08(%rip),%r13
jmp    11d6 <main+0x76>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1231 <main+0xd1>
test   %ebp,%ebp
jle    1200 <main+0xa0>
xor    %r12d,%r12d
nopl   (%rax)
mov    (%rbx),%edi
movslq %r12d,%rax
lea    (%rsp,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1245 <main+0xe5>
add    %eax,%r12d
cmp    %r12d,%ebp
jg     11e0 <main+0x80>
jmp    11c0 <main+0x60>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     11c0 <main+0x60>
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <main+0x60>
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <main+0x60>
lea    0xddf(%rip),%rdi
jmp    124c <main+0xec>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdb8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Bad arguments\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [14 x i8] c"printf failed\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
@.str.2 = [12 x i8] c"read failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 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 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
br label %21
21: ; preds = %70, %16
%22 = load i32, i32* %6
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 1024)
store i32 %24, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %71
26: ; preds = %21
%27 = load i32, i32* %8
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %54
29: ; preds = %26
store i32 0, i32* %10
br label %30
30: ; preds = %49, %29
%31 = load i32, i32* %10
%32 = load i32, i32* %8
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %53
34: ; preds = %30
%35 = load i32, i32* @STDOUT_FILENO
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i32, i32* %8
%41 = load i32, i32* %10
%42 = sub nsw i32 %40, %41
%43 = call i32 @write(i32 %35, i8* %39, i32 %42)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %34
%47 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %34
%50 = load i32, i32* %9
%51 = load i32, i32* %10
%52 = add nsw i32 %51, %50
store i32 %52, i32* %10
br label %30
53: ; preds = %30
br label %70
54: ; preds = %26
%55 = load i64, i64* @errno
%56 = load i64, i64* @EAGAIN
%57 = icmp ne i64 %55, %56
br i1 %57, label %58, label %69
58: ; preds = %54
%59 = load i64, i64* @errno
%60 = load i64, i64* @EWOULDBLOCK
%61 = icmp ne i64 %59, %60
br i1 %61, label %62, label %69
62: ; preds = %58
%63 = load i64, i64* @errno
%64 = load i64, i64* @EINTR
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %62, %58, %54
br label %70
70: ; preds = %69, %53
br label %21
71: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str.1 = [14 x i8] c"printf failed\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
@.str.2 = [12 x i8] c"read failed\00"
@str = [14 x i8] c"Bad arguments\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 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 = tail call i32 @atoi(i8* %11)
%13 = call i32 @read(i32 %12, i8* %4, i32 1024)
%14 = icmp eq i32 %13, 0
br i1 %14, label %48, label %15
15: ; preds = %9, %45
%16 = phi i32 [ %46, %45 ], [ %13, %9 ]
%17 = icmp sgt i32 %16, 0
br i1 %17, label %20, label %32
18: ; preds = %20
%19 = icmp sgt i32 %16, %28
br i1 %19, label %20, label %45
20: ; preds = %15, %18
%21 = phi i32 [ %28, %18 ], [ 0, %15 ]
%22 = load i32, i32* @STDOUT_FILENO
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %23
%25 = sub nsw i32 %16, %21
%26 = call i32 @write(i32 %22, i8* %24, i32 %25)
%27 = icmp slt i32 %26, 0
%28 = add nsw i32 %26, %21
br i1 %27, label %29, label %18
29: ; preds = %20
%30 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %15
%33 = load i64, i64* @errno
%34 = load i64, i64* @EAGAIN
%35 = icmp eq i64 %33, %34
%36 = load i64, i64* @EWOULDBLOCK
%37 = icmp eq i64 %33, %36
%38 = select i1 %35, i1 true, i1 %37
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %33, %39
%41 = select i1 %38, i1 true, i1 %40
br i1 %41, label %45, label %42
42: ; preds = %32
%43 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%44 = tail call i32 @exit(i32 1)
unreachable
45: ; preds = %18, %32
%46 = call i32 @read(i32 %12, i8* %4, i32 1024)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %15
48: ; preds = %45, %9
ret i32 0
}