#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 */

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  int fd1, fd2;
  char buf[1024];
  fd1 = open("1file", O_RDONLY);
  if (fd1 < 0) {
    perror("open():fd1");
    exit(1);
  }
  fd2 = open("2file", O_WRONLY | O_CREAT, 0600);
  if (fd2 < 0) {
    perror("open():fd2");
    close(fd1);
    exit(1);
  }
  int len, ret, pos;
  while ((len = read(fd1, buf, 1024)) != 0) {
    if (len < 0) {
      perror("read()");
      exit(2);
    }
    pos = 0;
    while (len > 0) {
      ret = write(fd2, buf + pos, len);
      if (ret < 0) {
        perror("write()");
        close(fd1);
        close(fd2);
        exit(2);
      }
      len -= ret;
      pos += ret;
    }
  }
  close(fd1);
  close(fd2);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ac <main+0x4c>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
or     (%rax),%esi
lea    0xe4c(%rip),%rdi
mov    $0x180,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11fc <main+0x9c>
lea    0xe32(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1201 <main+0xa1>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
je     12e6 <main+0x186>
cmpl   $0x0,-0x414(%rbp)
jge    1247 <main+0xe7>
lea    0xdea(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
movl   $0x0,-0x41c(%rbp)
cmpl   $0x0,-0x414(%rbp)
jle    12e1 <main+0x181>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
movslq -0x41c(%rbp),%rax
add    %rax,%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jge    12b6 <main+0x156>
lea    0xd92(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x418(%rbp),%ecx
mov    -0x414(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x414(%rbp)
mov    -0x418(%rbp),%eax
add    -0x41c(%rbp),%eax
mov    %eax,-0x41c(%rbp)
jmp    1251 <main+0xf1>
jmp    1201 <main+0xa1>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
jns    11a0 <main+0x40>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%r15d
mov    0x2e1e(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe55(%rip),%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
jns    11ea <main+0x8a>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%r12d
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    1218 <main+0xb8>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r12d,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    %eax,%ebx
mov    %rsp,%r14
nopl   (%rax)
test   %ebx,%ebx
js     1289 <main+0x129>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
mov    %r12d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1263 <main+0x103>
add    %eax,%ebp
sub    %eax,%ebx
jg     1230 <main+0xd0>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1220 <main+0xc0>
jmp    1201 <main+0xa1>
lea    0xdbf(%rip),%rdi
call   1070 <perror@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xd92(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"1file\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"open():fd1\00"
@.str.2 = [6 x i8] c"2file\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [11 x i8] c"open():fd2\00"
@.str.4 = [7 x i8] c"read()\00"
@.str.5 = [8 x i8] c"write()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @O_RDONLY
%9 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 384)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %15
br label %28
28: ; preds = %67, %27
%29 = load i32, i32* %2
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 1024)
store i32 %31, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %68
33: ; preds = %28
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 2)
unreachable
39: ; preds = %33
store i32 0, i32* %7
br label %40
40: ; preds = %60, %39
%41 = load i32, i32* %5
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %67
43: ; preds = %40
%44 = load i32, i32* %3
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32, i32* %5
%50 = call i32 @write(i32 %44, i8* %48, i32 %49)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %43
%54 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32, i32* %2
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
%59 = call i32 @exit(i32 2)
unreachable
60: ; preds = %43
%61 = load i32, i32* %6
%62 = load i32, i32* %5
%63 = sub nsw i32 %62, %61
store i32 %63, i32* %5
%64 = load i32, i32* %6
%65 = load i32, i32* %7
%66 = add nsw i32 %65, %64
store i32 %66, i32* %7
br label %40
67: ; preds = %40
br label %28
68: ; preds = %28
%69 = load i32, i32* %2
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* %3
%72 = call i32 @close(i32 %71)
%73 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"1file\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"open():fd1\00"
@.str.2 = [6 x i8] c"2file\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [11 x i8] c"open():fd2\00"
@.str.4 = [7 x i8] c"read()\00"
@.str.5 = [8 x i8] c"write()\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 384)
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = call i32 @read(i32 %4, i8* %2, i32 1024)
%17 = icmp eq i32 %16, 0
br i1 %17, label %47, label %25
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @close(i32 %4)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %43
%23 = call i32 @read(i32 %4, i8* %2, i32 1024)
%24 = icmp eq i32 %23, 0
br i1 %24, label %47, label %25
25: ; preds = %15, %22
%26 = phi i32 [ %23, %22 ], [ %16, %15 ]
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%30 = tail call i32 @exit(i32 2)
unreachable
31: ; preds = %25, %43
%32 = phi i32 [ %45, %43 ], [ 0, %25 ]
%33 = phi i32 [ %44, %43 ], [ %26, %25 ]
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %34
%36 = call i32 @write(i32 %13, i8* %35, i32 %33)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %31
%39 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%40 = tail call i32 @close(i32 %4)
%41 = tail call i32 @close(i32 %13)
%42 = tail call i32 @exit(i32 2)
unreachable
43: ; preds = %31
%44 = sub nsw i32 %33, %36
%45 = add nsw i32 %36, %32
%46 = icmp sgt i32 %44, 0
br i1 %46, label %31, label %22
47: ; preds = %22, %15
%48 = tail call i32 @close(i32 %4)
%49 = tail call i32 @close(i32 %13)
%50 = tail call i32 @exit(i32 0)
unreachable
}