#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*/ printk(char *, ...);
int mailbox_read(unsigned int channel) {
  unsigned int mail;
  printk("MAILBOX_READ: reading channel %x\n", channel);
  if (channel > 15) {
    printk("mailbox_read: channel too high\n", channel);
    return -1;
  }
  while ((bcm2835_read((0xb880 + 0x18)) & 0x40000000)) {
    printk("mailbox_read: mail_empty\n");
  }
  mail = bcm2835_read((0xb880 + 0x00));
  if ((mail & 0xf) != channel) {
    printk("mailbox_read: read from wrong channel %x %x\n", mail & 0xf,
           channel);
    return -1;
  }
  return mail & 0xfffffff0;
}


x86 assembly (clang, -O0):
<mailbox_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
cmpl   $0xf,-0x8(%rbp)
jbe    1163 <mailbox_read+0x43>
mov    -0x8(%rbp),%esi
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <mailbox_read+0xbb>
jmp    1168 <mailbox_read+0x48>
mov    $0xb898,%edi
call   1040 <bcm2835_read@plt>
and    $0x40000000,%eax
cmp    $0x0,%eax
je     1193 <mailbox_read+0x73>
lea    0xebb(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
jmp    1168 <mailbox_read+0x48>
mov    $0xb880,%edi
call   1040 <bcm2835_read@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xf,%eax
cmp    -0x8(%rbp),%eax
je     11d2 <mailbox_read+0xb2>
mov    -0xc(%rbp),%esi
and    $0xf,%esi
mov    -0x8(%rbp),%edx
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <mailbox_read+0xbb>
mov    -0xc(%rbp),%eax
and    $0xfffffff0,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mailbox_read>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xed4(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printk@plt>
cmp    $0xf,%ebp
jbe    114c <mailbox_read+0x2c>
lea    0xee1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printk@plt>
jmp    11b3 <mailbox_read+0x93>
mov    $0xb898,%edi
call   1040 <bcm2835_read@plt>
test   $0x40000000,%eax
je     118b <mailbox_read+0x6b>
lea    0xede(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <printk@plt>
mov    $0xb898,%edi
call   1040 <bcm2835_read@plt>
test   $0x40000000,%eax
jne    1170 <mailbox_read+0x50>
mov    $0xb880,%edi
call   1040 <bcm2835_read@plt>
mov    %eax,%esi
and    $0xf,%esi
cmp    %ebp,%esi
jne    11a3 <mailbox_read+0x83>
and    $0xfffffff0,%eax
jmp    11b8 <mailbox_read+0x98>
lea    0xeb2(%rip),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printk@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"MAILBOX_READ: reading channel %x\0A\00"
@.str.1 = [32 x i8] c"mailbox_read: channel too high\0A\00"
@.str.2 = [26 x i8] c"mailbox_read: mail_empty\0A\00"
@.str.3 = [45 x i8] c"mailbox_read: read from wrong channel %x %x\0A\00"
define i32 @mailbox_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* %3
%8 = icmp ugt i32 %7, 15
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %10)
store i32 -1, i32* %2
br label %33
12: ; preds = %1
br label %13
13: ; preds = %17, %12
%14 = call i32 @bcm2835_read(i32 47256)
%15 = and i32 %14, 1073741824
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %13
19: ; preds = %13
%20 = call i32 @bcm2835_read(i32 47232)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = and i32 %21, 15
%23 = load i32, i32* %3
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = load i32, i32* %4
%27 = and i32 %26, 15
%28 = load i32, i32* %3
%29 = call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %28)
store i32 -1, i32* %2
br label %33
30: ; preds = %19
%31 = load i32, i32* %4
%32 = and i32 %31, -16
store i32 %32, i32* %2
br label %33
33: ; preds = %30, %25, %9
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@.str = [34 x i8] c"MAILBOX_READ: reading channel %x\0A\00"
@.str.1 = [32 x i8] c"mailbox_read: channel too high\0A\00"
@.str.2 = [26 x i8] c"mailbox_read: mail_empty\0A\00"
@.str.3 = [45 x i8] c"mailbox_read: read from wrong channel %x %x\0A\00"
define i32 @mailbox_read(i32 %0) {
%2 = tail call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = icmp ugt i32 %0, 15
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = tail call i32 @bcm2835_read(i32 47256)
%6 = and i32 %5, 1073741824
%7 = icmp eq i32 %6, 0
br i1 %7, label %15, label %10
8: ; preds = %1
%9 = tail call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %23
10: ; preds = %4, %10
%11 = tail call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @bcm2835_read(i32 47256)
%13 = and i32 %12, 1073741824
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %10
15: ; preds = %10, %4
%16 = tail call i32 @bcm2835_read(i32 47232)
%17 = and i32 %16, 15
%18 = icmp eq i32 %17, %0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 (i8*, ...) @printk(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %0)
br label %23
21: ; preds = %15
%22 = and i32 %16, -16
br label %23
23: ; preds = %21, %19, %8
%24 = phi i32 [ -1, %8 ], [ -1, %19 ], [ %22, %21 ]
ret i32 %24
}