#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 char uint8_t;
typedef scalar_t__ uint32_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ update_checksum(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
uint32_t copy_image(int in_fd, int out_fd, uint16_t *sum) {
  uint8_t buf[0x1000];
  int offset = 0;
  int n;
  uint32_t image_size = 0;
  lseek(in_fd, 65536, SEEK_SET);
  while ((n = read(in_fd, buf + offset, 0x1000 - offset)) > 0) {
    n += offset;
    offset = n & 1;
    n -= offset;
    image_size = image_size + n;
    write(out_fd, buf, n);
    update_checksum(buf, n, sum);
    if (offset)
      buf[0] = buf[n];
  }
  if (offset) {
    image_size += 2;
    buf[1] = '\0';
    write(out_fd, buf, 2);
    update_checksum(buf, 2, sum);
  }
  return image_size;
}


x86 assembly (clang, -O0):
<copy_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x1014(%rbp)
movq   $0x0,-0x1020(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
mov    $0x10000,%esi
call   1050 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
movslq -0x1014(%rbp),%rax
add    %rax,%rsi
mov    $0x1000,%edx
sub    -0x1014(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x1018(%rbp)
cmp    $0x0,%eax
jle    1251 <copy_image+0x111>
mov    -0x1014(%rbp),%eax
add    -0x1018(%rbp),%eax
mov    %eax,-0x1018(%rbp)
mov    -0x1018(%rbp),%eax
and    $0x1,%eax
mov    %eax,-0x1014(%rbp)
mov    -0x1014(%rbp),%ecx
mov    -0x1018(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1018(%rbp)
mov    -0x1020(%rbp),%rax
movslq -0x1018(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%edx
call   1040 <write@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1030 <update_checksum@plt>
cmpl   $0x0,-0x1014(%rbp)
je     124c <copy_image+0x10c>
movslq -0x1018(%rbp),%rax
mov    -0x1010(%rbp,%rax,1),%al
mov    %al,-0x1010(%rbp)
jmp    1180 <copy_image+0x40>
cmpl   $0x0,-0x1014(%rbp)
je     12a0 <copy_image+0x160>
mov    -0x1020(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x1020(%rbp)
movb   $0x0,-0x100f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x2,%esi
call   1030 <update_checksum@plt>
mov    -0x1020(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdx,0x8(%rsp)
mov    %esi,%r15d
mov    %edi,%r12d
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
mov    $0x10000,%esi
call   1050 <lseek@plt>
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x1000,%edx
call   1060 <read@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    1233 <copy_image+0xf3>
lea    0x10(%rsp),%r13
xor    %ebx,%ebx
jmp    11c2 <copy_image+0x82>
cs nopw 0x0(%rax,%rax,1)
nop
add    %rbp,%rbx
mov    %r14d,%eax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    $0x1000,%edx
sub    %r14d,%edx
mov    %r12d,%edi
call   1060 <read@plt>
test   %eax,%eax
jle    11fc <copy_image+0xbc>
add    %r14d,%eax
movslq %eax,%r14
mov    %r14,%rbp
and    $0xfffffffffffffffe,%rbp
mov    %r15d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %r13,%rdi
mov    %ebp,%esi
mov    0x8(%rsp),%rdx
call   1030 <update_checksum@plt>
and    $0x1,%r14d
je     11a0 <copy_image+0x60>
movzbl 0x10(%rsp,%rbp,1),%eax
mov    %al,0x10(%rsp)
jmp    11a0 <copy_image+0x60>
test   %r14d,%r14d
je     1235 <copy_image+0xf5>
add    $0x2,%rbx
movb   $0x0,0x11(%rsp)
lea    0x10(%rsp),%rbp
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %rbp,%rdi
mov    $0x2,%esi
mov    0x8(%rsp),%rdx
call   1030 <update_checksum@plt>
jmp    1235 <copy_image+0xf5>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i64 @copy_image(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %8
store i64 0, i64* %10
%11 = load i32, i32* %4
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @lseek(i32 %11, i32 65536, i32 %12)
br label %14
14: ; preds = %53, %3
%15 = load i32, i32* %4
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %8
%21 = sub nsw i32 4096, %20
%22 = call i32 @read(i32 %15, i8* %19, i32 %21)
store i32 %22, i32* %9
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %54
24: ; preds = %14
%25 = load i32, i32* %8
%26 = load i32, i32* %9
%27 = add nsw i32 %26, %25
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = and i32 %28, 1
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %9
%33 = load i64, i64* %10
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = add nsw i64 %33, %35
store i64 %36, i64* %10
%37 = load i32, i32* %5
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%39 = load i32, i32* %9
%40 = call i32 @write(i32 %37, i8* %38, i32 %39)
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%42 = load i32, i32* %9
%43 = load i32*, i32** %6
%44 = call i32 @update_checksum(i8* %41, i32 %42, i32* %43)
%45 = load i32, i32* %8
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %24
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
store i8 %51, i8* %52
br label %53
53: ; preds = %47, %24
br label %14
54: ; preds = %14
%55 = load i32, i32* %8
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %67
57: ; preds = %54
%58 = load i64, i64* %10
%59 = add nsw i64 %58, 2
store i64 %59, i64* %10
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 1
store i8 0, i8* %60
%61 = load i32, i32* %5
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%63 = call i32 @write(i32 %61, i8* %62, i32 2)
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%65 = load i32*, i32** %6
%66 = call i32 @update_checksum(i8* %64, i32 2, i32* %65)
br label %67
67: ; preds = %57, %54
%68 = load i64, i64* %10
ret i64 %68
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i64 @copy_image(i32 %0, i32 %1, i32* %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek(i32 %0, i32 65536, i32 %6)
%8 = call i32 @read(i32 %0, i8* %5, i32 4096)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %38
10: ; preds = %3, %25
%11 = phi i32 [ %29, %25 ], [ %8, %3 ]
%12 = phi i64 [ %18, %25 ], [ 0, %3 ]
%13 = phi i32 [ %15, %25 ], [ 0, %3 ]
%14 = add nsw i32 %11, %13
%15 = and i32 %14, 1
%16 = and i32 %14, -2
%17 = sext i32 %16 to i64
%18 = add nsw i64 %12, %17
%19 = call i32 @write(i32 %1, i8* %5, i32 %16)
%20 = call i32 @update_checksum(i8* %5, i32 %16, i32* %2)
%21 = icmp eq i32 %15, 0
br i1 %21, label %25, label %22
22: ; preds = %10
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %17
%24 = load i8, i8* %23
store i8 %24, i8* %5
br label %25
25: ; preds = %22, %10
%26 = zext i32 %15 to i64
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %26
%28 = sub nuw nsw i32 4096, %15
%29 = call i32 @read(i32 %0, i8* %27, i32 %28)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %10, label %31
31: ; preds = %25
%32 = icmp eq i32 %15, 0
br i1 %32, label %38, label %33
33: ; preds = %31
%34 = add nsw i64 %18, 2
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 1
store i8 0, i8* %35
%36 = call i32 @write(i32 %1, i8* %5, i32 2)
%37 = call i32 @update_checksum(i8* %5, i32 2, i32* %2)
br label %38
38: ; preds = %3, %33, %31
%39 = phi i64 [ %34, %33 ], [ %18, %31 ], [ 0, %3 ]
ret i64 %39
}