#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_GetPowerState(unsigned int deviceId) {
  volatile unsigned int mailbuffer[256] __attribute__((aligned(16)));
  unsigned int bufSize = 1;
  mailbuffer[bufSize++] = 0;
  mailbuffer[bufSize++] = 0x00020001;
  mailbuffer[bufSize++] = 0x8;
  mailbuffer[bufSize++] = 0x4;
  mailbuffer[bufSize++] = deviceId;
  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 get power state, invalid mailbox response");
    return -1;
  }
  return mailbuffer[6];
}


x86 assembly (clang, -O0):
<Mailbox_GetPowerState>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
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   $0x20001,-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    -0x8(%rbp),%ecx
mov    -0x414(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x414(%rbp)
mov    %eax,%eax
mov    %ecx,-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     1289 <Mailbox_GetPowerState+0x159>
lea    0xd88(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <Mailbox_GetPowerState+0x162>
mov    -0x3f8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Mailbox_GetPowerState>:
sub    $0x408,%rsp
movl   $0x0,0x4(%rsp)
movl   $0x20001,0x8(%rsp)
movl   $0x8,0xc(%rsp)
movl   $0x4,0x10(%rsp)
mov    %edi,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    11a6 <Mailbox_GetPowerState+0x76>
mov    0x18(%rsp),%eax
add    $0x408,%rsp
ret
lea    0xe53(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
ret

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

LLVM IR (-O2):
@.str = [52 x i8] c"Failed to get power state, invalid mailbox response\00"
define i32 @Mailbox_GetPowerState(i32 %0) {
%2 = alloca [256 x i32]
%3 = bitcast [256 x i32]* %2 to i8*
%4 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 1
store volatile i32 0, i32* %4
%5 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 2
store volatile i32 131073, i32* %5
%6 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 3
store volatile i32 8, i32* %6
%7 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 4
store volatile i32 4, i32* %7
%8 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 5
store volatile i32 %0, i32* %8
%9 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 6
store volatile i32 0, i32* %9
%10 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 7
store volatile i32 0, i32* %10
%11 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 0
store volatile i32 32, i32* %11
%12 = ptrtoint [256 x i32]* %2 to i64
%13 = trunc i64 %12 to i32
%14 = xor i32 %13, -2147483648
%15 = call i32 @Mailbox_Write(i32 8, i32 %14)
%16 = call i32 @Mailbox_Read(i32 8)
%17 = load volatile i32, i32* %4
%18 = icmp eq i32 %17, -2147483648
br i1 %18, label %21, label %19
19: ; preds = %1
%20 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
br label %23
21: ; preds = %1
%22 = load volatile i32, i32* %9
br label %23
23: ; preds = %21, %19
%24 = phi i32 [ -1, %19 ], [ %22, %21 ]
ret i32 %24
}