#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*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, 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, char *, int);
int main(int argc, char *argv[]) {
  int fd1, fd2;
  FILE *fp;
  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);
  }
  fp = fopen(argv[2], "w");
  if (fp == NULL) {
    perror("fail to read");
    exit(1);
  }
  fd2 = open("test.txt", O_WRONLY);
  if (fd2 == -1) {
    perror("fail to read");
    exit(1);
  }
  rest = read(fd1, buf, 100000);
  printf("get %d bytes from %s\n", rest, argv[1]);
  while (rest > 0) {
    errno = 0;
    n = write(fd2, p, rest);
    fprintf(fp, "write %d, errno %s\n", n, strerror(errno));
    if (rest > 0) {
      p += n;
      rest -= n;
    }
  }
  printf("done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x18ae0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x186c0(%rbp),%rax
mov    %rax,-0x186d0(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d9 <main+0x49>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1212 <main+0x82>
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdf9(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    124b <main+0xbb>
lea    0xdd1(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
lea    0xdc1(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1283 <main+0xf3>
lea    0xd99(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x186c0(%rbp),%rsi
mov    $0x186a0,%edx
call   1050 <read@plt>
mov    %eax,-0x186c8(%rbp)
mov    -0x186c8(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x186c8(%rbp)
jle    1371 <main+0x1e1>
mov    0x2d0b(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%edi
mov    -0x186d0(%rbp),%rsi
mov    -0x186c8(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x186c4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18ae0(%rbp)
mov    -0x186c4(%rbp),%eax
mov    %eax,-0x18ad4(%rbp)
mov    0x2ccb(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x18ae0(%rbp),%rdi
mov    -0x18ad4(%rbp),%edx
mov    %rax,%rcx
lea    0xd0f(%rip),%rsi
call   1060 <fprintf@plt>
cmpl   $0x0,-0x186c8(%rbp)
jle    136c <main+0x1dc>
mov    -0x186c4(%rbp),%ecx
mov    -0x186d0(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x186d0(%rbp)
mov    -0x186c4(%rbp),%ecx
mov    -0x186c8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x186c8(%rbp)
jmp    12b9 <main+0x129>
lea    0xcd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18ae0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x186a8,%rsp
cmp    $0x3,%edi
jne    12c6 <main+0x126>
mov    %rsi,%r12
mov    0x8(%rsi),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12b0 <main+0x110>
mov    %eax,%ebp
mov    0x10(%r12),%rdi
lea    0xe27(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12b0 <main+0x110>
mov    %rax,%r14
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    0xe08(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12b0 <main+0x110>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x186a0,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    0x8(%r12),%rdx
lea    0xde0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
jle    1290 <main+0xf0>
mov    0x2d8c(%rip),%r12
nopl   0x0(%rax)
movq   $0x0,(%r12)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %eax,%r13d
mov    (%r12),%rdi
call   10c0 <strerror@plt>
mov    %r14,%rdi
lea    0xdb3(%rip),%rsi
mov    %r13d,%edx
mov    %rax,%rcx
call   1070 <fprintf@plt>
movslq %r13d,%rax
add    %rax,%rbx
sub    %eax,%ebp
jg     1250 <main+0xb0>
lea    0xdab(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x186a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd49(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd7a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <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"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [9 x i8] c"test.txt\00"
@O_WRONLY = global i32 0
@.str.4 = [22 x i8] c"get %d bytes from %s\0A\00"
@errno = global i64 0
@.str.5 = [20 x i8] c"write %d, errno %s\0A\00"
@.str.6 = [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 [100000 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = getelementptr inbounds [100000 x i8], [100000 x i8]* %9, i64 0, i64 0
store i8* %14, i8** %12
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %35, i32** %8
%36 = load i32*, i32** %8
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %31
%42 = load i32, i32* @O_WRONLY
%43 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %42)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
%50 = load i32, i32* %6
%51 = getelementptr inbounds [100000 x i8], [100000 x i8]* %9, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 100000)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %53, i8* %56)
br label %58
58: ; preds = %81, %49
%59 = load i32, i32* %11
%60 = icmp sgt i32 %59, 0
br i1 %60, label %61, label %82
61: ; preds = %58
store i64 0, i64* @errno
%62 = load i32, i32* %7
%63 = load i8*, i8** %12
%64 = load i32, i32* %11
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
store i32 %65, i32* %10
%66 = load i32*, i32** %8
%67 = load i32, i32* %10
%68 = load i64, i64* @errno
%69 = call i8* @strerror(i64 %68)
%70 = call i32 @fprintf(i32* %66, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %67, i8* %69)
%71 = load i32, i32* %11
%72 = icmp sgt i32 %71, 0
br i1 %72, label %73, label %81
73: ; preds = %61
%74 = load i32, i32* %10
%75 = load i8*, i8** %12
%76 = sext i32 %74 to i64
%77 = getelementptr inbounds i8, i8* %75, i64 %76
store i8* %77, i8** %12
%78 = load i32, i32* %10
%79 = load i32, i32* %11
%80 = sub nsw i32 %79, %78
store i32 %80, i32* %11
br label %81
81: ; preds = %73, %61
br label %58
82: ; preds = %58
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fail to read\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [9 x i8] c"test.txt\00"
@O_WRONLY = global i32 0
@.str.4 = [22 x i8] c"get %d bytes from %s\0A\00"
@errno = global i64 0
@.str.5 = [20 x i8] c"write %d, errno %s\0A\00"
@str = [5 x i8] c"done\00"
@str.7 = [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.7, 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 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* @O_WRONLY
%28 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = call i32 @read(i32 %13, i8* %4, i32 100000)
%35 = load i8*, i8** %10
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %34, i8* %35)
%37 = icmp sgt i32 %34, 0
br i1 %37, label %38, label %49
38: ; preds = %33, %38
%39 = phi i32 [ %45, %38 ], [ %34, %33 ]
%40 = phi i8* [ %47, %38 ], [ %4, %33 ]
store i64 0, i64* @errno
%41 = call i32 @write(i32 %28, i8* %40, i32 %39)
%42 = load i64, i64* @errno
%43 = tail call i8* @strerror(i64 %42)
%44 = tail call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %41, i8* %43)
%45 = sub nsw i32 %39, %41
%46 = sext i32 %41 to i64
%47 = getelementptr inbounds i8, i8* %40, i64 %46
%48 = icmp sgt i32 %45, 0
br i1 %48, label %38, label %49
49: ; preds = %38, %33
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
ret i32 0
}