#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*/ print_err(char *);
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) {
    print_err("SMBUS not ready (-2)\n");
    return -2;
  }
  outb(inb(0x1000 + 0x2) & 0xfe, 0x1000 + 0x2);
  outb(((device & 0x7f) << 1) | 1, 0x1000 + 0x4);
  outb(address & 0xFF, 0x1000 + 0x3);
  outb((inb(0x1000 + 0x2) & 0xe3) | (0x2 << 2), 0x1000 + 0x2);
  outb(inb(0x1000 + 0x0), 0x1000 + 0x0);
  outb(0, 0x1000 + 0x5);
  outb((inb(0x1000 + 0x2) | 0x40), 0x1000 + 0x2);
  if (smbus_wait_until_active() < 0) {
    print_err("SMBUS not active (-4)\n");
    return -4;
  }
  if (smbus_wait_until_done() < 0) {
    print_err("SMBUS not completed (-3)\n");
    return -3;
  }
  global_status_register = inb(0x1000 + 0x0) & ~(1 << 6);
  byte = inb(0x1000 + 0x5);
  if (global_status_register != 2) {
    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   1080 <smbus_wait_until_ready@plt>
cmp    $0x0,%rax
jge    1197 <smbus_read_byte+0x37>
lea    0xe7a(%rip),%rdi
call   1050 <print_err@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12c5 <smbus_read_byte+0x165>
mov    $0x1002,%edi
call   1070 <inb@plt>
mov    %eax,%edi
and    $0xfe,%edi
mov    $0x1002,%esi
call   1060 <outb@plt>
mov    -0x8(%rbp),%edi
and    $0x7f,%edi
shl    $0x1,%edi
or     $0x1,%edi
mov    $0x1004,%esi
call   1060 <outb@plt>
mov    -0xc(%rbp),%edi
and    $0xff,%edi
mov    $0x1003,%esi
call   1060 <outb@plt>
mov    $0x1002,%edi
call   1070 <inb@plt>
mov    %eax,%edi
and    $0xe3,%edi
or     $0x8,%edi
mov    $0x1002,%esi
call   1060 <outb@plt>
mov    $0x1000,%edi
call   1070 <inb@plt>
mov    %eax,%edi
mov    $0x1000,%esi
call   1060 <outb@plt>
xor    %edi,%edi
mov    $0x1005,%esi
call   1060 <outb@plt>
mov    $0x1002,%edi
call   1070 <inb@plt>
mov    %eax,%edi
or     $0x40,%edi
mov    $0x1002,%esi
call   1060 <outb@plt>
mov    $0x0,%al
call   1040 <smbus_wait_until_active@plt>
cmp    $0x0,%rax
jge    125f <smbus_read_byte+0xff>
lea    0xdc8(%rip),%rdi
call   1050 <print_err@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12c5 <smbus_read_byte+0x165>
mov    $0x0,%al
call   1030 <smbus_wait_until_done@plt>
cmp    $0x0,%rax
jge    1288 <smbus_read_byte+0x128>
lea    0xdb6(%rip),%rdi
call   1050 <print_err@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12c5 <smbus_read_byte+0x165>
mov    $0x1000,%edi
call   1070 <inb@plt>
and    $0xffffffbf,%eax
mov    %al,-0xd(%rbp)
mov    $0x1005,%edi
call   1070 <inb@plt>
mov    %al,-0xe(%rbp)
movzbl -0xd(%rbp),%eax
cmp    $0x2,%eax
je     12be <smbus_read_byte+0x15e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <smbus_read_byte+0x165>
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   1080 <smbus_wait_until_ready@plt>
test   %rax,%rax
js     124e <smbus_read_byte+0xee>
mov    $0x1002,%edi
call   1070 <inb@plt>
and    $0xfe,%eax
mov    %eax,%edi
mov    $0x1002,%esi
call   1060 <outb@plt>
lea    (%rbx,%rbx,1),%eax
add    $0x1,%eax
movzbl %al,%edi
mov    $0x1004,%esi
call   1060 <outb@plt>
movzbl %bpl,%edi
mov    $0x1003,%esi
call   1060 <outb@plt>
mov    $0x1002,%edi
call   1070 <inb@plt>
and    $0xe3,%eax
lea    0x8(%rax),%edi
mov    $0x1002,%esi
call   1060 <outb@plt>
mov    $0x1000,%edi
call   1070 <inb@plt>
mov    %eax,%edi
mov    $0x1000,%esi
call   1060 <outb@plt>
xor    %edi,%edi
mov    $0x1005,%esi
call   1060 <outb@plt>
mov    $0x1002,%edi
call   1070 <inb@plt>
or     $0x40,%eax
mov    %eax,%edi
mov    $0x1002,%esi
call   1060 <outb@plt>
xor    %eax,%eax
call   1040 <smbus_wait_until_active@plt>
test   %rax,%rax
js     1261 <smbus_read_byte+0x101>
xor    %eax,%eax
call   1030 <smbus_wait_until_done@plt>
test   %rax,%rax
js     1274 <smbus_read_byte+0x114>
mov    $0x1000,%edi
call   1070 <inb@plt>
mov    %eax,%ebx
and    $0xbf,%ebx
mov    $0x1005,%edi
call   1070 <inb@plt>
cmp    $0x2,%ebx
movzbl %al,%ecx
mov    $0xffffffff,%eax
cmove  %ecx,%eax
jmp    1285 <smbus_read_byte+0x125>
lea    0xdab(%rip),%rdi
call   1050 <print_err@plt>
mov    $0xfffffffe,%eax
jmp    1285 <smbus_read_byte+0x125>
lea    0xdae(%rip),%rdi
call   1050 <print_err@plt>
mov    $0xfffffffc,%eax
jmp    1285 <smbus_read_byte+0x125>
lea    0xdb2(%rip),%rdi
call   1050 <print_err@plt>
mov    $0xfffffffd,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"SMBUS not ready (-2)\0A\00"
@.str.1 = [23 x i8] c"SMBUS not active (-4)\0A\00"
@.str.2 = [26 x i8] c"SMBUS not completed (-3)\0A\00"
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 %12
10: ; preds = %2
%11 = call i32 @print_err(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %3
br label %56
12: ; preds = %2
%13 = call i32 @inb(i32 4098)
%14 = and i32 %13, 254
%15 = call i32 @outb(i32 %14, i32 4098)
%16 = load i32, i32* %4
%17 = and i32 %16, 127
%18 = shl i32 %17, 1
%19 = or i32 %18, 1
%20 = call i32 @outb(i32 %19, i32 4100)
%21 = load i32, i32* %5
%22 = and i32 %21, 255
%23 = call i32 @outb(i32 %22, i32 4099)
%24 = call i32 @inb(i32 4098)
%25 = and i32 %24, 227
%26 = or i32 %25, 8
%27 = call i32 @outb(i32 %26, i32 4098)
%28 = call i32 @inb(i32 4096)
%29 = call i32 @outb(i32 %28, i32 4096)
%30 = call i32 @outb(i32 0, i32 4101)
%31 = call i32 @inb(i32 4098)
%32 = or i32 %31, 64
%33 = call i32 @outb(i32 %32, i32 4098)
%34 = call i64 (...) @smbus_wait_until_active()
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %12
%37 = call i32 @print_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -4, i32* %3
br label %56
38: ; preds = %12
%39 = call i64 (...) @smbus_wait_until_done()
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @print_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %3
br label %56
43: ; preds = %38
%44 = call i32 @inb(i32 4096)
%45 = and i32 %44, -65
%46 = trunc i32 %45 to i8
store i8 %46, i8* %6
%47 = call i32 @inb(i32 4101)
%48 = trunc i32 %47 to i8
store i8 %48, i8* %7
%49 = load i8, i8* %6
%50 = zext i8 %49 to i32
%51 = icmp ne i32 %50, 2
br i1 %51, label %52, label %53
52: ; preds = %43
store i32 -1, i32* %3
br label %56
53: ; preds = %43
%54 = load i8, i8* %7
%55 = zext i8 %54 to i32
store i32 %55, i32* %3
br label %56
56: ; preds = %53, %52, %41, %36, %10
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [22 x i8] c"SMBUS not ready (-2)\0A\00"
@.str.1 = [23 x i8] c"SMBUS not active (-4)\0A\00"
@.str.2 = [26 x i8] c"SMBUS not completed (-3)\0A\00"
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 %7
5: ; preds = %2
%6 = tail call i32 @print_err(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %43
7: ; preds = %2
%8 = tail call i32 @inb(i32 4098)
%9 = and i32 %8, 254
%10 = tail call i32 @outb(i32 %9, i32 4098)
%11 = shl i32 %0, 1
%12 = and i32 %11, 254
%13 = or i32 %12, 1
%14 = tail call i32 @outb(i32 %13, i32 4100)
%15 = and i32 %1, 255
%16 = tail call i32 @outb(i32 %15, i32 4099)
%17 = tail call i32 @inb(i32 4098)
%18 = and i32 %17, 227
%19 = or i32 %18, 8
%20 = tail call i32 @outb(i32 %19, i32 4098)
%21 = tail call i32 @inb(i32 4096)
%22 = tail call i32 @outb(i32 %21, i32 4096)
%23 = tail call i32 @outb(i32 0, i32 4101)
%24 = tail call i32 @inb(i32 4098)
%25 = or i32 %24, 64
%26 = tail call i32 @outb(i32 %25, i32 4098)
%27 = tail call i64 (...) @smbus_wait_until_active()
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %7
%30 = tail call i32 @print_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %43
31: ; preds = %7
%32 = tail call i64 (...) @smbus_wait_until_done()
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 @print_err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %43
36: ; preds = %31
%37 = tail call i32 @inb(i32 4096)
%38 = and i32 %37, 191
%39 = tail call i32 @inb(i32 4101)
%40 = icmp eq i32 %38, 2
%41 = and i32 %39, 255
%42 = select i1 %40, i32 %41, i32 -1
br label %43
43: ; preds = %36, %34, %29, %5
%44 = phi i32 [ -2, %5 ], [ -4, %29 ], [ -3, %34 ], [ %42, %36 ]
ret i32 %44
}