#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 volatile);
int /*<<< orphan*/ wait(int);
int Mailbox_SetDevicePowerState(unsigned int deviceId,
                                unsigned int powerState) {
  volatile unsigned int mailbuffer[256] __attribute__((aligned(16)));
  unsigned int bufSize = 1;
  mailbuffer[bufSize++] = 0;
  mailbuffer[bufSize++] = 0x00028001;
  mailbuffer[bufSize++] = 0x8;
  mailbuffer[bufSize++] = 0x8;
  mailbuffer[bufSize++] = deviceId;
  mailbuffer[bufSize++] = powerState;
  mailbuffer[bufSize++] = 0;
  mailbuffer[0] = bufSize * 4;
  unsigned int bufferAddr = &mailbuffer;
  bufferAddr -= 0x80000000;
  Mailbox_Write(8, bufferAddr);
  wait(200);
  Mailbox_Read(8);
  if (mailbuffer[1] != 0x80000000) {
    printf("Failed to change power state of device '%d', Invalid mailbox "
           "response.\n",
           deviceId);
    return -1;
  }
  if (mailbuffer[5] != 0x0) {
    printf("Failed to change power state of device, invalid device id '%d'.\n",
           deviceId);
    return -1;
  }
  if ((mailbuffer[6] & 0x3) != powerState) {
    printf("Failed to change power state of device, device did not change "
           "state successfully, current state: %d.\n",
           mailbuffer[6]);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<Mailbox_SetDevicePowerState>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%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   $0x28001,-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   $0x8,-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    -0xc(%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
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    $0xc8,%edi
call   1060 <wait@plt>
mov    $0x8,%edi
call   1040 <Mailbox_Read@plt>
mov    -0x40c(%rbp),%eax
cmp    $0x80000000,%eax
je     12a8 <Mailbox_SetDevicePowerState+0x168>
mov    -0x8(%rbp),%esi
lea    0xd69(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <Mailbox_SetDevicePowerState+0x1c9>
mov    -0x3fc(%rbp),%eax
cmp    $0x0,%eax
je     12d2 <Mailbox_SetDevicePowerState+0x192>
mov    -0x8(%rbp),%esi
lea    0xd87(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <Mailbox_SetDevicePowerState+0x1c9>
mov    -0x3f8(%rbp),%eax
and    $0x3,%eax
cmp    -0xc(%rbp),%eax
je     1302 <Mailbox_SetDevicePowerState+0x1c2>
mov    -0x3f8(%rbp),%esi
lea    0xd98(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <Mailbox_SetDevicePowerState+0x1c9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Mailbox_SetDevicePowerState>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
movl   $0x0,0x4(%rsp)
movl   $0x28001,0x8(%rsp)
movl   $0x8,0xc(%rsp)
movl   $0x8,0x10(%rsp)
mov    %edi,0x14(%rsp)
mov    %esi,0x18(%rsp)
movl   $0x0,0x1c(%rsp)
movl   $0x20,(%rsp)
mov    %rsp,%rsi
xor    $0x80000000,%esi
mov    $0x8,%edi
call   1050 <Mailbox_Write@plt>
mov    $0xc8,%edi
call   1060 <wait@plt>
mov    $0x8,%edi
call   1040 <Mailbox_Read@plt>
mov    0x4(%rsp),%eax
cmp    $0x80000000,%eax
jne    11c6 <Mailbox_SetDevicePowerState+0x86>
cmpl   $0x0,0x14(%rsp)
je     11e3 <Mailbox_SetDevicePowerState+0xa3>
lea    0xe84(%rip),%rdi
jmp    11cd <Mailbox_SetDevicePowerState+0x8d>
lea    0xe33(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x18(%rsp),%ecx
and    $0x3,%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
je     11d9 <Mailbox_SetDevicePowerState+0x99>
mov    0x18(%rsp),%esi
lea    0xe8e(%rip),%rdi
jmp    11cf <Mailbox_SetDevicePowerState+0x8f>

LLVM IR (-O0):
@.str = [72 x i8] c"Failed to change power state of device '%d', Invalid mailbox response.\0A\00"
@.str.1 = [65 x i8] c"Failed to change power state of device, invalid device id '%d'.\0A\00"
@.str.2 = [102 x i8] c"Failed to change power state of device, device did not change state successfully, current state: %d.\0A\00"
define i32 @Mailbox_SetDevicePowerState(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i32]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 1, i32* %7
%9 = load i32, i32* %7
%10 = add i32 %9, 1
store i32 %10, i32* %7
%11 = zext i32 %9 to i64
%12 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %11
store volatile i32 0, i32* %12
%13 = load i32, i32* %7
%14 = add i32 %13, 1
store i32 %14, i32* %7
%15 = zext i32 %13 to i64
%16 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %15
store volatile i32 163841, i32* %16
%17 = load i32, i32* %7
%18 = add i32 %17, 1
store i32 %18, i32* %7
%19 = zext i32 %17 to i64
%20 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %19
store volatile i32 8, i32* %20
%21 = load i32, i32* %7
%22 = add i32 %21, 1
store i32 %22, i32* %7
%23 = zext i32 %21 to i64
%24 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %23
store volatile i32 8, i32* %24
%25 = load i32, i32* %4
%26 = load i32, i32* %7
%27 = add i32 %26, 1
store i32 %27, i32* %7
%28 = zext i32 %26 to i64
%29 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %28
store volatile i32 %25, i32* %29
%30 = load i32, i32* %5
%31 = load i32, i32* %7
%32 = add i32 %31, 1
store i32 %32, i32* %7
%33 = zext i32 %31 to i64
%34 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %33
store volatile i32 %30, i32* %34
%35 = load i32, i32* %7
%36 = add i32 %35, 1
store i32 %36, i32* %7
%37 = zext i32 %35 to i64
%38 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %37
store volatile i32 0, i32* %38
%39 = load i32, i32* %7
%40 = mul i32 %39, 4
%41 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 0
store volatile i32 %40, i32* %41
%42 = ptrtoint [256 x i32]* %6 to i32
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = sub i32 %43, -2147483648
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = call i32 @Mailbox_Write(i32 8, i32 %45)
%47 = call i32 @wait(i32 200)
%48 = call i32 @Mailbox_Read(i32 8)
%49 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 1
%50 = load volatile i32, i32* %49
%51 = icmp ne i32 %50, -2147483648
br i1 %51, label %52, label %55
52: ; preds = %2
%53 = load i32, i32* %4
%54 = call i32 @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i32 %53)
store i32 -1, i32* %3
br label %73
55: ; preds = %2
%56 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 5
%57 = load volatile i32, i32* %56
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = load i32, i32* %4
%61 = call i32 @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %60)
store i32 -1, i32* %3
br label %73
62: ; preds = %55
%63 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 6
%64 = load volatile i32, i32* %63
%65 = and i32 %64, 3
%66 = load i32, i32* %5
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %72
68: ; preds = %62
%69 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 6
%70 = load volatile i32, i32* %69
%71 = call i32 @printf(i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.2, i64 0, i64 0), i32 %70)
store i32 -1, i32* %3
br label %73
72: ; preds = %62
store i32 0, i32* %3
br label %73
73: ; preds = %72, %68, %59, %52
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [72 x i8] c"Failed to change power state of device '%d', Invalid mailbox response.\0A\00"
@.str.1 = [65 x i8] c"Failed to change power state of device, invalid device id '%d'.\0A\00"
@.str.2 = [102 x i8] c"Failed to change power state of device, device did not change state successfully, current state: %d.\0A\00"
define i32 @Mailbox_SetDevicePowerState(i32 %0, i32 %1) {
%3 = alloca [256 x i32]
%4 = bitcast [256 x i32]* %3 to i8*
%5 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 1
store volatile i32 0, i32* %5
%6 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 2
store volatile i32 163841, i32* %6
%7 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 3
store volatile i32 8, i32* %7
%8 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 4
store volatile i32 8, i32* %8
%9 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 5
store volatile i32 %0, i32* %9
%10 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 6
store volatile i32 %1, i32* %10
%11 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 7
store volatile i32 0, i32* %11
%12 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 0
store volatile i32 32, i32* %12
%13 = ptrtoint [256 x i32]* %3 to i64
%14 = trunc i64 %13 to i32
%15 = xor i32 %14, -2147483648
%16 = call i32 @Mailbox_Write(i32 8, i32 %15)
%17 = call i32 @wait(i32 200)
%18 = call i32 @Mailbox_Read(i32 8)
%19 = load volatile i32, i32* %5
%20 = icmp eq i32 %19, -2147483648
br i1 %20, label %23, label %21
21: ; preds = %2
%22 = call i32 @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %35
23: ; preds = %2
%24 = load volatile i32, i32* %9
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %35
28: ; preds = %23
%29 = load volatile i32, i32* %10
%30 = and i32 %29, 3
%31 = icmp eq i32 %30, %1
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load volatile i32, i32* %10
%34 = call i32 @printf(i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %28, %32, %26, %21
%36 = phi i32 [ -1, %21 ], [ -1, %26 ], [ -1, %32 ], [ 0, %28 ]
ret i32 %36
}