#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(scalar_t__);
int write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  int fd1, fd2;
  FILE *fp;
  int flags;
  char buf[100000];
  int n, rest;
  char *p = buf;
  char content[1024];
  if (argc != 3) {
    printf("expect args\n");
    exit(1);
  }
  fd1 = open(argv[1], O_RDONLY);
  if (fd1 == -1) {
    perror("fail to read");
    exit(1);
  }
  fd2 = open(argv[2], O_WRONLY);
  if (fd2 == -1) {
    perror("fail to read");
    exit(1);
  }
  fp = fdopen(fd2, "w");
  if (fp == NULL) {
    perror("fail to open");
    exit(1);
  }
  flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
  if (flags == -1) {
    perror("fail to fcntl");
    exit(1);
  }
  flags |= O_NONBLOCK;
  if (fcntl(STDOUT_FILENO, F_SETFL, 0) == -1) {
    perror("fail to fcntl");
    exit(1);
  }
  rest = read(fd1, buf, 100000);
  printf("get %d bytes from %s\n", rest, argv[1]);
  while (rest > 0) {
    errno = 0;
    n = write(STDOUT_FILENO, p, rest);
    fprintf(fp, "write %d, errno %s\n", n, strerror(errno));
    if (rest > 0) {
      p += n;
      rest -= n;
    }
  }
  printf("done\n");
  close(fd1);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x18af0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x186d0(%rbp),%rax
mov    %rax,-0x186e0(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1209 <main+0x49>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1242 <main+0x82>
lea    0xdda(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    127b <main+0xbb>
lea    0xda1(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    0xd95(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12af <main+0xef>
lea    0xd7c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12eb <main+0x12b>
lea    0xd4d(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
mov    0x2cbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1332 <main+0x172>
lea    0xd06(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x186d0(%rbp),%rsi
mov    $0x186a0,%edx
call   1080 <read@plt>
mov    %eax,-0x186d8(%rbp)
mov    -0x186d8(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xcd6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x186d8(%rbp)
jle    1426 <main+0x266>
mov    0x2c44(%rip),%rax
movq   $0x0,(%rax)
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
mov    -0x186e0(%rbp),%rsi
mov    -0x186d8(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x186d4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18af0(%rbp)
mov    -0x186d4(%rbp),%eax
mov    %eax,-0x18ae4(%rbp)
mov    0x2bfe(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x18af0(%rbp),%rdi
mov    -0x18ae4(%rbp),%edx
mov    %rax,%rcx
lea    0xc6c(%rip),%rsi
call   1090 <fprintf@plt>
cmpl   $0x0,-0x186d8(%rbp)
jle    1421 <main+0x261>
mov    -0x186d4(%rbp),%ecx
mov    -0x186e0(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x186e0(%rbp)
mov    -0x186d4(%rbp),%ecx
mov    -0x186d8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x186d8(%rbp)
jmp    1368 <main+0x1a8>
lea    0xc34(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x18af0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x186b8,%rsp
cmp    $0x3,%edi
jne    1348 <main+0x178>
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1336 <main+0x166>
mov    %eax,%ebx
mov    0x10(%r15),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1336 <main+0x166>
lea    0xde1(%rip),%rsi
mov    %eax,%edi
call   10b0 <fdopen@plt>
test   %rax,%rax
je     135e <main+0x18e>
mov    %rax,%r12
mov    0x2daa(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     133f <main+0x16f>
mov    0x0(%r13),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     133f <main+0x16f>
lea    0x10(%rsp),%rbp
mov    %ebx,0xc(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x186a0,%edx
call   1090 <read@plt>
mov    %eax,%ebx
mov    0x8(%r15),%rdx
lea    0xd85(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebx,%ebx
jle    1305 <main+0x135>
mov    0x2d0f(%rip),%r14
nopl   0x0(%rax)
movq   $0x0,(%r14)
mov    0x0(%r13),%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1050 <write@plt>
mov    %r13,%r15
mov    %eax,%r13d
mov    (%r14),%rdi
call   10f0 <strerror@plt>
mov    %r12,%rdi
lea    0xd53(%rip),%rsi
mov    %r13d,%edx
mov    %rax,%rcx
call   10a0 <fprintf@plt>
movslq %r13d,%rax
mov    %r15,%r13
add    %rax,%rbp
sub    %eax,%ebx
jg     12c0 <main+0xf0>
lea    0xd48(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x186b8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcc3(%rip),%rdi
jmp    1365 <main+0x195>
lea    0xcd6(%rip),%rdi
jmp    1365 <main+0x195>
lea    0xd0a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xcaa(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"expect args\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fail to read\00"
@O_WRONLY = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [13 x i8] c"fail to open\00"
@STDOUT_FILENO = global i32 0
@F_GETFL = global i32 0
@.str.4 = [14 x i8] c"fail to fcntl\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.5 = [22 x i8] c"get %d bytes from %s\0A\00"
@errno = global i64 0
@.str.6 = [20 x i8] c"write %d, errno %s\0A\00"
@.str.7 = [6 x i8] c"done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca [100000 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = getelementptr inbounds [100000 x i8], [100000 x i8]* %10, i64 0, i64 0
store i8* %15, i8** %13
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_WRONLY
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
%44 = load i32, i32* %7
%45 = call i32* @fdopen(i32 %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %45, i32** %8
%46 = load i32*, i32** %8
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = load i32, i32* @STDOUT_FILENO
%53 = load i32, i32* @F_GETFL
%54 = call i32 @fcntl(i32 %52, i32 %53, i32 0)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %51
%61 = load i32, i32* @O_NONBLOCK
%62 = load i32, i32* %9
%63 = or i32 %62, %61
store i32 %63, i32* %9
%64 = load i32, i32* @STDOUT_FILENO
%65 = load i32, i32* @F_SETFL
%66 = call i32 @fcntl(i32 %64, i32 %65, i32 0)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %60
%69 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %60
%72 = load i32, i32* %6
%73 = getelementptr inbounds [100000 x i8], [100000 x i8]* %10, i64 0, i64 0
%74 = call i32 @read(i32 %72, i8* %73, i32 100000)
store i32 %74, i32* %12
%75 = load i32, i32* %12
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %75, i8* %78)
br label %80
80: ; preds = %103, %71
%81 = load i32, i32* %12
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %104
83: ; preds = %80
store i64 0, i64* @errno
%84 = load i32, i32* @STDOUT_FILENO
%85 = load i8*, i8** %13
%86 = load i32, i32* %12
%87 = call i32 @write(i32 %84, i8* %85, i32 %86)
store i32 %87, i32* %11
%88 = load i32*, i32** %8
%89 = load i32, i32* %11
%90 = load i64, i64* @errno
%91 = call i8* @strerror(i64 %90)
%92 = call i32 @fprintf(i32* %88, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %89, i8* %91)
%93 = load i32, i32* %12
%94 = icmp sgt i32 %93, 0
br i1 %94, label %95, label %103
95: ; preds = %83
%96 = load i32, i32* %11
%97 = load i8*, i8** %13
%98 = sext i32 %96 to i64
%99 = getelementptr inbounds i8, i8* %97, i64 %98
store i8* %99, i8** %13
%100 = load i32, i32* %11
%101 = load i32, i32* %12
%102 = sub nsw i32 %101, %100
store i32 %102, i32* %12
br label %103
103: ; preds = %95, %83
br label %80
104: ; preds = %80
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%106 = load i32, i32* %6
%107 = call i32 @close(i32 %106)
%108 = load i32*, i32** %8
%109 = call i32 @fclose(i32* %108)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fail to read\00"
@O_WRONLY = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [13 x i8] c"fail to open\00"
@STDOUT_FILENO = global i32 0
@F_GETFL = global i32 0
@.str.4 = [14 x i8] c"fail to fcntl\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.5 = [22 x i8] c"get %d bytes from %s\0A\00"
@errno = global i64 0
@.str.6 = [20 x i8] c"write %d, errno %s\0A\00"
@str = [5 x i8] c"done\00"
@str.8 = [12 x i8] c"expect args\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100000 x i8]
%4 = getelementptr inbounds [100000 x i8], [100000 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, 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 eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %9
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_WRONLY
%22 = tail call i32 @open(i8* %20, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = tail call i32* @fdopen(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = load i32, i32* @STDOUT_FILENO
%35 = load i32, i32* @F_GETFL
%36 = tail call i32 @fcntl(i32 %34, i32 %35, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %33
%42 = load i32, i32* @STDOUT_FILENO
%43 = load i32, i32* @F_SETFL
%44 = tail call i32 @fcntl(i32 %42, i32 %43, i32 0)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %41
%50 = call i32 @read(i32 %13, i8* %4, i32 100000)
%51 = load i8*, i8** %10
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %50, i8* %51)
%53 = icmp sgt i32 %50, 0
br i1 %53, label %54, label %66
54: ; preds = %49, %54
%55 = phi i8* [ %64, %54 ], [ %4, %49 ]
%56 = phi i32 [ %62, %54 ], [ %50, %49 ]
store i64 0, i64* @errno
%57 = load i32, i32* @STDOUT_FILENO
%58 = call i32 @write(i32 %57, i8* %55, i32 %56)
%59 = load i64, i64* @errno
%60 = tail call i8* @strerror(i64 %59)
%61 = tail call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %58, i8* %60)
%62 = sub nsw i32 %56, %58
%63 = sext i32 %58 to i64
%64 = getelementptr inbounds i8, i8* %55, i64 %63
%65 = icmp sgt i32 %62, 0
br i1 %65, label %54, label %66
66: ; preds = %54, %49
%67 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%68 = tail call i32 @close(i32 %13)
%69 = tail call i32 @fclose(i32* %28)
ret i32 0
}