#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 */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, scalar_t__);
int cat_fd(int fd_in, int fd_out) {
  char buffer[1024];
  ssize_t read_bytes, written_bytes;
  while ((read_bytes = read(fd_in, buffer, 1024)) > 0) {
    written_bytes = write(fd_out, buffer, read_bytes);
    if (written_bytes < 0) {
      fprintf(
          stderr,
          "Could not write() %zd bytes to the destination file descriptor: %m"
          "\n",
          read_bytes);
      return -1;
    }
  }
  if (read_bytes < 0) {
    fprintf(stderr,
            "Could not read() %zd bytes to the destination file descriptor: %m"
            "\n",
            1024);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cat_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jle    11c0 <cat_fd+0x90>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jge    11bb <cat_fd+0x8b>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    -0x418(%rbp),%rax
mov    %eax,%edx
lea    0xe56(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <cat_fd+0xcb>
jmp    1141 <cat_fd+0x11>
cmpq   $0x0,-0x418(%rbp)
jge    11f4 <cat_fd+0xc4>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
mov    $0x400,%edx
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <cat_fd+0xcb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    118e <cat_fd+0x5e>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    1150 <cat_fd+0x20>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    %ebx,%edx
jmp    11aa <cat_fd+0x7a>
xor    %eax,%eax
test   %rbx,%rbx
jns    11b4 <cat_fd+0x84>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
mov    $0x400,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [68 x i8] c"Could not write() %zd bytes to the destination file descriptor: %m\0A\00"
@.str.1 = [67 x i8] c"Could not read() %zd bytes to the destination file descriptor: %m\0A\00"
define i32 @cat_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %9
9: ; preds = %26, %2
%10 = load i32, i32* %4
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = call i64 @read(i32 %10, i8* %11, i32 1024)
store i64 %12, i64* %7
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %9
%15 = load i32, i32* %5
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%17 = load i64, i64* %7
%18 = call i64 @write(i32 %15, i8* %16, i64 %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = load i64, i64* %7
%24 = trunc i64 %23 to i32
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 -1, i32* %3
br label %34
26: ; preds = %14
br label %9
27: ; preds = %9
%28 = load i64, i64* %7
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 1024)
store i32 -1, i32* %3
br label %34
33: ; preds = %27
store i32 0, i32* %3
br label %34
34: ; preds = %33, %30, %21
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [68 x i8] c"Could not write() %zd bytes to the destination file descriptor: %m\0A\00"
@.str.1 = [67 x i8] c"Could not read() %zd bytes to the destination file descriptor: %m\0A\00"
define i32 @cat_fd(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %8, %2
%6 = call i64 @read(i32 %0, i8* %4, i32 1024)
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = call i64 @write(i32 %1, i8* %4, i64 %6)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %5
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = trunc i64 %6 to i32
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %20
15: ; preds = %5
%16 = icmp slt i64 %6, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 1024)
br label %20
20: ; preds = %15, %17, %11
%21 = phi i32 [ -1, %11 ], [ -1, %17 ], [ 0, %15 ]
ret i32 %21
}