#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
void cat(int fd) {
  char *buffer = (char *)calloc(sizeof(char), 4096);
  int x;
  while ((x = read(fd, buffer, 4096)) > 0) {
    int writed = 0;
    int cur;
    while (writed < x) {
      cur = write(1, buffer + writed, x - writed);
      if (cur == -1) {
        perror("write");
        goto free;
      }
      writed += cur;
    }
  }
  if (x == -1)
    perror("read");
free:
  free(buffer);
  return;
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x1,%edi
mov    $0x1000,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11ea <cat+0x9a>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e5 <cat+0x95>
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d7 <cat+0x87>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
jmp    1205 <cat+0xb5>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1192 <cat+0x42>
jmp    116e <cat+0x1e>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1200 <cat+0xb0>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
jmp    1205 <cat+0xb5>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r15d
mov    $0x1,%edi
mov    $0x1000,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
mov    %r15d,%edi
mov    %rax,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11ca <cat+0x7a>
lea    0xe76(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%r12,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    $0x1,%edi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     11d6 <cat+0x86>
add    %eax,%ebx
cmp    %ebx,%ebp
jg     1190 <cat+0x40>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11ca <cat+0x7a>
xor    %ebx,%ebx
jmp    1190 <cat+0x40>
cmp    $0xffffffff,%ebp
jne    11de <cat+0x8e>
lea    0xe30(%rip),%r14
mov    %r14,%rdi
call   1070 <perror@plt>
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@.str.1 = [5 x i8] c"read\00"
define void @cat(i32 %0) {
%2 = tail call i64 @calloc(i32 1, i32 4096)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @read(i32 %0, i8* %3, i32 4096)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %9, label %21
6: ; preds = %11
%7 = tail call i32 @read(i32 %0, i8* %3, i32 4096)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %1, %6
%10 = phi i32 [ %7, %6 ], [ %4, %1 ]
br label %13
11: ; preds = %13
%12 = icmp sgt i32 %10, %20
br i1 %12, label %13, label %6
13: ; preds = %9, %11
%14 = phi i32 [ %20, %11 ], [ 0, %9 ]
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %3, i64 %15
%17 = sub nsw i32 %10, %14
%18 = tail call i32 @write(i32 1, i8* %16, i32 %17)
%19 = icmp eq i32 %18, -1
%20 = add nsw i32 %18, %14
br i1 %19, label %24, label %11
21: ; preds = %6, %1
%22 = phi i32 [ %4, %1 ], [ %7, %6 ]
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %13, %21
%25 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %21 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %13 ]
%26 = tail call i32 @perror(i8* %25)
br label %27
27: ; preds = %24, %21
%28 = tail call i32 @free(i8* %3)
ret void
}