#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;

/* Variables and functions */
scalar_t__ SPIEraseSector(int);
scalar_t__ SPIRead(int, int /*<<< orphan*/ *, int const);
scalar_t__ SPIWrite(int, int /*<<< orphan*/ *, int const);
int copy_raw(const uint32_t src_addr, const uint32_t dst_addr,
             const uint32_t size) {
  if (src_addr & 0xfff != 0 || dst_addr & 0xfff != 0) {
    return 1;
  }
  const uint32_t buffer_size = 0x1000;
  uint8_t buffer[buffer_size];
  uint32_t left = ((size + buffer_size - 1) & ~(buffer_size - 1));
  uint32_t saddr = src_addr;
  uint32_t daddr = dst_addr;
  while (left) {
    if (SPIEraseSector(daddr / buffer_size)) {
      return 2;
    }
    if (SPIRead(saddr, buffer, buffer_size)) {
      return 3;
    }
    if (SPIWrite(daddr, buffer, buffer_size)) {
      return 4;
    }
    saddr += buffer_size;
    daddr += buffer_size;
    left -= buffer_size;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    115f <copy_raw+0x2f>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     116b <copy_raw+0x3b>
movl   $0x1,-0x4(%rbp)
jmp    1278 <copy_raw+0x148>
movl   $0x1000,-0x14(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    %rsp,%rax
add    $0xffffffffffffc000,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,%rsp
mov    -0x10(%rbp),%eax
add    $0x1000,%eax
sub    $0x1,%eax
and    $0xfffff000,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1263 <copy_raw+0x133>
mov    -0x2c(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <SPIEraseSector@plt>
cmp    $0x0,%rax
je     11e1 <copy_raw+0xb1>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1271 <copy_raw+0x141>
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%edi
mov    $0x1000,%edx
call   1030 <SPIRead@plt>
cmp    $0x0,%rax
je     120f <copy_raw+0xdf>
movl   $0x3,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1271 <copy_raw+0x141>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    $0x1000,%edx
call   1040 <SPIWrite@plt>
cmp    $0x0,%rax
je     123d <copy_raw+0x10d>
movl   $0x4,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1271 <copy_raw+0x141>
mov    -0x28(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1000,%eax
mov    %eax,-0x24(%rbp)
jmp    11a8 <copy_raw+0x78>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_raw>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%eax
or     %edi,%eax
mov    $0x1,%r14d
test   $0x1,%al
jne    1206 <copy_raw+0xd6>
mov    %edx,%r15d
mov    %rsp,%rax
mov    %rsp,%r13
add    $0xffffffffffffc000,%r13
mov    %r13,%rsp
add    $0xfff,%r15d
xor    %r14d,%r14d
test   $0xfffff000,%r15d
je     1203 <copy_raw+0xd3>
mov    %esi,%ebx
mov    %edi,%r12d
mov    %rax,-0x30(%rbp)
and    $0xfffff000,%r15d
neg    %r15d
nopl   (%rax)
lea    0xfff(%rbx),%edi
test   %ebx,%ebx
cmovns %ebx,%edi
sar    $0xc,%edi
call   1050 <SPIEraseSector@plt>
test   %rax,%rax
jne    11e9 <copy_raw+0xb9>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
call   1030 <SPIRead@plt>
test   %rax,%rax
jne    11f1 <copy_raw+0xc1>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
call   1040 <SPIWrite@plt>
test   %rax,%rax
jne    11f9 <copy_raw+0xc9>
add    $0x1000,%r12d
add    $0x1000,%ebx
add    $0x1000,%r15d
jne    1190 <copy_raw+0x60>
jmp    11ff <copy_raw+0xcf>
mov    $0x2,%r14d
jmp    11ff <copy_raw+0xcf>
mov    $0x3,%r14d
jmp    11ff <copy_raw+0xcf>
mov    $0x4,%r14d
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @copy_raw(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%14 = load i32, i32* %5
%15 = and i32 %14, 1
%16 = icmp ne i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %3
%18 = load i32, i32* %6
%19 = and i32 %18, 1
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17, %3
store i32 1, i32* %4
br label %60
22: ; preds = %17
store i32 4096, i32* %8
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %9
%24 = alloca i32, i64 4096
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 4096
%27 = sub nsw i32 %26, 1
%28 = and i32 %27, -4096
store i32 %28, i32* %10
%29 = load i32, i32* %5
store i32 %29, i32* %11
%30 = load i32, i32* %6
store i32 %30, i32* %12
br label %31
31: ; preds = %50, %22
%32 = load i32, i32* %10
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %57
34: ; preds = %31
%35 = load i32, i32* %12
%36 = sdiv i32 %35, 4096
%37 = call i64 @SPIEraseSector(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 2, i32* %4
store i32 1, i32* %13
br label %58
40: ; preds = %34
%41 = load i32, i32* %11
%42 = call i64 @SPIRead(i32 %41, i32* %24, i32 4096)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 3, i32* %4
store i32 1, i32* %13
br label %58
45: ; preds = %40
%46 = load i32, i32* %12
%47 = call i64 @SPIWrite(i32 %46, i32* %24, i32 4096)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 4, i32* %4
store i32 1, i32* %13
br label %58
50: ; preds = %45
%51 = load i32, i32* %11
%52 = add nsw i32 %51, 4096
store i32 %52, i32* %11
%53 = load i32, i32* %12
%54 = add nsw i32 %53, 4096
store i32 %54, i32* %12
%55 = load i32, i32* %10
%56 = sub nsw i32 %55, 4096
store i32 %56, i32* %10
br label %31
57: ; preds = %31
store i32 0, i32* %4
store i32 1, i32* %13
br label %58
58: ; preds = %57, %49, %44, %39
%59 = load i8*, i8** %9
br label %60
60: ; preds = %58, %21
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
define i32 @copy_raw(i32 %0, i32 %1, i32 %2) {
%4 = or i32 %1, %0
%5 = and i32 %4, 1
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %34
7: ; preds = %3
%8 = tail call i8* @llvm.stacksave()
%9 = alloca [4096 x i32]
%10 = getelementptr inbounds [4096 x i32], [4096 x i32]* %9, i64 0, i64 0
%11 = add nsw i32 %2, 4095
%12 = and i32 %11, -4096
%13 = icmp eq i32 %12, 0
br i1 %13, label %32, label %14
14: ; preds = %7, %27
%15 = phi i32 [ %29, %27 ], [ %1, %7 ]
%16 = phi i32 [ %28, %27 ], [ %0, %7 ]
%17 = phi i32 [ %30, %27 ], [ %12, %7 ]
%18 = sdiv i32 %15, 4096
%19 = call i64 @SPIEraseSector(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %14
%22 = call i64 @SPIRead(i32 %16, i32* %10, i32 4096)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = call i64 @SPIWrite(i32 %15, i32* %10, i32 4096)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = add nsw i32 %16, 4096
%29 = add nsw i32 %15, 4096
%30 = add nsw i32 %17, -4096
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %14
32: ; preds = %14, %21, %24, %27, %7
%33 = phi i32 [ 0, %7 ], [ 0, %27 ], [ 4, %24 ], [ 3, %21 ], [ 2, %14 ]
br label %34
34: ; preds = %3, %32
%35 = phi i32 [ %33, %32 ], [ 1, %3 ]
ret i32 %35
}