#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 inb(int);
int /*<<< orphan*/ outb(int, int);
int /*<<< orphan*/ smbus_wait_until_done();
int /*<<< orphan*/ smbus_wait_until_ready();
int smbus_read_byte(unsigned device, unsigned address) {
  unsigned char host_status_register;
  unsigned char byte;
  int result;
  smbus_wait_until_ready();
  outb(inb(0x1000 + 2) & (~1), 0x1000 + 2);
  outb(((device & 0x7f) << 1) | 1, 0x1000 + 4);
  outb(address & 0xFF, 0x1000 + 3);
  outb((inb(0x1000 + 2) & 0xE3) | (0x2 << 2), 0x1000 + 2);
  outb(inb(0x1000 + 0), 0x1000 + 0);
  outb(0, 0x1000 + 5);
  outb((inb(0x1000 + 2) | 0x40), 0x1000 + 2);
  smbus_wait_until_done();
  host_status_register = inb(0x1000 + 0);
  byte = inb(0x1000 + 5);
  result = byte;
  if (host_status_register != 0x02) {
    result = -1;
  }
  return result;
}


x86 assembly (clang, -O0):
<smbus_read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <smbus_wait_until_ready@plt>
mov    $0x1002,%edi
call   1050 <inb@plt>
mov    %eax,%edi
and    $0xfffffffe,%edi
mov    $0x1002,%esi
call   1040 <outb@plt>
mov    -0x4(%rbp),%edi
and    $0x7f,%edi
shl    $0x1,%edi
or     $0x1,%edi
mov    $0x1004,%esi
call   1040 <outb@plt>
mov    -0x8(%rbp),%edi
and    $0xff,%edi
mov    $0x1003,%esi
call   1040 <outb@plt>
mov    $0x1002,%edi
call   1050 <inb@plt>
mov    %eax,%edi
and    $0xe3,%edi
or     $0x8,%edi
mov    $0x1002,%esi
call   1040 <outb@plt>
mov    $0x1000,%edi
call   1050 <inb@plt>
mov    %eax,%edi
mov    $0x1000,%esi
call   1040 <outb@plt>
xor    %edi,%edi
mov    $0x1005,%esi
call   1040 <outb@plt>
mov    $0x1002,%edi
call   1050 <inb@plt>
mov    %eax,%edi
or     $0x40,%edi
mov    $0x1002,%esi
call   1040 <outb@plt>
mov    $0x0,%al
call   1030 <smbus_wait_until_done@plt>
mov    $0x1000,%edi
call   1050 <inb@plt>
mov    %al,-0x9(%rbp)
mov    $0x1005,%edi
call   1050 <inb@plt>
mov    %al,-0xa(%rbp)
movzbl -0xa(%rbp),%eax
mov    %eax,-0x10(%rbp)
movzbl -0x9(%rbp),%eax
cmp    $0x2,%eax
je     122d <smbus_read_byte+0xed>
movl   $0xffffffff,-0x10(%rbp)
mov    -0x10(%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>
mov    $0x1002,%edi
call   1050 <inb@plt>
and    $0xfffffffe,%eax
mov    %eax,%edi
mov    $0x1002,%esi
call   1040 <outb@plt>
lea    (%rbx,%rbx,1),%eax
add    $0x1,%eax
movzbl %al,%edi
mov    $0x1004,%esi
call   1040 <outb@plt>
movzbl %bpl,%edi
mov    $0x1003,%esi
call   1040 <outb@plt>
mov    $0x1002,%edi
call   1050 <inb@plt>
and    $0xe3,%eax
lea    0x8(%rax),%edi
mov    $0x1002,%esi
call   1040 <outb@plt>
mov    $0x1000,%edi
call   1050 <inb@plt>
mov    %eax,%edi
mov    $0x1000,%esi
call   1040 <outb@plt>
xor    %edi,%edi
mov    $0x1005,%esi
call   1040 <outb@plt>
mov    $0x1002,%edi
call   1050 <inb@plt>
or     $0x40,%eax
mov    %eax,%edi
mov    $0x1002,%esi
call   1040 <outb@plt>
xor    %eax,%eax
call   1030 <smbus_wait_until_done@plt>
mov    $0x1000,%edi
call   1050 <inb@plt>
mov    %eax,%ebx
mov    $0x1005,%edi
call   1050 <inb@plt>
movzbl %al,%ecx
cmp    $0x2,%bl
mov    $0xffffffff,%eax
cmove  %ecx,%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 i8
%6 = alloca i8
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = call i32 (...) @smbus_wait_until_ready()
%9 = call i32 @inb(i32 4098)
%10 = and i32 %9, -2
%11 = call i32 @outb(i32 %10, i32 4098)
%12 = load i32, i32* %3
%13 = and i32 %12, 127
%14 = shl i32 %13, 1
%15 = or i32 %14, 1
%16 = call i32 @outb(i32 %15, i32 4100)
%17 = load i32, i32* %4
%18 = and i32 %17, 255
%19 = call i32 @outb(i32 %18, i32 4099)
%20 = call i32 @inb(i32 4098)
%21 = and i32 %20, 227
%22 = or i32 %21, 8
%23 = call i32 @outb(i32 %22, i32 4098)
%24 = call i32 @inb(i32 4096)
%25 = call i32 @outb(i32 %24, i32 4096)
%26 = call i32 @outb(i32 0, i32 4101)
%27 = call i32 @inb(i32 4098)
%28 = or i32 %27, 64
%29 = call i32 @outb(i32 %28, i32 4098)
%30 = call i32 (...) @smbus_wait_until_done()
%31 = call i32 @inb(i32 4096)
%32 = trunc i32 %31 to i8
store i8 %32, i8* %5
%33 = call i32 @inb(i32 4101)
%34 = trunc i32 %33 to i8
store i8 %34, i8* %6
%35 = load i8, i8* %6
%36 = zext i8 %35 to i32
store i32 %36, i32* %7
%37 = load i8, i8* %5
%38 = zext i8 %37 to i32
%39 = icmp ne i32 %38, 2
br i1 %39, label %40, label %41
40: ; preds = %2
store i32 -1, i32* %7
br label %41
41: ; preds = %40, %2
%42 = load i32, i32* %7
ret i32 %42
}

LLVM IR (-O2):
define i32 @smbus_read_byte(i32 %0, i32 %1) {
%3 = tail call i32 (...) @smbus_wait_until_ready()
%4 = tail call i32 @inb(i32 4098)
%5 = and i32 %4, -2
%6 = tail call i32 @outb(i32 %5, i32 4098)
%7 = shl i32 %0, 1
%8 = and i32 %7, 254
%9 = or i32 %8, 1
%10 = tail call i32 @outb(i32 %9, i32 4100)
%11 = and i32 %1, 255
%12 = tail call i32 @outb(i32 %11, i32 4099)
%13 = tail call i32 @inb(i32 4098)
%14 = and i32 %13, 227
%15 = or i32 %14, 8
%16 = tail call i32 @outb(i32 %15, i32 4098)
%17 = tail call i32 @inb(i32 4096)
%18 = tail call i32 @outb(i32 %17, i32 4096)
%19 = tail call i32 @outb(i32 0, i32 4101)
%20 = tail call i32 @inb(i32 4098)
%21 = or i32 %20, 64
%22 = tail call i32 @outb(i32 %21, i32 4098)
%23 = tail call i32 (...) @smbus_wait_until_done()
%24 = tail call i32 @inb(i32 4096)
%25 = tail call i32 @inb(i32 4101)
%26 = and i32 %25, 255
%27 = and i32 %24, 255
%28 = icmp eq i32 %27, 2
%29 = select i1 %28, i32 %26, i32 -1
ret i32 %29
}