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

/* Variables and functions */
int bcm2835_read(int);
int /*<<< orphan*/ bcm2835_write(int, unsigned int);
int /*<<< orphan*/ printk(char *, ...);
int mailbox_write(unsigned int value, unsigned int channel) {
  printk("MAILBOX_WRITE: writing value=%x channel %x\n", value, channel);
  if (value & 0xf) {
    printk("mailbox_write: bottom bits not zero %x\n", value);
    return -1;
  }
  if (channel > 15) {
    printk("mailbox_write: channel too high %x\n", channel);
    return -1;
  }
  while ((bcm2835_read((0xb880 + 0x38)) & 0x80000000)) {
    printk("Write mailbox full!\n");
  }
  bcm2835_write((0xb880 + 0x20), channel | value);
  return 0;
}


x86 assembly (clang, -O0):
<mailbox_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
mov    -0x8(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
je     117e <mailbox_write+0x4e>
mov    -0x8(%rbp),%esi
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <mailbox_write+0xbc>
cmpl   $0xf,-0xc(%rbp)
jbe    11a5 <mailbox_write+0x75>
mov    -0xc(%rbp),%esi
lea    0xec2(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <mailbox_write+0xbc>
jmp    11aa <mailbox_write+0x7a>
mov    $0xb8b8,%edi
call   1040 <bcm2835_read@plt>
and    $0x80000000,%eax
cmp    $0x0,%eax
je     11d5 <mailbox_write+0xa5>
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
jmp    11aa <mailbox_write+0x7a>
mov    -0xc(%rbp),%esi
or     -0x8(%rbp),%esi
mov    $0xb8a0,%edi
call   1050 <bcm2835_write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mailbox_write>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xec1(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printk@plt>
test   $0xf,%bpl
je     115b <mailbox_write+0x2b>
lea    0xed5(%rip),%rdi
mov    %ebp,%esi
jmp    1169 <mailbox_write+0x39>
cmp    $0xf,%ebx
jbe    117a <mailbox_write+0x4a>
lea    0xeed(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printk@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xb8b8,%edi
call   1040 <bcm2835_read@plt>
test   %eax,%eax
jns    11a8 <mailbox_write+0x78>
lea    0xee9(%rip),%r14
nop
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printk@plt>
mov    $0xb8b8,%edi
call   1040 <bcm2835_read@plt>
test   %eax,%eax
js     1190 <mailbox_write+0x60>
or     %ebp,%ebx
mov    $0xb8a0,%edi
mov    %ebx,%esi
call   1050 <bcm2835_write@plt>
xor    %eax,%eax
jmp    1175 <mailbox_write+0x45>

LLVM IR (-O0):
@.str = [44 x i8] c"MAILBOX_WRITE: writing value=%x channel %x\0A\00"
@.str.1 = [40 x i8] c"mailbox_write: bottom bits not zero %x\0A\00"
@.str.2 = [36 x i8] c"mailbox_write: channel too high %x\0A\00"
@.str.3 = [21 x i8] c"Write mailbox full!\0A\00"
define i32 @mailbox_write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = load i32, i32* %4
%7 = load i32, i32* %5
%8 = call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = load i32, i32* %4
%10 = and i32 %9, 15
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* %4
%14 = call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 -1, i32* %3
br label %33
15: ; preds = %2
%16 = load i32, i32* %5
%17 = icmp ugt i32 %16, 15
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* %5
%20 = call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %19)
store i32 -1, i32* %3
br label %33
21: ; preds = %15
br label %22
22: ; preds = %26, %21
%23 = call i32 @bcm2835_read(i32 47288)
%24 = and i32 %23, -2147483648
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %22
28: ; preds = %22
%29 = load i32, i32* %5
%30 = load i32, i32* %4
%31 = or i32 %29, %30
%32 = call i32 @bcm2835_write(i32 47264, i32 %31)
store i32 0, i32* %3
br label %33
33: ; preds = %28, %18, %12
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [44 x i8] c"MAILBOX_WRITE: writing value=%x channel %x\0A\00"
@.str.1 = [40 x i8] c"mailbox_write: bottom bits not zero %x\0A\00"
@.str.2 = [36 x i8] c"mailbox_write: channel too high %x\0A\00"
@.str.3 = [21 x i8] c"Write mailbox full!\0A\00"
define i32 @mailbox_write(i32 %0, i32 %1) {
%3 = tail call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%4 = and i32 %0, 15
%5 = icmp eq i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %22
8: ; preds = %2
%9 = icmp ugt i32 %1, 15
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = tail call i32 @bcm2835_read(i32 47288)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %19, label %15
13: ; preds = %8
%14 = tail call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %1)
br label %22
15: ; preds = %10, %15
%16 = tail call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @bcm2835_read(i32 47288)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %15
19: ; preds = %15, %10
%20 = or i32 %1, %0
%21 = tail call i32 @bcm2835_write(i32 47264, i32 %20)
br label %22
22: ; preds = %19, %13, %6
%23 = phi i32 [ -1, %6 ], [ -1, %13 ], [ 0, %19 ]
ret i32 %23
}