#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();
short smbus_read_byte(unsigned char device, unsigned char address) {
  unsigned char host_status_register;
  short 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    %sil,%al
mov    %dil,%cl
mov    %cl,-0x1(%rbp)
mov    %al,-0x2(%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>
movzbl -0x1(%rbp),%edi
and    $0x7f,%edi
shl    $0x1,%edi
or     $0x1,%edi
mov    $0x1004,%esi
call   1040 <outb@plt>
movzbl -0x2(%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,-0x3(%rbp)
mov    $0x1005,%edi
call   1050 <inb@plt>
mov    %ax,-0x6(%rbp)
movzbl -0x3(%rbp),%eax
cmp    $0x2,%eax
je     122e <smbus_read_byte+0xee>
movw   $0xffff,-0x6(%rbp)
movswl -0x6(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smbus_read_byte>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
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>
add    %bpl,%bpl
or     $0x1,%bpl
movzbl %bpl,%edi
mov    $0x1004,%esi
call   1040 <outb@plt>
mov    %ebx,%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    $0xffff,%ecx
cmovne %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define signext i16 @smbus_read_byte(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca i8
%4 = alloca i8
%5 = alloca i8
%6 = alloca i16
store i8 %0, i8* %3
store i8 %1, i8* %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 i8, i8* %3
%12 = zext i8 %11 to i32
%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 i8, i8* %4
%18 = zext i8 %17 to i32
%19 = and i32 %18, 255
%20 = call i32 @outb(i32 %19, i32 4099)
%21 = call i32 @inb(i32 4098)
%22 = and i32 %21, 227
%23 = or i32 %22, 8
%24 = call i32 @outb(i32 %23, i32 4098)
%25 = call i32 @inb(i32 4096)
%26 = call i32 @outb(i32 %25, i32 4096)
%27 = call i32 @outb(i32 0, i32 4101)
%28 = call i32 @inb(i32 4098)
%29 = or i32 %28, 64
%30 = call i32 @outb(i32 %29, i32 4098)
%31 = call i32 (...) @smbus_wait_until_done()
%32 = call i32 @inb(i32 4096)
%33 = trunc i32 %32 to i8
store i8 %33, i8* %5
%34 = call i32 @inb(i32 4101)
%35 = trunc i32 %34 to i16
store i16 %35, i16* %6
%36 = load i8, i8* %5
%37 = zext i8 %36 to i32
%38 = icmp ne i32 %37, 2
br i1 %38, label %39, label %40
39: ; preds = %2
store i16 -1, i16* %6
br label %40
40: ; preds = %39, %2
%41 = load i16, i16* %6
ret i16 %41
}

LLVM IR (-O2):
define signext i16 @smbus_read_byte(i8 zeroext %0, i8 zeroext %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 i8 %0, 1
%8 = or i8 %7, 1
%9 = zext i8 %8 to i32
%10 = tail call i32 @outb(i32 %9, i32 4100)
%11 = zext i8 %1 to i32
%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 = trunc i32 %25 to i16
%27 = and i32 %24, 255
%28 = icmp eq i32 %27, 2
%29 = select i1 %28, i16 %26, i16 -1
ret i16 %29
}