#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 uint8_t;
typedef int uint32_t;
typedef int uint16_t;

/* Variables and functions */
__attribute__((always_)) uint32_t flashWrite(uint32_t address, uint8_t **data,
                                             uint32_t writeLength) {
  uint32_t index, flash_word[4];
  uint8_t i;
  *((volatile uint32_t *)0x4010000C) = 0;
  for (index = 0; index < writeLength; index += (4 * 4)) {
    for (i = 0; i < 4; i++)
      flash_word[i] = (*(uint32_t *)(*data + i * 4));
    *((volatile uint32_t *)0x40100010) = 0x0000003F;
    *((volatile uint32_t *)0x40100018) = (uint16_t)((address + index) >> 2);
    *((volatile uint32_t *)0x40100040) = flash_word[0];
    *((volatile uint32_t *)0x40100044) = flash_word[1];
    *((volatile uint32_t *)0x40100048) = flash_word[2];
    *((volatile uint32_t *)0x4010004C) = flash_word[3];
    *((volatile uint32_t *)0x40100000) = 0xCC;
    while ((*((volatile uint32_t *)0x40100010) & 0x01) == 0)
      ;
    *data += (4 * 4);
  }
  return 0;
}


x86 assembly (clang, -O0):
<flashWrite>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x4010000c,%eax
movl   $0x0,(%rax)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e8 <flashWrite+0xe8>
movl   $0x0,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
jge    1166 <flashWrite+0x66>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x34(%rbp),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
movslq -0x34(%rbp),%rax
mov    %ecx,-0x30(%rbp,%rax,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1133 <flashWrite+0x33>
mov    $0x40100010,%eax
movl   $0x3f,(%rax)
mov    -0x4(%rbp),%ecx
add    -0x18(%rbp),%ecx
sar    $0x2,%ecx
mov    $0x40100018,%eax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    $0x40100040,%eax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    $0x40100044,%eax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
mov    $0x40100048,%eax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    $0x4010004c,%eax
mov    %ecx,(%rax)
mov    $0x40100000,%eax
movl   $0xcc,(%rax)
mov    $0x40100010,%eax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11cc <flashWrite+0xcc>
jmp    11b4 <flashWrite+0xb4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x40,%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x18(%rbp)
jmp    1120 <flashWrite+0x20>
xor    %eax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<flashWrite>:
push   %rbx
movl   $0x0,0x4010000c
test   %edx,%edx
jle    119b <flashWrite+0x9b>
mov    (%rsi),%rax
xor    %ecx,%ecx
nopl   0x0(%rax)
mov    (%rax),%r8d
mov    0x10(%rax),%r9d
mov    0x20(%rax),%r10d
mov    0x30(%rax),%r11d
movl   $0x3f,0x40100010
lea    (%rcx,%rdi,1),%ebx
sar    $0x2,%ebx
mov    %ebx,0x40100018
mov    %r8d,0x40100040
40
mov    %r9d,0x40100044
40
mov    %r10d,0x40100048
40
mov    %r11d,0x4010004c
40
movl   $0xcc,0x40100000
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
testl  $0x1,0x40100010
je     1180 <flashWrite+0x80>
add    $0x40,%rax
mov    %rax,(%rsi)
add    $0x10,%ecx
cmp    %edx,%ecx
jl     1120 <flashWrite+0x20>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @flashWrite(i32 %0, i32** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i32]
%9 = alloca i32
store i32 %0, i32* %4
store i32** %1, i32*** %5
store i32 %2, i32* %6
store volatile i32 0, i32* inttoptr (i64 1074790412 to i32*)
store i32 0, i32* %7
br label %10
10: ; preds = %54, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %57
14: ; preds = %10
store i32 0, i32* %9
br label %15
15: ; preds = %29, %14
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 4
br i1 %17, label %18, label %32
18: ; preds = %15
%19 = load i32**, i32*** %5
%20 = load i32*, i32** %19
%21 = load i32, i32* %9
%22 = mul nsw i32 %21, 4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %20, i64 %23
%25 = load i32, i32* %24
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 %27
store i32 %25, i32* %28
br label %29
29: ; preds = %18
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %15
32: ; preds = %15
store volatile i32 63, i32* inttoptr (i64 1074790416 to i32*)
%33 = load i32, i32* %4
%34 = load i32, i32* %7
%35 = add nsw i32 %33, %34
%36 = ashr i32 %35, 2
store volatile i32 %36, i32* inttoptr (i64 1074790424 to i32*)
%37 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%38 = load i32, i32* %37
store volatile i32 %38, i32* inttoptr (i64 1074790464 to i32*)
%39 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 1
%40 = load i32, i32* %39
store volatile i32 %40, i32* inttoptr (i64 1074790468 to i32*)
%41 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 2
%42 = load i32, i32* %41
store volatile i32 %42, i32* inttoptr (i64 1074790472 to i32*)
%43 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 3
%44 = load i32, i32* %43
store volatile i32 %44, i32* inttoptr (i64 1074790476 to i32*)
store volatile i32 204, i32* inttoptr (i64 1074790400 to i32*)
br label %45
45: ; preds = %49, %32
%46 = load volatile i32, i32* inttoptr (i64 1074790416 to i32*)
%47 = and i32 %46, 1
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
br label %45
50: ; preds = %45
%51 = load i32**, i32*** %5
%52 = load i32*, i32** %51
%53 = getelementptr inbounds i32, i32* %52, i64 16
store i32* %53, i32** %51
br label %54
54: ; preds = %50
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 16
store i32 %56, i32* %7
br label %10
57: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
define i32 @flashWrite(i32 %0, i32** nocapture %1, i32 %2) {
store volatile i32 0, i32* inttoptr (i64 1074790412 to i32*)
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %27
5: ; preds = %3
%6 = load i32*, i32** %1
br label %7
7: ; preds = %5, %23
%8 = phi i32* [ %6, %5 ], [ %24, %23 ]
%9 = phi i32 [ 0, %5 ], [ %25, %23 ]
%10 = load i32, i32* %8
%11 = getelementptr inbounds i32, i32* %8, i64 4
%12 = load i32, i32* %11
%13 = getelementptr inbounds i32, i32* %8, i64 8
%14 = load i32, i32* %13
%15 = getelementptr inbounds i32, i32* %8, i64 12
%16 = load i32, i32* %15
store volatile i32 63, i32* inttoptr (i64 1074790416 to i32*)
%17 = add nsw i32 %9, %0
%18 = ashr i32 %17, 2
store volatile i32 %18, i32* inttoptr (i64 1074790424 to i32*)
store volatile i32 %10, i32* inttoptr (i64 1074790464 to i32*)
store volatile i32 %12, i32* inttoptr (i64 1074790468 to i32*)
store volatile i32 %14, i32* inttoptr (i64 1074790472 to i32*)
store volatile i32 %16, i32* inttoptr (i64 1074790476 to i32*)
store volatile i32 204, i32* inttoptr (i64 1074790400 to i32*)
br label %19
19: ; preds = %19, %7
%20 = load volatile i32, i32* inttoptr (i64 1074790416 to i32*)
%21 = and i32 %20, 1
%22 = icmp eq i32 %21, 0
br i1 %22, label %19, label %23
23: ; preds = %19
%24 = getelementptr inbounds i32, i32* %8, i64 16
store i32* %24, i32** %1
%25 = add nuw nsw i32 %9, 16
%26 = icmp slt i32 %25, %2
br i1 %26, label %7, label %27
27: ; preds = %23, %3
ret i32 0
}