#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sleep(double);
scalar_t__ write(int, char *, scalar_t__);
int cat_fdio(int fd_in, int fd_out) {
  char buf[4096];
  ssize_t nread;
  while ((nread = read(fd_in, buf, sizeof buf)) > 0) {
    ssize_t ntotalwritten = 0;
    while (ntotalwritten < nread) {
      ssize_t nwritten =
          write(fd_out, buf + ntotalwritten, nread - ntotalwritten);
      if (nwritten < 1)
        return -1;
      ntotalwritten += nwritten;
    }
    sleep(0.5);
  }
  return nread == 0 ? 0 : -1;
}


x86 assembly (clang, -O0):
<cat_fdio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jle    11f6 <cat_fdio+0xc6>
movq   $0x0,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
cmp    -0x1018(%rbp),%rax
jge    11e4 <cat_fdio+0xb4>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
add    -0x1020(%rbp),%rsi
mov    -0x1018(%rbp),%rdx
sub    -0x1020(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x1,-0x1028(%rbp)
01
jge    11ca <cat_fdio+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <cat_fdio+0xde>
mov    -0x1028(%rbp),%rax
add    -0x1020(%rbp),%rax
mov    %rax,-0x1020(%rbp)
jmp    1171 <cat_fdio+0x41>
movsd  0xe14(%rip),%xmm0
call   1050 <sleep@plt>
jmp    1141 <cat_fdio+0x11>
mov    -0x1018(%rbp),%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_fdio>:
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   1040 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jle    11a5 <cat_fdio+0x75>
mov    %rsp,%r15
xor    %ebx,%ebx
nop
mov    %rbp,%rdx
sub    %rbx,%rdx
jle    1180 <cat_fdio+0x50>
lea    (%rsp,%rbx,1),%rsi
mov    %r12d,%edi
call   1030 <write@plt>
add    %rax,%rbx
test   %rax,%rax
jg     1160 <cat_fdio+0x30>
jmp    11ac <cat_fdio+0x7c>
xchg   %ax,%ax
movsd  0xe78(%rip),%xmm0
call   1050 <sleep@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jg     115d <cat_fdio+0x2d>
neg    %rbp
sbb    %eax,%eax
jmp    11b1 <cat_fdio+0x81>
mov    $0xffffffff,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @cat_fdio(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 i64 @read(i32 %0, i8* %4, i32 4096)
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %22
7: ; preds = %2, %18
%8 = phi i64 [ %20, %18 ], [ %5, %2 ]
br label %9
9: ; preds = %7, %12
%10 = phi i64 [ %17, %12 ], [ 0, %7 ]
%11 = icmp sgt i64 %8, %10
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %10
%14 = sub nsw i64 %8, %10
%15 = call i64 @write(i32 %1, i8* %13, i64 %14)
%16 = icmp slt i64 %15, 1
%17 = add nsw i64 %15, %10
br i1 %16, label %26, label %9
18: ; preds = %9
%19 = tail call i32 @sleep(double 5.000000e-01)
%20 = call i64 @read(i32 %0, i8* %4, i32 4096)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %7, label %22
22: ; preds = %18, %2
%23 = phi i64 [ %5, %2 ], [ %20, %18 ]
%24 = icmp ne i64 %23, 0
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %12, %22
%27 = phi i32 [ %25, %22 ], [ -1, %12 ]
ret i32 %27
}