#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 uint32_t;
typedef scalar_t__ int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char **, int, int, int /*<<< orphan*/ *);
void copy_chunked(FILE *in, FILE *out, int64_t offset, int64_t size) {
  uint32_t real_offset = (uint32_t)offset;
  fseek(in, real_offset, SEEK_SET);
  uint32_t real_size = (uint32_t)size;
  if (size < 0) {
    fseek(in, 0, SEEK_END);
    real_size = ftell(in);
    real_size -= real_offset;
    fseek(in, real_offset, SEEK_SET);
  }
  char buffer[512];
  while (real_size >= 512) {
    fread(&buffer, 1, 512, in);
    fwrite(&buffer, 1, 512, out);
    real_size -= 512;
  }
  if (real_size > 0) {
    fread(&buffer, 1, real_size, in);
    fwrite(&buffer, 1, real_size, out);
  }
}


x86 assembly (clang, -O0):
<copy_chunked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11c9 <copy_chunked+0x89>
mov    -0x8(%rbp),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
jmp    11ce <copy_chunked+0x8e>
cmpl   $0x200,-0x28(%rbp)
jl     121f <copy_chunked+0xdf>
lea    -0x230(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x28(%rbp)
jmp    11ce <copy_chunked+0x8e>
cmpl   $0x0,-0x28(%rbp)
jle    1259 <copy_chunked+0x119>
lea    -0x230(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
lea    -0x230(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_chunked>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e76(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %r12d,%esi
call   1050 <fseek@plt>
test   %rbx,%rbx
js     1205 <copy_chunked+0xc5>
cmp    $0x200,%ebx
jl     123f <copy_chunked+0xff>
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
lea    -0x200(%rbx),%ebp
cmp    $0x3ff,%ebx
mov    %ebp,%ebx
ja     1190 <copy_chunked+0x50>
test   %ebp,%ebp
jle    11f5 <copy_chunked+0xb5>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
mov    %r15,%rdi
call   1040 <ftell@plt>
mov    %eax,%ebx
sub    %r12d,%ebx
mov    0x0(%rbp),%edx
mov    %r15,%rdi
mov    %r12d,%esi
call   1050 <fseek@plt>
cmp    $0x200,%ebx
jge    1182 <copy_chunked+0x42>
mov    %ebx,%ebp
test   %ebp,%ebp
jg     11ce <copy_chunked+0x8e>
jmp    11f5 <copy_chunked+0xb5>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
define void @copy_chunked(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 [512 x i8]
store i32* %0, i32** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%12 = load i64, i64* %7
%13 = trunc i64 %12 to i32
store i32 %13, i32* %9
%14 = load i32*, i32** %5
%15 = load i32, i32* %9
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @fseek(i32* %14, i32 %15, i32 %16)
%18 = load i64, i64* %8
%19 = trunc i64 %18 to i32
store i32 %19, i32* %10
%20 = load i64, i64* %8
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %4
%23 = load i32*, i32** %5
%24 = load i32, i32* @SEEK_END
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i32*, i32** %5
%27 = call i32 @ftell(i32* %26)
store i32 %27, i32* %10
%28 = load i32, i32* %9
%29 = load i32, i32* %10
%30 = sub nsw i32 %29, %28
store i32 %30, i32* %10
%31 = load i32*, i32** %5
%32 = load i32, i32* %9
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %31, i32 %32, i32 %33)
br label %35
35: ; preds = %22, %4
br label %36
36: ; preds = %39, %35
%37 = load i32, i32* %10
%38 = icmp sge i32 %37, 512
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = bitcast [512 x i8]* %11 to i8**
%41 = load i32*, i32** %5
%42 = call i32 @fread(i8** %40, i32 1, i32 512, i32* %41)
%43 = bitcast [512 x i8]* %11 to i8**
%44 = load i32*, i32** %6
%45 = call i32 @fwrite(i8** %43, i32 1, i32 512, i32* %44)
%46 = load i32, i32* %10
%47 = sub nsw i32 %46, 512
store i32 %47, i32* %10
br label %36
48: ; preds = %36
%49 = load i32, i32* %10
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %48
%52 = bitcast [512 x i8]* %11 to i8**
%53 = load i32, i32* %10
%54 = load i32*, i32** %5
%55 = call i32 @fread(i8** %52, i32 1, i32 %53, i32* %54)
%56 = bitcast [512 x i8]* %11 to i8**
%57 = load i32, i32* %10
%58 = load i32*, i32** %6
%59 = call i32 @fwrite(i8** %56, i32 1, i32 %57, i32* %58)
br label %60
60: ; preds = %51, %48
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
define void @copy_chunked(i32* nocapture %0, i32* nocapture %1, i64 %2, i64 %3) {
%5 = alloca [512 x i8]
%6 = trunc i64 %2 to i32
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @fseek(i32* %0, i32 %6, i32 %7)
%9 = trunc i64 %3 to i32
%10 = icmp slt i64 %3, 0
br i1 %10, label %11, label %18
11: ; preds = %4
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @fseek(i32* %0, i32 0, i32 %12)
%14 = tail call i32 @ftell(i32* %0)
%15 = sub nsw i32 %14, %6
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %0, i32 %6, i32 %16)
br label %18
18: ; preds = %11, %4
%19 = phi i32 [ %15, %11 ], [ %9, %4 ]
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%21 = icmp sgt i32 %19, 511
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = bitcast [512 x i8]* %5 to i8**
br label %24
24: ; preds = %22, %24
%25 = phi i32 [ %19, %22 ], [ %28, %24 ]
%26 = call i32 @fread(i8** %23, i32 1, i32 512, i32* %0)
%27 = call i32 @fwrite(i8** %23, i32 1, i32 512, i32* %1)
%28 = add nsw i32 %25, -512
%29 = icmp ugt i32 %25, 1023
br i1 %29, label %24, label %30
30: ; preds = %24, %18
%31 = phi i32 [ %19, %18 ], [ %28, %24 ]
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = bitcast [512 x i8]* %5 to i8**
%35 = call i32 @fread(i8** %34, i32 1, i32 %31, i32* %0)
%36 = call i32 @fwrite(i8** %34, i32 1, i32 %31, i32* %1)
br label %37
37: ; preds = %33, %30
ret void
}