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

/* Variables and functions */
int rdsr();
int /*<<< orphan*/ usart_cs(int);
int /*<<< orphan*/ usart_out_byte(int);
int /*<<< orphan*/ wren();
void write(uint32_t addr, uint8_t *bytes, uint16_t len) {
  wren();
  usart_cs(0);
  usart_out_byte(0x02);
  usart_out_byte(addr >> 16);
  usart_out_byte(addr >> 8);
  usart_out_byte(addr);
  while (len-- > 0)
    usart_out_byte(*bytes++);
  usart_cs(1);
  for (;;) {
    uint8_t status = rdsr();
    if ((status & (1 << 0)) == 0)
      break;
  }
}


x86 assembly (clang, -O0):
<write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1050 <wren@plt>
xor    %edi,%edi
call   1060 <usart_cs@plt>
mov    $0x2,%edi
call   1030 <usart_out_byte@plt>
mov    -0x4(%rbp),%edi
sar    $0x10,%edi
call   1030 <usart_out_byte@plt>
mov    -0x4(%rbp),%edi
sar    $0x8,%edi
call   1030 <usart_out_byte@plt>
mov    -0x4(%rbp),%edi
call   1030 <usart_out_byte@plt>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    11b7 <write+0x77>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%edi
call   1030 <usart_out_byte@plt>
jmp    1188 <write+0x48>
mov    $0x1,%edi
call   1060 <usart_cs@plt>
mov    $0x0,%al
call   1040 <rdsr@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11df <write+0x9f>
jmp    11e4 <write+0xa4>
jmp    11c1 <write+0x81>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <wren@plt>
xor    %edi,%edi
call   1060 <usart_cs@plt>
mov    $0x2,%edi
call   1030 <usart_out_byte@plt>
mov    %ebp,%edi
sar    $0x10,%edi
call   1030 <usart_out_byte@plt>
mov    %ebp,%edi
sar    $0x8,%edi
call   1030 <usart_out_byte@plt>
mov    %ebp,%edi
call   1030 <usart_out_byte@plt>
test   %ebx,%ebx
jle    11a4 <write+0x64>
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14),%edi
add    $0x4,%r14
call   1030 <usart_out_byte@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     1190 <write+0x50>
mov    $0x1,%edi
call   1060 <usart_cs@plt>
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <rdsr@plt>
test   $0x1,%al
jne    11b0 <write+0x70>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @write(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%8 = call i32 (...) @wren()
%9 = call i32 @usart_cs(i32 0)
%10 = call i32 @usart_out_byte(i32 2)
%11 = load i32, i32* %4
%12 = ashr i32 %11, 16
%13 = call i32 @usart_out_byte(i32 %12)
%14 = load i32, i32* %4
%15 = ashr i32 %14, 8
%16 = call i32 @usart_out_byte(i32 %15)
%17 = load i32, i32* %4
%18 = call i32 @usart_out_byte(i32 %17)
br label %19
19: ; preds = %23, %3
%20 = load i32, i32* %6
%21 = add nsw i32 %20, -1
store i32 %21, i32* %6
%22 = icmp sgt i32 %20, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32*, i32** %5
%25 = getelementptr inbounds i32, i32* %24, i32 1
store i32* %25, i32** %5
%26 = load i32, i32* %24
%27 = call i32 @usart_out_byte(i32 %26)
br label %19
28: ; preds = %19
%29 = call i32 @usart_cs(i32 1)
br label %30
30: ; preds = %36, %28
%31 = call i32 (...) @rdsr()
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = and i32 %32, 1
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
br label %37
36: ; preds = %30
br label %30
37: ; preds = %35
ret void
}

LLVM IR (-O2):
define void @write(i32 %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32 (...) @wren()
%5 = tail call i32 @usart_cs(i32 0)
%6 = tail call i32 @usart_out_byte(i32 2)
%7 = ashr i32 %0, 16
%8 = tail call i32 @usart_out_byte(i32 %7)
%9 = ashr i32 %0, 8
%10 = tail call i32 @usart_out_byte(i32 %9)
%11 = tail call i32 @usart_out_byte(i32 %0)
%12 = icmp sgt i32 %2, 0
br i1 %12, label %13, label %21
13: ; preds = %3, %13
%14 = phi i32* [ %17, %13 ], [ %1, %3 ]
%15 = phi i32 [ %16, %13 ], [ %2, %3 ]
%16 = add nsw i32 %15, -1
%17 = getelementptr inbounds i32, i32* %14, i64 1
%18 = load i32, i32* %14
%19 = tail call i32 @usart_out_byte(i32 %18)
%20 = icmp ugt i32 %15, 1
br i1 %20, label %13, label %21
21: ; preds = %13, %3
%22 = tail call i32 @usart_cs(i32 1)
br label %23
23: ; preds = %23, %21
%24 = tail call i32 (...) @rdsr()
%25 = and i32 %24, 1
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %23
27: ; preds = %23
ret void
}