#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 ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int awrite(int, char *, int);
int read(int, char *, int);
int cat(int f) {
  char buffer[1024];
  ssize_t amount;
  while ((amount = read(f, buffer, sizeof(buffer))))
    if ((amount == -1) || (awrite(1, buffer, amount) == -1))
      return -1;
  return 0;
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
je     118f <cat+0x6f>
cmpl   $0xffffffff,-0x414(%rbp)
je     117e <cat+0x5e>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
mov    $0x1,%edi
call   1030 <awrite@plt>
cmp    $0xffffffff,%eax
jne    118a <cat+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <cat+0x76>
jmp    112e <cat+0xe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
nop
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1165 <cat+0x45>
test   %eax,%eax
je     1163 <cat+0x43>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <awrite@plt>
cmp    $0xffffffff,%eax
jne    1130 <cat+0x10>
mov    $0xffffffff,%eax
jmp    1165 <cat+0x45>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @cat(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32 %0, i32* %3
br label %6
6: ; preds = %20, %1
%7 = load i32, i32* %3
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 1024)
store i32 %9, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %6
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* %5
%17 = call i32 @awrite(i32 1, i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %14, %11
store i32 -1, i32* %2
br label %22
20: ; preds = %14
br label %6
21: ; preds = %6
store i32 0, i32* %2
br label %22
22: ; preds = %21, %19
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
define i32 @cat(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %6, %1
%5 = call i32 @read(i32 %0, i8* %3, i32 1024)
switch i32 %5, label %6 [
i32 0, label %11
i32 -1, label %9
]
6: ; preds = %4
%7 = call i32 @awrite(i32 1, i8* %3, i32 %5)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %4
9: ; preds = %4, %6
%10 = phi i32 [ -1, %6 ], [ %5, %4 ]
br label %11
11: ; preds = %4, %9
%12 = phi i32 [ %10, %9 ], [ 0, %4 ]
ret i32 %12
}