#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 /*<<< orphan*/ Mailbox_Read(int);
int /*<<< orphan*/ Mailbox_Write(int, unsigned int);
int /*<<< orphan*/ printf(char *);
unsigned int Mailbox_SdGetBaseFrequency(void) {
  volatile unsigned int mailbuffer[256] __attribute__((aligned(16)));
  unsigned int bufSize = 1;
  mailbuffer[bufSize++] = 0;
  mailbuffer[bufSize++] = 0x00030002;
  mailbuffer[bufSize++] = 8;
  mailbuffer[bufSize++] = 4;
  mailbuffer[bufSize++] = 1;
  mailbuffer[bufSize++] = 0;
  mailbuffer[bufSize++] = 0;
  mailbuffer[0] = bufSize * 4;
  unsigned int bufferAddr = &mailbuffer;
  bufferAddr -= 0x80000000;
  Mailbox_Write(8, bufferAddr);
  Mailbox_Read(8);
  if (mailbuffer[1] != 0x80000000) {
    printf(
        "Failed to retrieve SD base frequency - Invalid mailbox response.\n");
    return -1;
  }
  if (mailbuffer[5] != 0x1) {
    printf("Failed to retrieve SD base frequency - Invalid clock id.\n");
    return -1;
  }
  return mailbuffer[6];
}


x86 assembly (clang, -O0):
<Mailbox_SdGetBaseFrequency>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x1,-0x414(%rbp)
mov    -0x414(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x414(%rbp)
mov    %eax,%eax
movl   $0x0,-0x410(%rbp,%rax,4)
mov    -0x414(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x414(%rbp)
mov    %eax,%eax
movl   $0x30002,-0x410(%rbp,%rax,4)
mov    -0x414(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x414(%rbp)
mov    %eax,%eax
movl   $0x8,-0x410(%rbp,%rax,4)
mov    -0x414(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x414(%rbp)
mov    %eax,%eax
movl   $0x4,-0x410(%rbp,%rax,4)
mov    -0x414(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x414(%rbp)
mov    %eax,%eax
movl   $0x1,-0x410(%rbp,%rax,4)
mov    -0x414(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x414(%rbp)
mov    %eax,%eax
movl   $0x0,-0x410(%rbp,%rax,4)
mov    -0x414(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x414(%rbp)
mov    %eax,%eax
movl   $0x0,-0x410(%rbp,%rax,4)
mov    -0x414(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x410(%rbp)
lea    -0x410(%rbp),%rax
mov    %eax,-0x418(%rbp)
mov    -0x418(%rbp),%eax
sub    $0x80000000,%eax
mov    %eax,-0x418(%rbp)
mov    -0x418(%rbp),%esi
mov    $0x8,%edi
call   1050 <Mailbox_Write@plt>
mov    $0x8,%edi
call   1040 <Mailbox_Read@plt>
mov    -0x40c(%rbp),%eax
cmp    $0x80000000,%eax
je     1287 <Mailbox_SdGetBaseFrequency+0x157>
lea    0xd8a(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <Mailbox_SdGetBaseFrequency+0x187>
mov    -0x3fc(%rbp),%eax
cmp    $0x1,%eax
je     12ae <Mailbox_SdGetBaseFrequency+0x17e>
lea    0xda5(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <Mailbox_SdGetBaseFrequency+0x187>
mov    -0x3f8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Mailbox_SdGetBaseFrequency>:
sub    $0x408,%rsp
movl   $0x0,0x4(%rsp)
movl   $0x30002,0x8(%rsp)
movl   $0x8,0xc(%rsp)
movl   $0x4,0x10(%rsp)
movl   $0x1,0x14(%rsp)
movl   $0x0,0x18(%rsp)
movl   $0x0,0x1c(%rsp)
movl   $0x20,(%rsp)
mov    %rsp,%rsi
xor    $0x80000000,%esi
mov    $0x8,%edi
call   1050 <Mailbox_Write@plt>
mov    $0x8,%edi
call   1040 <Mailbox_Read@plt>
mov    0x4(%rsp),%eax
cmp    $0x80000000,%eax
jne    11b3 <Mailbox_SdGetBaseFrequency+0x83>
mov    0x14(%rsp),%eax
cmp    $0x1,%eax
jne    11bc <Mailbox_SdGetBaseFrequency+0x8c>
mov    0x18(%rsp),%eax
add    $0x408,%rsp
ret
lea    0xe7f(%rip),%rdi
jmp    11c3 <Mailbox_SdGetBaseFrequency+0x93>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
ret

LLVM IR (-O0):
@.str = [66 x i8] c"Failed to retrieve SD base frequency - Invalid mailbox response.\0A\00"
@.str.1 = [58 x i8] c"Failed to retrieve SD base frequency - Invalid clock id.\0A\00"
define i32 @Mailbox_SdGetBaseFrequency() {
%1 = alloca i32
%2 = alloca [256 x i32]
%3 = alloca i32
%4 = alloca i32
store i32 1, i32* %3
%5 = load i32, i32* %3
%6 = add i32 %5, 1
store i32 %6, i32* %3
%7 = zext i32 %5 to i64
%8 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %7
store volatile i32 0, i32* %8
%9 = load i32, i32* %3
%10 = add i32 %9, 1
store i32 %10, i32* %3
%11 = zext i32 %9 to i64
%12 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %11
store volatile i32 196610, i32* %12
%13 = load i32, i32* %3
%14 = add i32 %13, 1
store i32 %14, i32* %3
%15 = zext i32 %13 to i64
%16 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %15
store volatile i32 8, i32* %16
%17 = load i32, i32* %3
%18 = add i32 %17, 1
store i32 %18, i32* %3
%19 = zext i32 %17 to i64
%20 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %19
store volatile i32 4, i32* %20
%21 = load i32, i32* %3
%22 = add i32 %21, 1
store i32 %22, i32* %3
%23 = zext i32 %21 to i64
%24 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %23
store volatile i32 1, i32* %24
%25 = load i32, i32* %3
%26 = add i32 %25, 1
store i32 %26, i32* %3
%27 = zext i32 %25 to i64
%28 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %27
store volatile i32 0, i32* %28
%29 = load i32, i32* %3
%30 = add i32 %29, 1
store i32 %30, i32* %3
%31 = zext i32 %29 to i64
%32 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 %31
store volatile i32 0, i32* %32
%33 = load i32, i32* %3
%34 = mul i32 %33, 4
%35 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 0
store volatile i32 %34, i32* %35
%36 = ptrtoint [256 x i32]* %2 to i32
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = sub i32 %37, -2147483648
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = call i32 @Mailbox_Write(i32 8, i32 %39)
%41 = call i32 @Mailbox_Read(i32 8)
%42 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 1
%43 = load volatile i32, i32* %42
%44 = icmp ne i32 %43, -2147483648
br i1 %44, label %45, label %47
45: ; preds = %0
%46 = call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %56
47: ; preds = %0
%48 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 5
%49 = load volatile i32, i32* %48
%50 = icmp ne i32 %49, 1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %56
53: ; preds = %47
%54 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 6
%55 = load volatile i32, i32* %54
store i32 %55, i32* %1
br label %56
56: ; preds = %53, %51, %45
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@str = [57 x i8] c"Failed to retrieve SD base frequency - Invalid clock id.\00"
@str.2 = [65 x i8] c"Failed to retrieve SD base frequency - Invalid mailbox response.\00"
define i32 @Mailbox_SdGetBaseFrequency() {
%1 = alloca [256 x i32]
%2 = bitcast [256 x i32]* %1 to i8*
%3 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 1
store volatile i32 0, i32* %3
%4 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 2
store volatile i32 196610, i32* %4
%5 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 3
store volatile i32 8, i32* %5
%6 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 4
store volatile i32 4, i32* %6
%7 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 5
store volatile i32 1, i32* %7
%8 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 6
store volatile i32 0, i32* %8
%9 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 7
store volatile i32 0, i32* %9
%10 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 0
store volatile i32 32, i32* %10
%11 = ptrtoint [256 x i32]* %1 to i64
%12 = trunc i64 %11 to i32
%13 = xor i32 %12, -2147483648
%14 = call i32 @Mailbox_Write(i32 8, i32 %13)
%15 = call i32 @Mailbox_Read(i32 8)
%16 = load volatile i32, i32* %3
%17 = icmp eq i32 %16, -2147483648
br i1 %17, label %20, label %18
18: ; preds = %0
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str.2, i64 0, i64 0))
br label %27
20: ; preds = %0
%21 = load volatile i32, i32* %7
%22 = icmp eq i32 %21, 1
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str, i64 0, i64 0))
br label %27
25: ; preds = %20
%26 = load volatile i32, i32* %8
br label %27
27: ; preds = %25, %23, %18
%28 = phi i32 [ -1, %18 ], [ -1, %23 ], [ %26, %25 ]
ret i32 %28
}