#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*/ OS_disable_interrupts(unsigned long *);
int /*<<< orphan*/ OS_enable_interrupts(unsigned long);
int lpc_read_reg(int, unsigned long *);
int lpc_write_reg(int, unsigned int);
int blender_read_reg(unsigned int reg, unsigned int *p_value) {
  int err;
  unsigned long value;
  unsigned long ie;
  if (reg & 1)
    return 0x1000;
  OS_disable_interrupts(&ie);
  err = lpc_write_reg(0x04, (1 << 20) + (3 << 16));
  if (err) {
    OS_enable_interrupts(ie);
    return err;
  }
  err = lpc_write_reg(0x0C, (reg << 16) | 0x08 | 0x02);
  if (err) {
    OS_enable_interrupts(ie);
    return err;
  }
  err = lpc_read_reg(0x00, &value);
  if (err) {
    OS_enable_interrupts(ie);
    return err;
  }
  OS_enable_interrupts(ie);
  *p_value = (unsigned int)(value & 0xFFFF);
  return 0;
}


x86 assembly (clang, -O0):
<blender_read_reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     116a <blender_read_reg+0x2a>
movl   $0x1000,-0x4(%rbp)
jmp    1228 <blender_read_reg+0xe8>
lea    -0x28(%rbp),%rdi
call   1050 <OS_disable_interrupts@plt>
mov    $0x4,%edi
mov    $0x130000,%esi
call   1040 <lpc_write_reg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11a3 <blender_read_reg+0x63>
mov    -0x28(%rbp),%rdi
call   1030 <OS_enable_interrupts@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <blender_read_reg+0xe8>
mov    -0x8(%rbp),%esi
shl    $0x10,%esi
or     $0x8,%esi
or     $0x2,%esi
mov    $0xc,%edi
call   1040 <lpc_write_reg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11da <blender_read_reg+0x9a>
mov    -0x28(%rbp),%rdi
call   1030 <OS_enable_interrupts@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <blender_read_reg+0xe8>
xor    %edi,%edi
lea    -0x20(%rbp),%rsi
call   1060 <lpc_read_reg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1206 <blender_read_reg+0xc6>
mov    -0x28(%rbp),%rdi
call   1030 <OS_enable_interrupts@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <blender_read_reg+0xe8>
mov    -0x28(%rbp),%rdi
call   1030 <OS_enable_interrupts@plt>
mov    -0x20(%rbp),%rax
and    $0xffff,%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blender_read_reg>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    $0x1000,%ebp
test   $0x1,%bl
jne    1193 <blender_read_reg+0x53>
mov    %rsi,%r14
mov    %rsp,%rdi
call   1050 <OS_disable_interrupts@plt>
mov    $0x4,%edi
mov    $0x130000,%esi
call   1040 <lpc_write_reg@plt>
test   %eax,%eax
jne    1188 <blender_read_reg+0x48>
shl    $0x10,%ebx
or     $0xa,%ebx
mov    $0xc,%edi
mov    %ebx,%esi
call   1040 <lpc_write_reg@plt>
test   %eax,%eax
je     119e <blender_read_reg+0x5e>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1030 <OS_enable_interrupts@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1060 <lpc_read_reg@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1030 <OS_enable_interrupts@plt>
test   %ebp,%ebp
jne    1193 <blender_read_reg+0x53>
movzwl 0x8(%rsp),%eax
mov    %eax,(%r14)
xor    %ebp,%ebp
jmp    1193 <blender_read_reg+0x53>

LLVM IR (-O0):
define i32 @blender_read_reg(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i32* %1, i32** %5
%9 = load i32, i32* %4
%10 = and i32 %9, 1
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 4096, i32* %3
br label %49
13: ; preds = %2
%14 = call i32 @OS_disable_interrupts(i64* %8)
%15 = call i32 @lpc_write_reg(i32 4, i32 1245184)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i64, i64* %8
%20 = call i32 @OS_enable_interrupts(i64 %19)
%21 = load i32, i32* %6
store i32 %21, i32* %3
br label %49
22: ; preds = %13
%23 = load i32, i32* %4
%24 = shl i32 %23, 16
%25 = or i32 %24, 8
%26 = or i32 %25, 2
%27 = call i32 @lpc_write_reg(i32 12, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %22
%31 = load i64, i64* %8
%32 = call i32 @OS_enable_interrupts(i64 %31)
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %49
34: ; preds = %22
%35 = call i32 @lpc_read_reg(i32 0, i64* %7)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i64, i64* %8
%40 = call i32 @OS_enable_interrupts(i64 %39)
%41 = load i32, i32* %6
store i32 %41, i32* %3
br label %49
42: ; preds = %34
%43 = load i64, i64* %8
%44 = call i32 @OS_enable_interrupts(i64 %43)
%45 = load i64, i64* %7
%46 = and i64 %45, 65535
%47 = trunc i64 %46 to i32
%48 = load i32*, i32** %5
store i32 %47, i32* %48
store i32 0, i32* %3
br label %49
49: ; preds = %42, %38, %30, %18, %12
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
define i32 @blender_read_reg(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = and i32 %0, 1
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %33
9: ; preds = %2
%10 = call i32 @OS_disable_interrupts(i64* %4)
%11 = call i32 @lpc_write_reg(i32 4, i32 1245184)
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = load i64, i64* %4
%15 = call i32 @OS_enable_interrupts(i64 %14)
br label %33
16: ; preds = %9
%17 = shl i32 %0, 16
%18 = or i32 %17, 10
%19 = call i32 @lpc_write_reg(i32 12, i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i64, i64* %4
%23 = call i32 @OS_enable_interrupts(i64 %22)
br label %33
24: ; preds = %16
%25 = call i32 @lpc_read_reg(i32 0, i64* %3)
%26 = icmp eq i32 %25, 0
%27 = load i64, i64* %4
%28 = call i32 @OS_enable_interrupts(i64 %27)
br i1 %26, label %29, label %33
29: ; preds = %24
%30 = load i64, i64* %3
%31 = trunc i64 %30 to i32
%32 = and i32 %31, 65535
store i32 %32, i32* %1
br label %33
33: ; preds = %24, %2, %29, %21, %13
%34 = phi i32 [ %11, %13 ], [ %19, %21 ], [ 0, %29 ], [ 4096, %2 ], [ %25, %24 ]
ret i32 %34
}