#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 sio_read_reg(int, unsigned long *);
int sio_write_reg(int, unsigned long);
int power_down(void) {
  int err;
  unsigned long reg;
  err = sio_read_reg(0x0C, &reg);
  if (err)
    return err;
  reg |= 0x0001;
  err = sio_write_reg(0x0C, reg);
  if (err)
    return err;
  reg |= 0x0002;
  err = sio_write_reg(0x0C, reg);
  if (err)
    return err;
  return 0;
}


x86 assembly (clang, -O0):
<power_down>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0xc,%edi
lea    -0x10(%rbp),%rsi
call   1040 <sio_read_reg@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     114e <power_down+0x2e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <power_down+0x99>
mov    -0x10(%rbp),%rax
or     $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    $0xc,%edi
call   1030 <sio_write_reg@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1180 <power_down+0x60>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <power_down+0x99>
mov    -0x10(%rbp),%rax
or     $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    $0xc,%edi
call   1030 <sio_write_reg@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11b2 <power_down+0x92>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <power_down+0x99>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<power_down>:
push   %rax
mov    %rsp,%rsi
mov    $0xc,%edi
call   1040 <sio_read_reg@plt>
test   %eax,%eax
je     1134 <power_down+0x14>
pop    %rcx
ret
mov    (%rsp),%rsi
or     $0x1,%rsi
mov    %rsi,(%rsp)
mov    $0xc,%edi
call   1030 <sio_write_reg@plt>
test   %eax,%eax
jne    1132 <power_down+0x12>
mov    (%rsp),%rsi
or     $0x2,%rsi
mov    %rsi,(%rsp)
mov    $0xc,%edi
call   1030 <sio_write_reg@plt>
pop    %rcx
ret

LLVM IR (-O0):
define i32 @power_down() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = call i32 @sio_read_reg(i32 12, i64* %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = load i32, i32* %2
store i32 %8, i32* %1
br label %28
9: ; preds = %0
%10 = load i64, i64* %3
%11 = or i64 %10, 1
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = call i32 @sio_write_reg(i32 12, i64 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = load i32, i32* %2
store i32 %17, i32* %1
br label %28
18: ; preds = %9
%19 = load i64, i64* %3
%20 = or i64 %19, 2
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = call i32 @sio_write_reg(i32 12, i64 %21)
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = load i32, i32* %2
store i32 %26, i32* %1
br label %28
27: ; preds = %18
store i32 0, i32* %1
br label %28
28: ; preds = %27, %25, %16, %7
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
define i32 @power_down() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = call i32 @sio_read_reg(i32 12, i64* %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %0
%6 = load i64, i64* %1
%7 = or i64 %6, 1
store i64 %7, i64* %1
%8 = call i32 @sio_write_reg(i32 12, i64 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = load i64, i64* %1
%12 = or i64 %11, 2
store i64 %12, i64* %1
%13 = call i32 @sio_write_reg(i32 12, i64 %12)
br label %14
14: ; preds = %10, %5, %0
%15 = phi i32 [ %3, %0 ], [ %8, %5 ], [ %13, %10 ]
ret i32 %15
}