#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 /*<<< orphan*/ SHUT_RD;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int write(int, char *, int);
void com(int src, int dst) {
  char buf[1024 * 4];
  int r, i, j;
  r = read(src, buf, 1024 * 4);
  while (r > 0) {
    i = 0;
    while (i < r) {
      j = write(dst, buf + i, r - i);
      if (j == -1) {
        perror("write");
        exit(1);
        ;
      }
      i += j;
    }
    r = read(src, buf, 1024 * 4);
  }
  if (r == -1) {
    perror("read");
    exit(1);
    ;
  }
  shutdown(src, SHUT_RD);
  shutdown(dst, SHUT_WR);
  close(src);
  close(dst);
  exit(0);
}


x86 assembly (clang, -O0):
<com>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jle    1238 <com+0xd8>
movl   $0x0,-0x1018(%rbp)
mov    -0x1018(%rbp),%eax
cmp    -0x1014(%rbp),%eax
jge    1219 <com+0xb9>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
movslq -0x1018(%rbp),%rax
add    %rax,%rsi
mov    -0x1014(%rbp),%edx
sub    -0x1018(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0xffffffff,-0x101c(%rbp)
jne    1202 <com+0xa2>
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x101c(%rbp),%eax
add    -0x1018(%rbp),%eax
mov    %eax,-0x1018(%rbp)
jmp    11a2 <com+0x42>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x1014(%rbp)
jmp    118b <com+0x2b>
cmpl   $0xffffffff,-0x1014(%rbp)
jne    125b <com+0xfb>
lea    0xdba(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    -0x8(%rbp),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<com>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %esi,%r12d
mov    %edi,%r14d
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11d4 <com+0x74>
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11e2 <com+0x82>
add    %eax,%ebx
cmp    %ebx,%ebp
jg     11a0 <com+0x40>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1190 <com+0x30>
cmp    $0xffffffff,%ebp
jne    11f8 <com+0x98>
lea    0xe26(%rip),%rdi
jmp    11e9 <com+0x89>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <shutdown@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1040 <shutdown@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@.str.1 = [5 x i8] c"read\00"
@SHUT_RD = global i32 0
@SHUT_WR = global i32 0
define void @com(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 4096)
store i32 %11, i32* %6
br label %12
12: ; preds = %39, %2
%13 = load i32, i32* %6
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %43
15: ; preds = %12
store i32 0, i32* %7
br label %16
16: ; preds = %35, %15
%17 = load i32, i32* %7
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %39
20: ; preds = %16
%21 = load i32, i32* %4
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = sub nsw i32 %26, %27
%29 = call i32 @write(i32 %21, i8* %25, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %20
%36 = load i32, i32* %8
%37 = load i32, i32* %7
%38 = add nsw i32 %37, %36
store i32 %38, i32* %7
br label %16
39: ; preds = %16
%40 = load i32, i32* %3
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 4096)
store i32 %42, i32* %6
br label %12
43: ; preds = %12
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %43
%50 = load i32, i32* %3
%51 = load i32, i32* @SHUT_RD
%52 = call i32 @shutdown(i32 %50, i32 %51)
%53 = load i32, i32* %4
%54 = load i32, i32* @SHUT_WR
%55 = call i32 @shutdown(i32 %53, i32 %54)
%56 = load i32, i32* %3
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %4
%59 = call i32 @close(i32 %58)
%60 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@.str.1 = [5 x i8] c"read\00"
@SHUT_RD = global i32 0
@SHUT_WR = global i32 0
define void @com(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 4096)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %25
7: ; preds = %2, %22
%8 = phi i32 [ %23, %22 ], [ %5, %2 ]
br label %11
9: ; preds = %11
%10 = icmp sgt i32 %8, %18
br i1 %10, label %11, label %22
11: ; preds = %7, %9
%12 = phi i32 [ %18, %9 ], [ 0, %7 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %13
%15 = sub nsw i32 %8, %12
%16 = call i32 @write(i32 %1, i8* %14, i32 %15)
%17 = icmp eq i32 %16, -1
%18 = add nsw i32 %16, %12
br i1 %17, label %19, label %9
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %9
%23 = call i32 @read(i32 %0, i8* %4, i32 4096)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %7, label %25
25: ; preds = %22, %2
%26 = phi i32 [ %5, %2 ], [ %23, %22 ]
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %25
%32 = load i32, i32* @SHUT_RD
%33 = tail call i32 @shutdown(i32 %0, i32 %32)
%34 = load i32, i32* @SHUT_WR
%35 = tail call i32 @shutdown(i32 %1, i32 %34)
%36 = tail call i32 @close(i32 %0)
%37 = tail call i32 @close(i32 %1)
%38 = tail call i32 @exit(i32 0)
unreachable
}