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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int /*<<< orphan*/, int, int /*<<< orphan*/);
int read(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
uint32_t copy_image(int32_t in_fd, int32_t out_fd) {
  uint8_t buf[0x1000];
  int n;
  uint32_t image_size = 0;
  uint8_t zero = 0;
  lseek(in_fd, 65536, SEEK_SET);
  while ((n = read(in_fd, buf, 0x1000)) > 0) {
    image_size = image_size + n;
    write(out_fd, buf, n);
  }
  if (image_size % 2) {
    image_size++;
    write(out_fd, &zero, 1);
  }
  return image_size;
}


x86 assembly (clang, -O0):
<copy_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x4018(%rbp)
movl   $0x0,-0x401c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
mov    $0x10000,%esi
call   1040 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x4014(%rbp)
cmp    $0x0,%eax
jle    11ba <copy_image+0x8a>
mov    -0x4018(%rbp),%eax
add    -0x4014(%rbp),%eax
mov    %eax,-0x4018(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    -0x4014(%rbp),%edx
call   1030 <write@plt>
jmp    116b <copy_image+0x3b>
mov    -0x4018(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11f4 <copy_image+0xc4>
mov    -0x4018(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4018(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x401c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x4018(%rbp),%eax
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_image>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4018,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
movl   $0x0,0xc(%rsp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edx
mov    $0x10000,%esi
call   1040 <lseek@plt>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    11bd <copy_image+0x8d>
lea    0x10(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
add    %eax,%ebx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     1180 <copy_image+0x50>
test   $0x1,%bl
je     11bd <copy_image+0x8d>
add    $0x1,%ebx
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebx,%eax
add    $0x4018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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