#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*/ int32_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int /*<<< orphan*/, int, int /*<<< orphan*/);
int read(int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, unsigned char *, int);
unsigned long copy_image(int32_t in_fd, int32_t out_fd) {
  unsigned char buf[0x1000];
  int n;
  unsigned long image_size = 0;
  unsigned char 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    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movq   $0x0,-0x1020(%rbp)
movb   $0x0,-0x1021(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
mov    $0x10000,%esi
call   1040 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jle    11be <copy_image+0x8e>
mov    -0x1020(%rbp),%rax
movslq -0x1014(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1030 <write@plt>
jmp    1169 <copy_image+0x39>
mov    -0x1020(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     11f9 <copy_image+0xc9>
mov    -0x1020(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1021(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x1020(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_image>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
movb   $0x0,0xf(%rsp)
mov    0x2e91(%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    11c1 <copy_image+0x91>
lea    0x10(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %eax,%ecx
add    %rcx,%rbx
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     11c1 <copy_image+0x91>
add    $0x1,%rbx
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %rbx,%rax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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