#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 off_t;
typedef int cur;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int lseek(int, int, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int copy_blocks(int ifd, int ofd, unsigned long *offset, unsigned long *size) {
  off_t cur;
  int amt;
  unsigned long len = 0;
  char buffer[1024];
  cur = lseek(ofd, 0, SEEK_CUR);
  if (cur % 1024) {
    cur = (((cur) + 1024 - 1) & (~(1024 - 1)));
    cur = lseek(ofd, cur, SEEK_SET);
  }
  *offset = (unsigned long)cur;
  while ((amt = read(ifd, buffer, 1024)) > 0) {
    write(ofd, buffer, amt);
    len += amt;
  }
  *size = len;
  return 0;
}


x86 assembly (clang, -O0):
<copy_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11a5 <copy_blocks+0x75>
mov    -0x1c(%rbp),%eax
add    $0x400,%eax
sub    $0x1,%eax
and    $0xfffffc00,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    11f3 <copy_blocks+0xc3>
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
movslq -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11b0 <copy_blocks+0x80>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_blocks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r13d
mov    %edi,%r12d
mov    0x2e94(%rip),%rax
mov    (%rax),%edx
mov    %esi,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   $0x3ff,%eax
je     1183 <copy_blocks+0x53>
add    $0x3ff,%eax
and    $0xfffffc00,%eax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%edx
mov    %r13d,%edi
mov    %eax,%esi
call   1040 <lseek@plt>
cltq
mov    %rax,(%r15)
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11da <copy_blocks+0xaa>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %ebp,%eax
add    %rax,%rbx
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11b0 <copy_blocks+0x80>
jmp    11dc <copy_blocks+0xac>
xor    %ebx,%ebx
mov    %rbx,(%r14)
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
define i32 @copy_blocks(i32 %0, i32 %1, i64* %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64*
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca [1024 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64* %2, i64** %7
store i64* %3, i64** %8
store i64 0, i64* %11
%13 = load i32, i32* %6
%14 = load i32, i32* @SEEK_CUR
%15 = call i32 @lseek(i32 %13, i32 0, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = srem i32 %16, 1024
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %4
%20 = load i32, i32* %9
%21 = add nsw i32 %20, 1024
%22 = sub nsw i32 %21, 1
%23 = and i32 %22, -1024
store i32 %23, i32* %9
%24 = load i32, i32* %6
%25 = load i32, i32* %9
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %24, i32 %25, i32 %26)
store i32 %27, i32* %9
br label %28
28: ; preds = %19, %4
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = load i64*, i64** %7
store i64 %30, i64* %31
br label %32
32: ; preds = %37, %28
%33 = load i32, i32* %5
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 1024)
store i32 %35, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %32
%38 = load i32, i32* %6
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%40 = load i32, i32* %10
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = load i64, i64* %11
%45 = add i64 %44, %43
store i64 %45, i64* %11
br label %32
46: ; preds = %32
%47 = load i64, i64* %11
%48 = load i64*, i64** %8
store i64 %47, i64* %48
ret i32 0
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
define i32 @copy_blocks(i32 %0, i32 %1, i64* nocapture writeonly %2, i64* nocapture writeonly %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @SEEK_CUR
%8 = tail call i32 @lseek(i32 %1, i32 0, i32 %7)
%9 = and i32 %8, 1023
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = add nsw i32 %8, 1023
%13 = and i32 %12, -1024
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %1, i32 %13, i32 %14)
br label %16
16: ; preds = %11, %4
%17 = phi i32 [ %15, %11 ], [ %8, %4 ]
%18 = sext i32 %17 to i64
store i64 %18, i64* %2
%19 = call i32 @read(i32 %0, i8* %6, i32 1024)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %16, %21
%22 = phi i32 [ %27, %21 ], [ %19, %16 ]
%23 = phi i64 [ %26, %21 ], [ 0, %16 ]
%24 = call i32 @write(i32 %1, i8* %6, i32 %22)
%25 = zext i32 %22 to i64
%26 = add i64 %23, %25
%27 = call i32 @read(i32 %0, i8* %6, i32 1024)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %21, label %29
29: ; preds = %21, %16
%30 = phi i64 [ 0, %16 ], [ %26, %21 ]
store i64 %30, i64* %3
ret i32 0
}