#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 inw(int);
int /*<<< orphan*/ outb(unsigned int, int);
int /*<<< orphan*/ outw(int, int);
scalar_t__ smbus_wait_until_done();
scalar_t__ smbus_wait_until_ready();
int smbus_read_byte(unsigned device, unsigned address) {
  unsigned char global_control_register;
  unsigned char global_status_register;
  unsigned char byte;
  if (smbus_wait_until_ready() < 0) {
    return -2;
  }
  outw(inw(0x0f00 + 0xe2) & ~((1 << 10) | (1 << 9) | (1 << 8) | (1 << 4)),
       0x0f00 + 0xe2);
  outw(((device & 0x7f) << 1) | 1, 0x0f00 + 0xe4);
  outb(address & 0xFF, 0x0f00 + 0xe8);
  outw((inw(0x0f00 + 0xe2) & ~7) | (0x2), 0x0f00 + 0xe2);
  outw(inw(0x0f00 + 0xe0), 0x0f00 + 0xe0);
  outw(0, 0x0f00 + 0xe6);
  outw((inw(0x0f00 + 0xe2) | (1 << 3)), 0x0f00 + 0xe2);
  if (smbus_wait_until_done() < 0) {
    return -3;
  }
  global_status_register = inw(0x0f00 + 0xe0);
  byte = inw(0x0f00 + 0xe6) & 0xff;
  if (global_status_register != (1 << 4)) {
    return -1;
  }
  return byte;
}


x86 assembly (clang, -O0):
<smbus_read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1060 <smbus_wait_until_ready@plt>
cmp    $0x0,%rax
jge    117b <smbus_read_byte+0x2b>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1273 <smbus_read_byte+0x123>
mov    $0xfe2,%edi
call   1030 <inw@plt>
mov    %eax,%edi
and    $0xfffff8ef,%edi
mov    $0xfe2,%esi
call   1070 <outw@plt>
mov    -0x8(%rbp),%edi
and    $0x7f,%edi
shl    $0x1,%edi
or     $0x1,%edi
mov    $0xfe4,%esi
call   1070 <outw@plt>
mov    -0xc(%rbp),%edi
and    $0xff,%edi
mov    $0xfe8,%esi
call   1050 <outb@plt>
mov    $0xfe2,%edi
call   1030 <inw@plt>
mov    %eax,%edi
and    $0xfffffff8,%edi
or     $0x2,%edi
mov    $0xfe2,%esi
call   1070 <outw@plt>
mov    $0xfe0,%edi
call   1030 <inw@plt>
mov    %eax,%edi
mov    $0xfe0,%esi
call   1070 <outw@plt>
xor    %edi,%edi
mov    $0xfe6,%esi
call   1070 <outw@plt>
mov    $0xfe2,%edi
call   1030 <inw@plt>
mov    %eax,%edi
or     $0x8,%edi
mov    $0xfe2,%esi
call   1070 <outw@plt>
mov    $0x0,%al
call   1040 <smbus_wait_until_done@plt>
cmp    $0x0,%rax
jge    1234 <smbus_read_byte+0xe4>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1273 <smbus_read_byte+0x123>
mov    $0xfe0,%edi
call   1030 <inw@plt>
mov    %al,-0xe(%rbp)
mov    $0xfe6,%edi
call   1030 <inw@plt>
and    $0xff,%eax
mov    %al,-0xf(%rbp)
movzbl -0xe(%rbp),%eax
cmp    $0x10,%eax
je     126c <smbus_read_byte+0x11c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <smbus_read_byte+0x123>
movzbl -0xf(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smbus_read_byte>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
xor    %eax,%eax
call   1060 <smbus_wait_until_ready@plt>
test   %rax,%rax
js     122a <smbus_read_byte+0xda>
mov    $0xfe2,%edi
call   1030 <inw@plt>
and    $0xfffff8ef,%eax
mov    %eax,%edi
mov    $0xfe2,%esi
call   1070 <outw@plt>
lea    (%rbx,%rbx,1),%eax
add    $0x1,%eax
movzbl %al,%edi
mov    $0xfe4,%esi
call   1070 <outw@plt>
movzbl %bpl,%edi
mov    $0xfe8,%esi
call   1050 <outb@plt>
mov    $0xfe2,%edi
call   1030 <inw@plt>
and    $0xfffffff8,%eax
lea    0x2(%rax),%edi
mov    $0xfe2,%esi
call   1070 <outw@plt>
mov    $0xfe0,%edi
call   1030 <inw@plt>
mov    %eax,%edi
mov    $0xfe0,%esi
call   1070 <outw@plt>
xor    %edi,%edi
mov    $0xfe6,%esi
call   1070 <outw@plt>
mov    $0xfe2,%edi
call   1030 <inw@plt>
or     $0x8,%eax
mov    %eax,%edi
mov    $0xfe2,%esi
call   1070 <outw@plt>
xor    %eax,%eax
call   1040 <smbus_wait_until_done@plt>
test   %rax,%rax
js     1231 <smbus_read_byte+0xe1>
mov    $0xfe0,%edi
call   1030 <inw@plt>
mov    %eax,%ebx
mov    $0xfe6,%edi
call   1030 <inw@plt>
cmp    $0x10,%bl
movzbl %al,%ecx
mov    $0xffffffff,%eax
cmove  %ecx,%eax
jmp    1236 <smbus_read_byte+0xe6>
mov    $0xfffffffe,%eax
jmp    1236 <smbus_read_byte+0xe6>
mov    $0xfffffffd,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @smbus_read_byte(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = call i64 (...) @smbus_wait_until_ready()
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -2, i32* %3
br label %50
12: ; preds = %2
%13 = call i32 @inw(i32 4066)
%14 = and i32 %13, -1809
%15 = call i32 @outw(i32 %14, i32 4066)
%16 = load i32, i32* %4
%17 = and i32 %16, 127
%18 = shl i32 %17, 1
%19 = or i32 %18, 1
%20 = call i32 @outw(i32 %19, i32 4068)
%21 = load i32, i32* %5
%22 = and i32 %21, 255
%23 = call i32 @outb(i32 %22, i32 4072)
%24 = call i32 @inw(i32 4066)
%25 = and i32 %24, -8
%26 = or i32 %25, 2
%27 = call i32 @outw(i32 %26, i32 4066)
%28 = call i32 @inw(i32 4064)
%29 = call i32 @outw(i32 %28, i32 4064)
%30 = call i32 @outw(i32 0, i32 4070)
%31 = call i32 @inw(i32 4066)
%32 = or i32 %31, 8
%33 = call i32 @outw(i32 %32, i32 4066)
%34 = call i64 (...) @smbus_wait_until_done()
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %12
store i32 -3, i32* %3
br label %50
37: ; preds = %12
%38 = call i32 @inw(i32 4064)
%39 = trunc i32 %38 to i8
store i8 %39, i8* %7
%40 = call i32 @inw(i32 4070)
%41 = and i32 %40, 255
%42 = trunc i32 %41 to i8
store i8 %42, i8* %8
%43 = load i8, i8* %7
%44 = zext i8 %43 to i32
%45 = icmp ne i32 %44, 16
br i1 %45, label %46, label %47
46: ; preds = %37
store i32 -1, i32* %3
br label %50
47: ; preds = %37
%48 = load i8, i8* %8
%49 = zext i8 %48 to i32
store i32 %49, i32* %3
br label %50
50: ; preds = %47, %46, %36, %11
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
define i32 @smbus_read_byte(i32 %0, i32 %1) {
%3 = tail call i64 (...) @smbus_wait_until_ready()
%4 = icmp slt i64 %3, 0
br i1 %4, label %34, label %5
5: ; preds = %2
%6 = tail call i32 @inw(i32 4066)
%7 = and i32 %6, -1809
%8 = tail call i32 @outw(i32 %7, i32 4066)
%9 = shl i32 %0, 1
%10 = and i32 %9, 254
%11 = or i32 %10, 1
%12 = tail call i32 @outw(i32 %11, i32 4068)
%13 = and i32 %1, 255
%14 = tail call i32 @outb(i32 %13, i32 4072)
%15 = tail call i32 @inw(i32 4066)
%16 = and i32 %15, -8
%17 = or i32 %16, 2
%18 = tail call i32 @outw(i32 %17, i32 4066)
%19 = tail call i32 @inw(i32 4064)
%20 = tail call i32 @outw(i32 %19, i32 4064)
%21 = tail call i32 @outw(i32 0, i32 4070)
%22 = tail call i32 @inw(i32 4066)
%23 = or i32 %22, 8
%24 = tail call i32 @outw(i32 %23, i32 4066)
%25 = tail call i64 (...) @smbus_wait_until_done()
%26 = icmp slt i64 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %5
%28 = tail call i32 @inw(i32 4064)
%29 = tail call i32 @inw(i32 4070)
%30 = and i32 %28, 255
%31 = icmp eq i32 %30, 16
%32 = and i32 %29, 255
%33 = select i1 %31, i32 %32, i32 -1
br label %34
34: ; preds = %27, %5, %2
%35 = phi i32 [ -2, %2 ], [ -3, %5 ], [ %33, %27 ]
ret i32 %35
}