#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);
scalar_t__ smbus_wait_until_active();
scalar_t__ smbus_wait_until_done();
scalar_t__ smbus_wait_until_ready();
int smbus_read_byte(unsigned device, unsigned address) {
  unsigned char global_status_register;
  unsigned char byte;
  if (smbus_wait_until_ready() < 0) {
    outb(inb(0x5000 + 0x0), 0x5000 + 0x0);
    if (smbus_wait_until_ready() < 0) {
      return -2;
    }
  }
  outb(inb(0x5000 + 0x2) & 0xfe, 0x5000 + 0x2);
  outb(((device & 0x7f) << 1) | 1, 0x5000 + 0x4);
  outb(address & 0xFF, 0x5000 + 0x3);
  outb((inb(0x5000 + 0x2) & 0xe3) | (0x2 << 2), 0x5000 + 0x2);
  outb(inb(0x5000 + 0x0), 0x5000 + 0x0);
  outb(0, 0x5000 + 0x5);
  outb((inb(0x5000 + 0x2) | 0x40), 0x5000 + 0x2);
  if (smbus_wait_until_active() < 0) {
    return -4;
  }
  if (smbus_wait_until_done() < 0) {
    return -3;
  }
  global_status_register = inb(0x5000 + 0x0) & ~((1 << 1) | (1 << 0));
  byte = inb(0x5000 + 0x5);
  if (global_status_register != 0) {
    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   1070 <smbus_wait_until_ready@plt>
cmp    $0x0,%rax
jge    11a7 <smbus_read_byte+0x57>
mov    $0x5000,%edi
call   1060 <inb@plt>
mov    %eax,%edi
mov    $0x5000,%esi
call   1050 <outb@plt>
mov    $0x0,%al
call   1070 <smbus_wait_until_ready@plt>
cmp    $0x0,%rax
jge    11a2 <smbus_read_byte+0x52>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12bd <smbus_read_byte+0x16d>
jmp    11a7 <smbus_read_byte+0x57>
mov    $0x5002,%edi
call   1060 <inb@plt>
mov    %eax,%edi
and    $0xfe,%edi
mov    $0x5002,%esi
call   1050 <outb@plt>
mov    -0x8(%rbp),%edi
and    $0x7f,%edi
shl    $0x1,%edi
or     $0x1,%edi
mov    $0x5004,%esi
call   1050 <outb@plt>
mov    -0xc(%rbp),%edi
and    $0xff,%edi
mov    $0x5003,%esi
call   1050 <outb@plt>
mov    $0x5002,%edi
call   1060 <inb@plt>
mov    %eax,%edi
and    $0xe3,%edi
or     $0x8,%edi
mov    $0x5002,%esi
call   1050 <outb@plt>
mov    $0x5000,%edi
call   1060 <inb@plt>
mov    %eax,%edi
mov    $0x5000,%esi
call   1050 <outb@plt>
xor    %edi,%edi
mov    $0x5005,%esi
call   1050 <outb@plt>
mov    $0x5002,%edi
call   1060 <inb@plt>
mov    %eax,%edi
or     $0x40,%edi
mov    $0x5002,%esi
call   1050 <outb@plt>
mov    $0x0,%al
call   1040 <smbus_wait_until_active@plt>
cmp    $0x0,%rax
jge    1263 <smbus_read_byte+0x113>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12bd <smbus_read_byte+0x16d>
mov    $0x0,%al
call   1030 <smbus_wait_until_done@plt>
cmp    $0x0,%rax
jge    1280 <smbus_read_byte+0x130>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12bd <smbus_read_byte+0x16d>
mov    $0x5000,%edi
call   1060 <inb@plt>
and    $0xfffffffc,%eax
mov    %al,-0xd(%rbp)
mov    $0x5005,%edi
call   1060 <inb@plt>
mov    %al,-0xe(%rbp)
movzbl -0xd(%rbp),%eax
cmp    $0x0,%eax
je     12b6 <smbus_read_byte+0x166>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <smbus_read_byte+0x16d>
movzbl -0xe(%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   1070 <smbus_wait_until_ready@plt>
test   %rax,%rax
jns    1189 <smbus_read_byte+0x39>
mov    $0x5000,%edi
call   1060 <inb@plt>
mov    %eax,%edi
mov    $0x5000,%esi
call   1050 <outb@plt>
xor    %eax,%eax
call   1070 <smbus_wait_until_ready@plt>
test   %rax,%rax
js     1269 <smbus_read_byte+0x119>
mov    $0x5002,%edi
call   1060 <inb@plt>
and    $0xfe,%eax
mov    %eax,%edi
mov    $0x5002,%esi
call   1050 <outb@plt>
lea    (%rbx,%rbx,1),%eax
add    $0x1,%eax
movzbl %al,%edi
mov    $0x5004,%esi
call   1050 <outb@plt>
movzbl %bpl,%edi
mov    $0x5003,%esi
call   1050 <outb@plt>
mov    $0x5002,%edi
call   1060 <inb@plt>
and    $0xe3,%eax
lea    0x8(%rax),%edi
mov    $0x5002,%esi
call   1050 <outb@plt>
mov    $0x5000,%edi
call   1060 <inb@plt>
mov    %eax,%edi
mov    $0x5000,%esi
call   1050 <outb@plt>
xor    %edi,%edi
mov    $0x5005,%esi
call   1050 <outb@plt>
mov    $0x5002,%edi
call   1060 <inb@plt>
or     $0x40,%eax
mov    %eax,%edi
mov    $0x5002,%esi
call   1050 <outb@plt>
xor    %eax,%eax
call   1040 <smbus_wait_until_active@plt>
test   %rax,%rax
js     125b <smbus_read_byte+0x10b>
xor    %eax,%eax
call   1030 <smbus_wait_until_done@plt>
test   %rax,%rax
js     1262 <smbus_read_byte+0x112>
mov    $0x5000,%edi
call   1060 <inb@plt>
mov    %eax,%ebx
and    $0xfc,%ebx
mov    $0x5005,%edi
call   1060 <inb@plt>
movzbl %al,%ecx
neg    %ebx
sbb    %eax,%eax
or     %ecx,%eax
jmp    126e <smbus_read_byte+0x11e>
mov    $0xfffffffc,%eax
jmp    126e <smbus_read_byte+0x11e>
mov    $0xfffffffd,%eax
jmp    126e <smbus_read_byte+0x11e>
mov    $0xfffffffe,%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
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i64 (...) @smbus_wait_until_ready()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = call i32 @inb(i32 20480)
%12 = call i32 @outb(i32 %11, i32 20480)
%13 = call i64 (...) @smbus_wait_until_ready()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10
store i32 -2, i32* %3
br label %59
16: ; preds = %10
br label %17
17: ; preds = %16, %2
%18 = call i32 @inb(i32 20482)
%19 = and i32 %18, 254
%20 = call i32 @outb(i32 %19, i32 20482)
%21 = load i32, i32* %4
%22 = and i32 %21, 127
%23 = shl i32 %22, 1
%24 = or i32 %23, 1
%25 = call i32 @outb(i32 %24, i32 20484)
%26 = load i32, i32* %5
%27 = and i32 %26, 255
%28 = call i32 @outb(i32 %27, i32 20483)
%29 = call i32 @inb(i32 20482)
%30 = and i32 %29, 227
%31 = or i32 %30, 8
%32 = call i32 @outb(i32 %31, i32 20482)
%33 = call i32 @inb(i32 20480)
%34 = call i32 @outb(i32 %33, i32 20480)
%35 = call i32 @outb(i32 0, i32 20485)
%36 = call i32 @inb(i32 20482)
%37 = or i32 %36, 64
%38 = call i32 @outb(i32 %37, i32 20482)
%39 = call i64 (...) @smbus_wait_until_active()
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %17
store i32 -4, i32* %3
br label %59
42: ; preds = %17
%43 = call i64 (...) @smbus_wait_until_done()
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 -3, i32* %3
br label %59
46: ; preds = %42
%47 = call i32 @inb(i32 20480)
%48 = and i32 %47, -4
%49 = trunc i32 %48 to i8
store i8 %49, i8* %6
%50 = call i32 @inb(i32 20485)
%51 = trunc i32 %50 to i8
store i8 %51, i8* %7
%52 = load i8, i8* %6
%53 = zext i8 %52 to i32
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %46
store i32 -1, i32* %3
br label %59
56: ; preds = %46
%57 = load i8, i8* %7
%58 = zext i8 %57 to i32
store i32 %58, i32* %3
br label %59
59: ; preds = %56, %55, %45, %41, %15
%60 = load i32, i32* %3
ret i32 %60
}

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 %5, label %10
5: ; preds = %2
%6 = tail call i32 @inb(i32 20480)
%7 = tail call i32 @outb(i32 %6, i32 20480)
%8 = tail call i64 (...) @smbus_wait_until_ready()
%9 = icmp slt i64 %8, 0
br i1 %9, label %42, label %10
10: ; preds = %5, %2
%11 = tail call i32 @inb(i32 20482)
%12 = and i32 %11, 254
%13 = tail call i32 @outb(i32 %12, i32 20482)
%14 = shl i32 %0, 1
%15 = and i32 %14, 254
%16 = or i32 %15, 1
%17 = tail call i32 @outb(i32 %16, i32 20484)
%18 = and i32 %1, 255
%19 = tail call i32 @outb(i32 %18, i32 20483)
%20 = tail call i32 @inb(i32 20482)
%21 = and i32 %20, 227
%22 = or i32 %21, 8
%23 = tail call i32 @outb(i32 %22, i32 20482)
%24 = tail call i32 @inb(i32 20480)
%25 = tail call i32 @outb(i32 %24, i32 20480)
%26 = tail call i32 @outb(i32 0, i32 20485)
%27 = tail call i32 @inb(i32 20482)
%28 = or i32 %27, 64
%29 = tail call i32 @outb(i32 %28, i32 20482)
%30 = tail call i64 (...) @smbus_wait_until_active()
%31 = icmp slt i64 %30, 0
br i1 %31, label %42, label %32
32: ; preds = %10
%33 = tail call i64 (...) @smbus_wait_until_done()
%34 = icmp slt i64 %33, 0
br i1 %34, label %42, label %35
35: ; preds = %32
%36 = tail call i32 @inb(i32 20480)
%37 = and i32 %36, 252
%38 = tail call i32 @inb(i32 20485)
%39 = icmp eq i32 %37, 0
%40 = and i32 %38, 255
%41 = select i1 %39, i32 %40, i32 -1
br label %42
42: ; preds = %35, %32, %10, %5
%43 = phi i32 [ -2, %5 ], [ -4, %10 ], [ -3, %32 ], [ %41, %35 ]
ret i32 %43
}