#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*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
scalar_t__ realloc(char *, int);
scalar_t__ write(int, char *, int);
int transport(int in, int out) {
  char *buf = NULL;
  int len = (4096);
  int count = -1;
  while (len == (4096)) {
    count++;
    if ((buf = (char *)realloc(buf, (count + 1) * (4096))) == NULL) {
      perror("realloc");
      exit(1);
    }
    if ((len = read(in, buf + count * (4096), (4096))) < 0) {
      perror("read");
      exit(1);
    }
  }
  len += count * (4096);
  if (len > 0 && write(out, buf, len) < 0) {
    perror("write");
    exit(1);
  }
  free(buf);
  return len;
}


x86 assembly (clang, -O0):
<transport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x1000,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
cmpl   $0x1000,-0x14(%rbp)
jne    1213 <transport+0xb3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
shl    $0xc,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11d0 <transport+0x70>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%eax
shl    $0xc,%eax
cltq
add    %rax,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120e <transport+0xae>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1184 <transport+0x24>
mov    -0x18(%rbp),%eax
shl    $0xc,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1258 <transport+0xf8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1258 <transport+0xf8>
lea    0xdc4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transport>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %edi,%r15d
mov    $0x1000,%ebp
mov    $0xfffff000,%ebx
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
cmp    $0x1000,%ebp
jne    11c6 <transport+0x66>
lea    0x2000(%rbx),%esi
mov    %r12,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     11f1 <transport+0x91>
mov    %rax,%r12
add    $0x1000,%ebx
movslq %ebx,%rsi
add    %rax,%rsi
mov    %r15d,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1180 <transport+0x20>
lea    0xe44(%rip),%rdi
jmp    11f8 <transport+0x98>
add    %ebx,%ebp
test   %ebp,%ebp
jle    11de <transport+0x7e>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
test   %rax,%rax
js     1207 <transport+0xa7>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe08(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdff(%rip),%rdi
jmp    11f8 <transport+0x98>

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

LLVM IR (-O2):
@.str = [8 x i8] c"realloc\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [6 x i8] c"write\00"
define i32 @transport(i32 %0, i32 %1) {
br label %3
3: ; preds = %19, %2
%4 = phi i64 [ %9, %19 ], [ -1, %2 ]
%5 = phi i8* [ %14, %19 ], [ null, %2 ]
%6 = phi i32 [ %24, %19 ], [ 4096, %2 ]
%7 = icmp eq i32 %6, 4096
br i1 %7, label %8, label %29
8: ; preds = %3
%9 = add i64 %4, 1
%10 = trunc i64 %4 to i32
%11 = shl i32 %10, 12
%12 = add i32 %11, 8192
%13 = tail call i64 @realloc(i8* %5, i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %19
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %8
%20 = trunc i64 %9 to i32
%21 = shl nsw i32 %20, 12
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %14, i64 %22
%24 = tail call i32 @read(i32 %0, i8* %23, i32 4096)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %3
26: ; preds = %19
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %3
%30 = trunc i64 %4 to i32
%31 = shl nsw i32 %30, 12
%32 = add nsw i32 %31, %6
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = tail call i64 @write(i32 %1, i8* %5, i32 %32)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %34, %29
%41 = tail call i32 @free(i8* %5)
ret i32 %32
}