#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;
  int result;
  smbus_wait_until_ready();
  outb(inb(0x1002) & (~1), 0x1002);
  outb(((device & 0x7f) << 1) | 1, 0x1004);
  outb(address & 0xFF, 0x1003);
  outb((inb(0x1002) & 0xE3) | 8, 0x1002);
  outb(inb(0x1000), 0x1000);
  outb(0, 0x1005);
  outb((inb(0x1002) | 0x40), 0x1002);
  smbus_wait_until_done();
  host_status_register = inb(0x1000);
  result = inb(0x1005);
  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    %eax,-0x10(%rbp)
movzbl -0x9(%rbp),%eax
cmp    $0x2,%eax
je     1226 <smbus_read_byte+0xe6>
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>
cmp    $0x2,%bl
mov    $0xffffffff,%ecx
cmovne %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 i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i32 (...) @smbus_wait_until_ready()
%8 = call i32 @inb(i32 4098)
%9 = and i32 %8, -2
%10 = call i32 @outb(i32 %9, i32 4098)
%11 = load i32, i32* %3
%12 = and i32 %11, 127
%13 = shl i32 %12, 1
%14 = or i32 %13, 1
%15 = call i32 @outb(i32 %14, i32 4100)
%16 = load i32, i32* %4
%17 = and i32 %16, 255
%18 = call i32 @outb(i32 %17, i32 4099)
%19 = call i32 @inb(i32 4098)
%20 = and i32 %19, 227
%21 = or i32 %20, 8
%22 = call i32 @outb(i32 %21, i32 4098)
%23 = call i32 @inb(i32 4096)
%24 = call i32 @outb(i32 %23, i32 4096)
%25 = call i32 @outb(i32 0, i32 4101)
%26 = call i32 @inb(i32 4098)
%27 = or i32 %26, 64
%28 = call i32 @outb(i32 %27, i32 4098)
%29 = call i32 (...) @smbus_wait_until_done()
%30 = call i32 @inb(i32 4096)
%31 = trunc i32 %30 to i8
store i8 %31, i8* %5
%32 = call i32 @inb(i32 4101)
store i32 %32, i32* %6
%33 = load i8, i8* %5
%34 = zext i8 %33 to i32
%35 = icmp ne i32 %34, 2
br i1 %35, label %36, label %37
36: ; preds = %2
store i32 -1, i32* %6
br label %37
37: ; preds = %36, %2
%38 = load i32, i32* %6
ret i32 %38
}

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 %24, 255
%27 = icmp eq i32 %26, 2
%28 = select i1 %27, i32 %25, i32 -1
ret i32 %28
}