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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ ferrordie(int /*<<< orphan*/ *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(void *, int, int, int /*<<< orphan*/ *);
void *malloc(int);
void fcpy(FILE *dst, FILE *src, uint32_t dst_off, uint32_t src_off,
          uint32_t size) {
  int left = size;
  int read;
  int written;
  int block;
  void *blockbuf;
  if (fseek(src, src_off, SEEK_SET) < 0)
    ferrordie(src, "reading ELF segment data");
  if (fseek(dst, dst_off, SEEK_SET) < 0)
    ferrordie(dst, "writing DOL segment data");
  blockbuf = malloc(((((1024 * 1024)) < (left)) ? ((1024 * 1024)) : (left)));
  while (left) {
    block = ((((1024 * 1024)) < (left)) ? ((1024 * 1024)) : (left));
    read = fread(blockbuf, 1, block, src);
    if (read != block) {
      free(blockbuf);
      ferrordie(src, "reading ELF segment data");
    }
    written = fwrite(blockbuf, 1, block, dst);
    if (written != block) {
      free(blockbuf);
      ferrordie(dst, "writing DOL segment data");
    }
    left -= block;
  }
  free(blockbuf);
}


x86 assembly (clang, -O0):
<fcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
jge    11af <fcpy+0x4f>
mov    -0x10(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1080 <ferrordie@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
jge    11de <fcpy+0x7e>
mov    -0x8(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1080 <ferrordie@plt>
mov    $0x100000,%eax
cmp    -0x20(%rbp),%eax
jge    11f9 <fcpy+0x99>
mov    $0x100000,%eax
mov    %eax,-0x3c(%rbp)
jmp    11ff <fcpy+0x9f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12c6 <fcpy+0x166>
mov    $0x100000,%eax
cmp    -0x20(%rbp),%eax
jge    1230 <fcpy+0xd0>
mov    $0x100000,%eax
mov    %eax,-0x40(%rbp)
jmp    1236 <fcpy+0xd6>
mov    -0x20(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     1279 <fcpy+0x119>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
lea    0xd8c(%rip),%rsi
call   1080 <ferrordie@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     12b6 <fcpy+0x156>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
lea    0xd68(%rip),%rsi
call   1080 <ferrordie@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    120b <fcpy+0xab>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcpy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e63(%rip),%r12
mov    (%r12),%edx
mov    %rsi,%rdi
mov    %ecx,%esi
call   1060 <fseek@plt>
test   %rax,%rax
jns    119f <fcpy+0x3f>
lea    0xe69(%rip),%rsi
mov    %r14,%rdi
call   1080 <ferrordie@plt>
mov    (%r12),%edx
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <fseek@plt>
test   %rax,%rax
jns    11c1 <fcpy+0x61>
lea    0xe60(%rip),%rsi
mov    %r15,%rdi
call   1080 <ferrordie@plt>
cmp    $0x100000,%ebx
mov    $0x100000,%edi
cmovl  %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %ebx,%ebx
je     1261 <fcpy+0x101>
lea    0xe1a(%rip),%rbp
jmp    11f5 <fcpy+0x95>
nopl   0x0(%rax,%rax,1)
sub    %r13d,%ebx
je     1261 <fcpy+0x101>
cmp    $0x100000,%ebx
mov    $0x100000,%r13d
cmovl  %ebx,%r13d
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %r13d,%eax
je     1230 <fcpy+0xd0>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1080 <ferrordie@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
cmp    %r13d,%eax
je     11f0 <fcpy+0x90>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
lea    0xdbf(%rip),%rsi
call   1080 <ferrordie@plt>
jmp    11f0 <fcpy+0x90>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [25 x i8] c"reading ELF segment data\00"
@.str.1 = [25 x i8] c"writing DOL segment data\00"
define void @fcpy(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = load i32, i32* %10
store i32 %16, i32* %11
%17 = load i32*, i32** %7
%18 = load i32, i32* %9
%19 = load i32, i32* @SEEK_SET
%20 = call i64 @fseek(i32* %17, i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %5
%23 = load i32*, i32** %7
%24 = call i32 @ferrordie(i32* %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %22, %5
%26 = load i32*, i32** %6
%27 = load i32, i32* %8
%28 = load i32, i32* @SEEK_SET
%29 = call i64 @fseek(i32* %26, i32 %27, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i32*, i32** %6
%33 = call i32 @ferrordie(i32* %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %31, %25
%35 = load i32, i32* %11
%36 = icmp slt i32 1048576, %35
br i1 %36, label %37, label %38
37: ; preds = %34
br label %40
38: ; preds = %34
%39 = load i32, i32* %11
br label %40
40: ; preds = %38, %37
%41 = phi i32 [ 1048576, %37 ], [ %39, %38 ]
%42 = call i8* @malloc(i32 %41)
store i8* %42, i8** %15
br label %43
43: ; preds = %79, %40
%44 = load i32, i32* %11
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %83
46: ; preds = %43
%47 = load i32, i32* %11
%48 = icmp slt i32 1048576, %47
br i1 %48, label %49, label %50
49: ; preds = %46
br label %52
50: ; preds = %46
%51 = load i32, i32* %11
br label %52
52: ; preds = %50, %49
%53 = phi i32 [ 1048576, %49 ], [ %51, %50 ]
store i32 %53, i32* %14
%54 = load i8*, i8** %15
%55 = load i32, i32* %14
%56 = load i32*, i32** %7
%57 = call i32 @fread(i8* %54, i32 1, i32 %55, i32* %56)
store i32 %57, i32* %12
%58 = load i32, i32* %12
%59 = load i32, i32* %14
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %66
61: ; preds = %52
%62 = load i8*, i8** %15
%63 = call i32 @free(i8* %62)
%64 = load i32*, i32** %7
%65 = call i32 @ferrordie(i32* %64, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %66
66: ; preds = %61, %52
%67 = load i8*, i8** %15
%68 = load i32, i32* %14
%69 = load i32*, i32** %6
%70 = call i32 @fwrite(i8* %67, i32 1, i32 %68, i32* %69)
store i32 %70, i32* %13
%71 = load i32, i32* %13
%72 = load i32, i32* %14
%73 = icmp ne i32 %71, %72
br i1 %73, label %74, label %79
74: ; preds = %66
%75 = load i8*, i8** %15
%76 = call i32 @free(i8* %75)
%77 = load i32*, i32** %6
%78 = call i32 @ferrordie(i32* %77, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %79
79: ; preds = %74, %66
%80 = load i32, i32* %14
%81 = load i32, i32* %11
%82 = sub nsw i32 %81, %80
store i32 %82, i32* %11
br label %43
83: ; preds = %43
%84 = load i8*, i8** %15
%85 = call i32 @free(i8* %84)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [25 x i8] c"reading ELF segment data\00"
@.str.1 = [25 x i8] c"writing DOL segment data\00"
define void @fcpy(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = load i32, i32* @SEEK_SET
%7 = tail call i64 @fseek(i32* %1, i32 %3, i32 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @ferrordie(i32* %1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = load i32, i32* @SEEK_SET
%13 = tail call i64 @fseek(i32* %0, i32 %2, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @ferrordie(i32* %0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = icmp slt i32 %4, 1048576
%19 = select i1 %18, i32 %4, i32 1048576
%20 = tail call i8* @malloc(i32 %19)
%21 = icmp eq i32 %4, 0
br i1 %21, label %40, label %22
22: ; preds = %17, %37
%23 = phi i32 [ %38, %37 ], [ %4, %17 ]
%24 = icmp slt i32 %23, 1048576
%25 = select i1 %24, i32 %23, i32 1048576
%26 = tail call i32 @fread(i8* %20, i32 1, i32 %25, i32* %1)
%27 = icmp eq i32 %26, %25
br i1 %27, label %31, label %28
28: ; preds = %22
%29 = tail call i32 @free(i8* %20)
%30 = tail call i32 @ferrordie(i32* %1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %28, %22
%32 = tail call i32 @fwrite(i8* %20, i32 1, i32 %25, i32* %0)
%33 = icmp eq i32 %32, %25
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = tail call i32 @free(i8* %20)
%36 = tail call i32 @ferrordie(i32* %0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %34, %31
%38 = sub nsw i32 %23, %25
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %22
40: ; preds = %37, %17
%41 = tail call i32 @free(i8* %20)
ret void
}