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

/* Variables and functions */
scalar_t__ read_block(int, char *, int);
scalar_t__ write_block(int, char *, scalar_t__);
ssize_t copy_block(int fd_out, int fd_in, size_t nbyte) {
  char buffer[(1024)];
  ssize_t rd;
  size_t total;
  total = 0;
  while (nbyte > 0 && (rd = read_block(fd_in, buffer,
                                       nbyte > sizeof(buffer) ? sizeof(buffer)
                                                              : nbyte)) > 0) {
    if (write_block(fd_out, buffer, rd) < rd) {
      return -1;
    }
    total += rd;
    nbyte -= rd;
  }
  if (rd < 0) {
    return rd;
  } else {
    return total;
  }
}


x86 assembly (clang, -O0):
<copy_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x430(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x431(%rbp)
jbe    11c3 <copy_block+0xa3>
mov    -0x10(%rbp),%eax
mov    %eax,-0x444(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x440(%rbp)
cmpq   $0x400,-0x18(%rbp)
jbe    1189 <copy_block+0x69>
mov    $0x400,%eax
mov    %rax,-0x450(%rbp)
jmp    1194 <copy_block+0x74>
mov    -0x18(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x440(%rbp),%rsi
mov    -0x444(%rbp),%edi
mov    -0x450(%rbp),%rax
mov    %eax,%edx
call   1040 <read_block@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
setg   %al
mov    %al,-0x431(%rbp)
mov    -0x431(%rbp),%al
test   $0x1,%al
jne    11d6 <copy_block+0xb6>
jmp    1232 <copy_block+0x112>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1030 <write_block@plt>
cmp    -0x428(%rbp),%rax
jge    1206 <copy_block+0xe6>
movq   $0xffffffffffffffff,-0x8(%rbp)
jmp    125b <copy_block+0x13b>
mov    -0x428(%rbp),%rax
add    -0x430(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x428(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1140 <copy_block+0x20>
cmpq   $0x0,-0x428(%rbp)
jge    1250 <copy_block+0x130>
mov    -0x428(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125b <copy_block+0x13b>
mov    -0x430(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
test   %rdx,%rdx
je     118f <copy_block+0x6f>
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %edi,%r15d
xor    %r14d,%r14d
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x400,%rbx
mov    $0x400,%edx
cmovb  %ebx,%edx
mov    %r12d,%edi
mov    %r13,%rsi
call   1040 <read_block@plt>
mov    %rax,%rbp
test   %rax,%rax
jle    1192 <copy_block+0x72>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <write_block@plt>
cmp    %rbp,%rax
jl     119b <copy_block+0x7b>
add    %rbp,%r14
sub    %rbp,%rbx
jne    1150 <copy_block+0x30>
jmp    1192 <copy_block+0x72>
xor    %r14d,%r14d
test   %rbp,%rbp
cmovs  %rbp,%r14
jmp    11a2 <copy_block+0x82>
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @copy_block(i32 %0, i32 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca [1024 x i8]
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i64 0, i64* %10
br label %11
11: ; preds = %37, %3
%12 = load i64, i64* %7
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = load i32, i32* %6
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%17 = load i64, i64* %7
%18 = icmp ugt i64 %17, 1024
br i1 %18, label %19, label %20
19: ; preds = %14
br label %22
20: ; preds = %14
%21 = load i64, i64* %7
br label %22
22: ; preds = %20, %19
%23 = phi i64 [ 1024, %19 ], [ %21, %20 ]
%24 = trunc i64 %23 to i32
%25 = call i64 @read_block(i32 %15, i8* %16, i32 %24)
store i64 %25, i64* %9
%26 = icmp sgt i64 %25, 0
br label %27
27: ; preds = %22, %11
%28 = phi i1 [ false, %11 ], [ %26, %22 ]
br i1 %28, label %29, label %44
29: ; preds = %27
%30 = load i32, i32* %5
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%32 = load i64, i64* %9
%33 = call i64 @write_block(i32 %30, i8* %31, i64 %32)
%34 = load i64, i64* %9
%35 = icmp slt i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %29
store i64 -1, i64* %4
br label %51
37: ; preds = %29
%38 = load i64, i64* %9
%39 = load i64, i64* %10
%40 = add i64 %39, %38
store i64 %40, i64* %10
%41 = load i64, i64* %9
%42 = load i64, i64* %7
%43 = sub i64 %42, %41
store i64 %43, i64* %7
br label %11
44: ; preds = %27
%45 = load i64, i64* %9
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = load i64, i64* %9
store i64 %48, i64* %4
br label %51
49: ; preds = %44
%50 = load i64, i64* %10
store i64 %50, i64* %4
br label %51
51: ; preds = %49, %47, %36
%52 = load i64, i64* %4
ret i64 %52
}

LLVM IR (-O2):
define i64 @copy_block(i32 %0, i32 %1, i64 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %2, 0
br i1 %6, label %22, label %7
7: ; preds = %3, %18
%8 = phi i64 [ %19, %18 ], [ 0, %3 ]
%9 = phi i64 [ %20, %18 ], [ %2, %3 ]
%10 = icmp ult i64 %9, 1024
%11 = select i1 %10, i64 %9, i64 1024
%12 = trunc i64 %11 to i32
%13 = call i64 @read_block(i32 %1, i8* %5, i32 %12)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %7
%16 = call i64 @write_block(i32 %0, i8* %5, i64 %13)
%17 = icmp slt i64 %16, %13
br i1 %17, label %27, label %18
18: ; preds = %15
%19 = add i64 %13, %8
%20 = sub i64 %9, %13
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %7
22: ; preds = %7, %18, %3
%23 = phi i64 [ 0, %3 ], [ %19, %18 ], [ %8, %7 ]
%24 = phi i64 [ undef, %3 ], [ %13, %18 ], [ %13, %7 ]
%25 = icmp slt i64 %24, 0
%26 = select i1 %25, i64 %24, i64 %23
br label %27
27: ; preds = %15, %22
%28 = phi i64 [ %26, %22 ], [ -1, %15 ]
ret i64 %28
}