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

/* Variables and functions */
__attribute__((section(".boot.text"))) int
bootloader_intflash_write(uint32_t destaddr, uint8_t *sourcedata,
                          uint32_t len) {
  uint32_t blocksize = ((*((volatile uint32_t *)(0x40023C10))) >> 8) & 0x03;
  uint32_t offset;
  blocksize = 1 << blocksize;
  if ((len & (blocksize - 1)) != 0) {
    return 1;
  }
  *((volatile uint32_t *)(0x40023C04)) = (0x45670123);
  *((volatile uint32_t *)(0x40023C04)) = (0xCDEF89AB);
  for (offset = 0; offset < len; offset += blocksize) {
    *((volatile uint32_t *)(0x40023C10)) =
        (((*((volatile uint32_t *)(0x40023C10))) & ~((1 << 1))) | ((1 << 0)));
    switch (blocksize) {
    case 1:
      *((volatile uint8_t *)destaddr) = *sourcedata;
      break;
    case 2:
      *((volatile uint16_t *)destaddr) = *(uint16_t *)sourcedata;
      break;
    case 4:
      *((volatile uint32_t *)destaddr) = *(uint32_t *)sourcedata;
      break;
    case 8:
      *((volatile uint64_t *)destaddr) = *(uint64_t *)sourcedata;
      break;
    default:
      return 1;
    }
    while ((*((volatile uint32_t *)(0x40023C0C))) & (1 << 16))
      ;
    sourcedata += blocksize;
    destaddr += blocksize;
  }
  *((volatile uint32_t *)(0x40023C10)) =
      (((*((volatile uint32_t *)(0x40023C10))) & ~(0)) | ((1 << 31)));
  return 0;
}


x86 assembly (clang, -O0):
<bootloader_intflash_write>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x40023c10,%eax
mov    (%rax),%eax
sar    $0x8,%eax
and    $0x3,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     114b <bootloader_intflash_write+0x4b>
movl   $0x1,-0x4(%rbp)
jmp    126a <bootloader_intflash_write+0x16a>
mov    $0x40023c04,%eax
movl   $0x45670123,(%rax)
mov    $0x40023c04,%eax
movl   $0xcdef89ab,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    124c <bootloader_intflash_write+0x14c>
mov    0x40023c10,%eax
or     $0x1,%eax
and    $0xfffffffd,%eax
mov    %eax,0x40023c10
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x7,%eax
ja     11f5 <bootloader_intflash_write+0xf5>
mov    -0x28(%rbp),%rax
lea    0xe58(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
movslq -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    1201 <bootloader_intflash_write+0x101>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
movslq -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    1201 <bootloader_intflash_write+0x101>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
movslq -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    1201 <bootloader_intflash_write+0x101>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
movslq -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    1201 <bootloader_intflash_write+0x101>
movl   $0x1,-0x4(%rbp)
jmp    126a <bootloader_intflash_write+0x16a>
jmp    1206 <bootloader_intflash_write+0x106>
mov    $0x40023c0c,%eax
mov    (%rax),%eax
and    $0x10000,%eax
cmp    $0x0,%eax
je     1220 <bootloader_intflash_write+0x120>
jmp    1206 <bootloader_intflash_write+0x106>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1168 <bootloader_intflash_write+0x68>
mov    $0x40023c10,%eax
mov    (%rax),%ecx
and    $0xffffffff,%ecx
or     $0x80000000,%ecx
mov    $0x40023c10,%eax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bootloader_intflash_write>:
mov    0x40023c10,%ecx
shr    $0x8,%rcx
and    $0x3,%cl
mov    $0x1,%r8d
shl    %cl,%r8
lea    -0x1(%r8),%ecx
mov    $0x1,%eax
test   %edx,%ecx
je     1125 <bootloader_intflash_write+0x25>
ret
movl   $0x45670123,0x40023c04
23 01 67 45
movl   $0xcdef89ab,0x40023c04
ab 89 ef cd
test   %edx,%edx
jle    119b <bootloader_intflash_write+0x9b>
movslq %edi,%r10
xor    %edi,%edi
mov    $0x116,%r9d
nopw   0x0(%rax,%rax,1)
mov    0x40023c10,%ecx
and    $0xfffffffc,%ecx
or     $0x1,%ecx
mov    %ecx,0x40023c10
cmp    $0x8,%r8d
ja     1124 <bootloader_intflash_write+0x24>
bt     %r8d,%r9d
jae    1124 <bootloader_intflash_write+0x24>
mov    (%rsi),%ecx
mov    %ecx,(%r10)
cs nopw 0x0(%rax,%rax,1)
nop
testl  $0x10000,0x40023c0c
jne    1180 <bootloader_intflash_write+0x80>
lea    (%rsi,%r8,4),%rsi
add    %r8,%r10
add    %r8d,%edi
cmp    %edx,%edi
jl     1150 <bootloader_intflash_write+0x50>
orl    $0x80000000,0x40023c10
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @bootloader_intflash_write(i32 %0, i32* %1, i32 %2) section ".boot.text" {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load volatile i32, i32* inttoptr (i64 1073888272 to i32*)
%11 = ashr i32 %10, 8
%12 = and i32 %11, 3
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = shl i32 1, %13
store i32 %14, i32* %8
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = sub nsw i32 %16, 1
%18 = and i32 %15, %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 1, i32* %4
br label %78
21: ; preds = %3
store volatile i32 1164378403, i32* inttoptr (i64 1073888260 to i32*)
store volatile i32 -839939669, i32* inttoptr (i64 1073888260 to i32*)
store i32 0, i32* %9
br label %22
22: ; preds = %70, %21
%23 = load i32, i32* %9
%24 = load i32, i32* %7
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %74
26: ; preds = %22
%27 = load volatile i32, i32* inttoptr (i64 1073888272 to i32*)
%28 = and i32 %27, -3
%29 = or i32 %28, 1
store volatile i32 %29, i32* inttoptr (i64 1073888272 to i32*)
%30 = load i32, i32* %8
switch i32 %30, label %55 [
i32 1, label %31
i32 2, label %37
i32 4, label %43
i32 8, label %49
]
31: ; preds = %26
%32 = load i32*, i32** %6
%33 = load i32, i32* %32
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = inttoptr i64 %35 to i32*
store volatile i32 %33, i32* %36
br label %56
37: ; preds = %26
%38 = load i32*, i32** %6
%39 = load i32, i32* %38
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = inttoptr i64 %41 to i32*
store volatile i32 %39, i32* %42
br label %56
43: ; preds = %26
%44 = load i32*, i32** %6
%45 = load i32, i32* %44
%46 = load i32, i32* %5
%47 = sext i32 %46 to i64
%48 = inttoptr i64 %47 to i32*
store volatile i32 %45, i32* %48
br label %56
49: ; preds = %26
%50 = load i32*, i32** %6
%51 = load i32, i32* %50
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = inttoptr i64 %53 to i32*
store volatile i32 %51, i32* %54
br label %56
55: ; preds = %26
store i32 1, i32* %4
br label %78
56: ; preds = %49, %43, %37, %31
br label %57
57: ; preds = %61, %56
%58 = load volatile i32, i32* inttoptr (i64 1073888268 to i32*)
%59 = and i32 %58, 65536
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %57
br label %57
62: ; preds = %57
%63 = load i32, i32* %8
%64 = load i32*, i32** %6
%65 = sext i32 %63 to i64
%66 = getelementptr inbounds i32, i32* %64, i64 %65
store i32* %66, i32** %6
%67 = load i32, i32* %8
%68 = load i32, i32* %5
%69 = add nsw i32 %68, %67
store i32 %69, i32* %5
br label %70
70: ; preds = %62
%71 = load i32, i32* %8
%72 = load i32, i32* %9
%73 = add nsw i32 %72, %71
store i32 %73, i32* %9
br label %22
74: ; preds = %22
%75 = load volatile i32, i32* inttoptr (i64 1073888272 to i32*)
%76 = and i32 %75, -1
%77 = or i32 %76, -2147483648
store volatile i32 %77, i32* inttoptr (i64 1073888272 to i32*)
store i32 0, i32* %4
br label %78
78: ; preds = %74, %55, %20
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
define i32 @bootloader_intflash_write(i32 %0, i32* nocapture %1, i32 %2) section ".boot.text" {
%4 = load volatile i32, i32* inttoptr (i64 1073888272 to i32*)
%5 = lshr i32 %4, 8
%6 = and i32 %5, 3
%7 = shl nuw nsw i32 1, %6
%8 = add nsw i32 %7, -1
%9 = and i32 %8, %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %3
store volatile i32 1164378403, i32* inttoptr (i64 1073888260 to i32*)
store volatile i32 -839939669, i32* inttoptr (i64 1073888260 to i32*)
%12 = icmp sgt i32 %2, 0
br i1 %12, label %13, label %35
13: ; preds = %11
%14 = zext i32 %7 to i64
%15 = sext i32 %0 to i64
br label %16
16: ; preds = %13, %30
%17 = phi i64 [ %15, %13 ], [ %32, %30 ]
%18 = phi i32 [ 0, %13 ], [ %33, %30 ]
%19 = phi i32* [ %1, %13 ], [ %31, %30 ]
%20 = load volatile i32, i32* inttoptr (i64 1073888272 to i32*)
%21 = and i32 %20, -4
%22 = or i32 %21, 1
store volatile i32 %22, i32* inttoptr (i64 1073888272 to i32*)
switch i32 %7, label %38 [
i32 1, label %23
i32 2, label %23
i32 4, label %23
i32 8, label %23
]
23: ; preds = %16, %16, %16, %16
%24 = load i32, i32* %19
%25 = inttoptr i64 %17 to i32*
store volatile i32 %24, i32* %25
br label %26
26: ; preds = %26, %23
%27 = load volatile i32, i32* inttoptr (i64 1073888268 to i32*)
%28 = and i32 %27, 65536
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %26
30: ; preds = %26
%31 = getelementptr inbounds i32, i32* %19, i64 %14
%32 = add i64 %17, %14
%33 = add nuw nsw i32 %18, %7
%34 = icmp slt i32 %33, %2
br i1 %34, label %16, label %35
35: ; preds = %30, %11
%36 = load volatile i32, i32* inttoptr (i64 1073888272 to i32*)
%37 = or i32 %36, -2147483648
store volatile i32 %37, i32* inttoptr (i64 1073888272 to i32*)
br label %38
38: ; preds = %16, %3, %35
%39 = phi i32 [ 0, %35 ], [ 1, %3 ], [ 1, %16 ]
ret i32 %39
}