#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 */
struct zip_error {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int off_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ _zip_error_set(struct zip_error *, int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
scalar_t__ fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
scalar_t__ fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
int copy_data(FILE *fs, off_t len, FILE *ft, struct zip_error *error) {
  char buf[8192];
  ssize_t n, nn;
  if (len == 0)
    return 0;
  while (len > 0) {
    nn = len > sizeof(buf) ? sizeof(buf) : len;
    if ((n = fread(buf, 1, nn, fs)) < 0) {
      _zip_error_set(error, 5, errno);
      return -1;
    } else if (n == 0) {
      _zip_error_set(error, 17, 0);
      return -1;
    }
    if (fwrite(buf, 1, n, ft) != n) {
      _zip_error_set(error, 6, errno);
      return -1;
    }
    len -= (off_t)n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1160 <copy_data+0x30>
movl   $0x0,-0x4(%rbp)
jmp    1291 <copy_data+0x161>
jmp    1165 <copy_data+0x35>
cmpl   $0x0,-0x14(%rbp)
jle    128a <copy_data+0x15a>
movslq -0x14(%rbp),%rax
cmp    $0x2000,%rax
jbe    1190 <copy_data+0x60>
mov    $0x2000,%eax
mov    %rax,-0x2048(%rbp)
jmp    119b <copy_data+0x6b>
movslq -0x14(%rbp),%rax
mov    %rax,-0x2048(%rbp)
mov    -0x2048(%rbp),%rax
mov    %rax,-0x2040(%rbp)
lea    -0x2030(%rbp),%rdi
mov    -0x2040(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x2038(%rbp)
cmp    $0x0,%rax
jge    11f9 <copy_data+0xc9>
mov    -0x28(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1050 <_zip_error_set@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <copy_data+0x161>
cmpq   $0x0,-0x2038(%rbp)
jne    1223 <copy_data+0xf3>
mov    -0x28(%rbp),%rdi
mov    $0x11,%esi
xor    %edx,%edx
call   1050 <_zip_error_set@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <copy_data+0x161>
jmp    1228 <copy_data+0xf8>
lea    -0x2030(%rbp),%rdi
mov    -0x2038(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
cmp    -0x2038(%rbp),%rax
je     1274 <copy_data+0x144>
mov    -0x28(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
mov    $0x6,%esi
call   1050 <_zip_error_set@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <copy_data+0x161>
mov    -0x2038(%rbp),%rax
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1165 <copy_data+0x35>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
xor    %eax,%eax
test   %esi,%esi
jle    11f9 <copy_data+0xc9>
mov    %rdx,%r12
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rcx,0x8(%rsp)
mov    $0x2000,%r15d
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edx
cmp    $0x2000,%ebp
cmovae %r15,%rdx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
js     11b9 <copy_data+0x89>
je     11ce <copy_data+0x9e>
mov    %rax,%r14
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r12,%rcx
call   1040 <fwrite@plt>
cmp    %r14,%rax
jne    11dc <copy_data+0xac>
sub    %r14d,%ebp
test   %ebp,%ebp
jg     1170 <copy_data+0x40>
xor    %eax,%eax
jmp    11f9 <copy_data+0xc9>
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    0x8(%rsp),%rdi
mov    $0x5,%esi
jmp    11ef <copy_data+0xbf>
mov    0x8(%rsp),%rdi
mov    $0x11,%esi
xor    %edx,%edx
jmp    11ef <copy_data+0xbf>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    0x8(%rsp),%rdi
mov    $0x6,%esi
call   1050 <_zip_error_set@plt>
mov    $0xffffffff,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.zip_error = type { i32 }
@errno = global i32 0
define i32 @copy_data(i32* %0, i32 %1, i32* %2, %struct.zip_error* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.zip_error*
%10 = alloca [8192 x i8]
%11 = alloca i64
%12 = alloca i64
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store %struct.zip_error* %3, %struct.zip_error** %9
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 0, i32* %5
br label %63
16: ; preds = %4
br label %17
17: ; preds = %57, %16
%18 = load i32, i32* %7
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %62
20: ; preds = %17
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = icmp ugt i64 %22, 8192
br i1 %23, label %24, label %25
24: ; preds = %20
br label %28
25: ; preds = %20
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
br label %28
28: ; preds = %25, %24
%29 = phi i64 [ 8192, %24 ], [ %27, %25 ]
store i64 %29, i64* %12
%30 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%31 = load i64, i64* %12
%32 = load i32*, i32** %6
%33 = call i64 @fread(i8* %30, i32 1, i64 %31, i32* %32)
store i64 %33, i64* %11
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load %struct.zip_error*, %struct.zip_error** %9
%37 = load i32, i32* @errno
%38 = call i32 @_zip_error_set(%struct.zip_error* %36, i32 5, i32 %37)
store i32 -1, i32* %5
br label %63
39: ; preds = %28
%40 = load i64, i64* %11
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load %struct.zip_error*, %struct.zip_error** %9
%44 = call i32 @_zip_error_set(%struct.zip_error* %43, i32 17, i32 0)
store i32 -1, i32* %5
br label %63
45: ; preds = %39
br label %46
46: ; preds = %45
%47 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%48 = load i64, i64* %11
%49 = load i32*, i32** %8
%50 = call i64 @fwrite(i8* %47, i32 1, i64 %48, i32* %49)
%51 = load i64, i64* %11
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %57
53: ; preds = %46
%54 = load %struct.zip_error*, %struct.zip_error** %9
%55 = load i32, i32* @errno
%56 = call i32 @_zip_error_set(%struct.zip_error* %54, i32 6, i32 %55)
store i32 -1, i32* %5
br label %63
57: ; preds = %46
%58 = load i64, i64* %11
%59 = trunc i64 %58 to i32
%60 = load i32, i32* %7
%61 = sub nsw i32 %60, %59
store i32 %61, i32* %7
br label %17
62: ; preds = %17
store i32 0, i32* %5
br label %63
63: ; preds = %62, %53, %42, %35, %15
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
%struct.zip_error = type { i32 }
@errno = global i32 0
define i32 @copy_data(i32* nocapture %0, i32 %1, i32* nocapture %2, %struct.zip_error* %3) {
%5 = alloca [8192 x i8]
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %10, label %32
8: ; preds = %24
%9 = icmp sgt i32 %28, 0
br i1 %9, label %10, label %32
10: ; preds = %4, %8
%11 = phi i32 [ %28, %8 ], [ %1, %4 ]
%12 = zext i32 %11 to i64
%13 = icmp ult i64 %12, 8192
%14 = select i1 %13, i64 %12, i64 8192
%15 = call i64 @fread(i8* %6, i32 1, i64 %14, i32* %0)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* @errno
%19 = tail call i32 @_zip_error_set(%struct.zip_error* %3, i32 5, i32 %18)
br label %32
20: ; preds = %10
%21 = icmp eq i64 %15, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @_zip_error_set(%struct.zip_error* %3, i32 17, i32 0)
br label %32
24: ; preds = %20
%25 = call i64 @fwrite(i8* %6, i32 1, i64 %15, i32* %2)
%26 = icmp eq i64 %25, %15
%27 = trunc i64 %15 to i32
%28 = sub nsw i32 %11, %27
br i1 %26, label %8, label %29
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = tail call i32 @_zip_error_set(%struct.zip_error* %3, i32 6, i32 %30)
br label %32
32: ; preds = %8, %4, %29, %22, %17
%33 = phi i32 [ -1, %17 ], [ -1, %22 ], [ -1, %29 ], [ 0, %4 ], [ 0, %8 ]
ret i32 %33
}