#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(unsigned int);
int /*<<< orphan*/ outb(int, unsigned int);
int /*<<< orphan*/ print_debug(char *);
int /*<<< orphan*/ print_debug_hex8(unsigned char);
int /*<<< orphan*/ smbus_ack(unsigned int, int);
scalar_t__ smbus_check_stop_condition(unsigned int);
unsigned char smbus_get_result(unsigned int);
scalar_t__ smbus_send_command(unsigned int, unsigned char);
scalar_t__ smbus_send_slave_address(unsigned int, unsigned char);
scalar_t__ smbus_start_condition(unsigned int);
scalar_t__ smbus_stop_condition(unsigned int);
unsigned char do_smbus_read_byte(unsigned smbus_io_base, unsigned char device,
                                 unsigned char address) {
  unsigned char error = 0;
  if ((smbus_check_stop_condition(smbus_io_base))) {
    error = 1;
    goto err;
  }
  if ((smbus_start_condition(smbus_io_base))) {
    error = 2;
    goto err;
  }
  if ((smbus_send_slave_address(smbus_io_base, device))) {
    error = 3;
    goto err;
  }
  smbus_ack(smbus_io_base, 1);
  if ((smbus_send_command(smbus_io_base, address))) {
    error = 4;
    goto err;
  }
  if ((smbus_start_condition(smbus_io_base))) {
    error = 5;
    goto err;
  }
  if ((smbus_send_slave_address(smbus_io_base, device | 0x01))) {
    error = 6;
    goto err;
  }
  if ((smbus_stop_condition(smbus_io_base))) {
    error = 7;
    goto err;
  }
  return smbus_get_result(smbus_io_base);
err:
  print_debug("SMBUS READ ERROR:");
  print_debug_hex8(error);
  print_debug(" device:");
  print_debug_hex8(device);
  print_debug("\n");
  smbus_stop_condition(smbus_io_base);
  outb(inb(smbus_io_base + 0x01), smbus_io_base + 0x01);
  outb(0x0, smbus_io_base + 0x01);
  return 0xFF;
}


x86 assembly (clang, -O0):
<do_smbus_read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dl,%al
mov    %sil,%cl
mov    %edi,-0x8(%rbp)
mov    %cl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
movb   $0x0,-0xb(%rbp)
mov    -0x8(%rbp),%edi
call   1090 <smbus_check_stop_condition@plt>
cmp    $0x0,%rax
je     11e5 <do_smbus_read_byte+0x35>
movb   $0x1,-0xb(%rbp)
jmp    12b6 <do_smbus_read_byte+0x106>
mov    -0x8(%rbp),%edi
call   1080 <smbus_start_condition@plt>
cmp    $0x0,%rax
je     1200 <do_smbus_read_byte+0x50>
movb   $0x2,-0xb(%rbp)
jmp    12b6 <do_smbus_read_byte+0x106>
mov    -0x8(%rbp),%edi
movzbl -0x9(%rbp),%esi
call   10c0 <smbus_send_slave_address@plt>
cmp    $0x0,%rax
je     121f <do_smbus_read_byte+0x6f>
movb   $0x3,-0xb(%rbp)
jmp    12b6 <do_smbus_read_byte+0x106>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   10a0 <smbus_ack@plt>
mov    -0x8(%rbp),%edi
movzbl -0xa(%rbp),%esi
call   1050 <smbus_send_command@plt>
cmp    $0x0,%rax
je     124b <do_smbus_read_byte+0x9b>
movb   $0x4,-0xb(%rbp)
jmp    12b6 <do_smbus_read_byte+0x106>
mov    -0x8(%rbp),%edi
call   1080 <smbus_start_condition@plt>
cmp    $0x0,%rax
je     1266 <do_smbus_read_byte+0xb6>
movb   $0x5,-0xb(%rbp)
jmp    12b6 <do_smbus_read_byte+0x106>
mov    -0x8(%rbp),%edi
movzbl -0x9(%rbp),%eax
or     $0x1,%eax
movzbl %al,%esi
call   10c0 <smbus_send_slave_address@plt>
cmp    $0x0,%rax
je     128b <do_smbus_read_byte+0xdb>
movb   $0x6,-0xb(%rbp)
jmp    12b6 <do_smbus_read_byte+0x106>
mov    -0x8(%rbp),%edi
call   1060 <smbus_stop_condition@plt>
cmp    $0x0,%rax
je     12a6 <do_smbus_read_byte+0xf6>
movb   $0x7,-0xb(%rbp)
jmp    12b6 <do_smbus_read_byte+0x106>
mov    -0x8(%rbp),%edi
call   1040 <smbus_get_result@plt>
mov    %al,-0x1(%rbp)
jmp    131d <do_smbus_read_byte+0x16d>
lea    0xd43(%rip),%rdi
call   10d0 <print_debug@plt>
movzbl -0xb(%rbp),%edi
call   1030 <print_debug_hex8@plt>
lea    0xd40(%rip),%rdi
call   10d0 <print_debug@plt>
movzbl -0x9(%rbp),%edi
call   1030 <print_debug_hex8@plt>
lea    0xd34(%rip),%rdi
call   10d0 <print_debug@plt>
mov    -0x8(%rbp),%edi
call   1060 <smbus_stop_condition@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   10b0 <inb@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%esi
add    $0x1,%esi
call   1070 <outb@plt>
mov    -0x8(%rbp),%esi
add    $0x1,%esi
xor    %edi,%edi
call   1070 <outb@plt>
movb   $0xff,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_smbus_read_byte>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %esi,%ebp
mov    %edi,%ebx
call   1090 <smbus_check_stop_condition@plt>
mov    $0x1,%r12b
movzbl %bpl,%r14d
test   %rax,%rax
jne    1245 <do_smbus_read_byte+0x95>
mov    %ebx,%edi
call   1080 <smbus_start_condition@plt>
mov    $0x2,%r12b
test   %rax,%rax
jne    1245 <do_smbus_read_byte+0x95>
mov    %ebx,%edi
mov    %r14d,%esi
call   10c0 <smbus_send_slave_address@plt>
mov    $0x3,%r12b
test   %rax,%rax
jne    1245 <do_smbus_read_byte+0x95>
mov    %ebx,%edi
mov    $0x1,%esi
call   10a0 <smbus_ack@plt>
movzbl %r15b,%esi
mov    %ebx,%edi
call   1050 <smbus_send_command@plt>
mov    $0x4,%r12b
test   %rax,%rax
jne    1245 <do_smbus_read_byte+0x95>
mov    %ebx,%edi
call   1080 <smbus_start_condition@plt>
mov    $0x5,%r12b
test   %rax,%rax
jne    1245 <do_smbus_read_byte+0x95>
or     $0x1,%bpl
movzbl %bpl,%esi
mov    %ebx,%edi
call   10c0 <smbus_send_slave_address@plt>
mov    $0x6,%r12b
test   %rax,%rax
jne    1245 <do_smbus_read_byte+0x95>
mov    %ebx,%edi
call   1060 <smbus_stop_condition@plt>
mov    $0x7,%r12b
test   %rax,%rax
je     12a8 <do_smbus_read_byte+0xf8>
lea    0xdb4(%rip),%rdi
call   10d0 <print_debug@plt>
movzbl %r12b,%edi
call   1030 <print_debug_hex8@plt>
lea    0xdb1(%rip),%rdi
call   10d0 <print_debug@plt>
mov    %r14d,%edi
call   1030 <print_debug_hex8@plt>
lea    0xda6(%rip),%rdi
call   10d0 <print_debug@plt>
mov    %ebx,%edi
call   1060 <smbus_stop_condition@plt>
add    $0x1,%ebx
mov    %ebx,%edi
call   10b0 <inb@plt>
mov    %eax,%edi
mov    %ebx,%esi
call   1070 <outb@plt>
xor    %edi,%edi
mov    %ebx,%esi
call   1070 <outb@plt>
mov    $0xff,%al
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <smbus_get_result@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"SMBUS READ ERROR:\00"
@.str.1 = [9 x i8] c" device:\00"
@.str.2 = [2 x i8] c"\0A\00"
define zeroext i8 @do_smbus_read_byte(i32 %0, i8 zeroext %1, i8 zeroext %2) {
%4 = alloca i8
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
store i32 %0, i32* %5
store i8 %1, i8* %6
store i8 %2, i8* %7
store i8 0, i8* %8
%9 = load i32, i32* %5
%10 = call i64 @smbus_check_stop_condition(i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i8 1, i8* %8
br label %54
13: ; preds = %3
%14 = load i32, i32* %5
%15 = call i64 @smbus_start_condition(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i8 2, i8* %8
br label %54
18: ; preds = %13
%19 = load i32, i32* %5
%20 = load i8, i8* %6
%21 = call i64 @smbus_send_slave_address(i32 %19, i8 zeroext %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i8 3, i8* %8
br label %54
24: ; preds = %18
%25 = load i32, i32* %5
%26 = call i32 @smbus_ack(i32 %25, i32 1)
%27 = load i32, i32* %5
%28 = load i8, i8* %7
%29 = call i64 @smbus_send_command(i32 %27, i8 zeroext %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
store i8 4, i8* %8
br label %54
32: ; preds = %24
%33 = load i32, i32* %5
%34 = call i64 @smbus_start_condition(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
store i8 5, i8* %8
br label %54
37: ; preds = %32
%38 = load i32, i32* %5
%39 = load i8, i8* %6
%40 = zext i8 %39 to i32
%41 = or i32 %40, 1
%42 = trunc i32 %41 to i8
%43 = call i64 @smbus_send_slave_address(i32 %38, i8 zeroext %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %37
store i8 6, i8* %8
br label %54
46: ; preds = %37
%47 = load i32, i32* %5
%48 = call i64 @smbus_stop_condition(i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46
store i8 7, i8* %8
br label %54
51: ; preds = %46
%52 = load i32, i32* %5
%53 = call zeroext i8 @smbus_get_result(i32 %52)
store i8 %53, i8* %4
br label %73
54: ; preds = %50, %45, %36, %31, %23, %17, %12
%55 = call i32 @print_debug(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%56 = load i8, i8* %8
%57 = call i32 @print_debug_hex8(i8 zeroext %56)
%58 = call i32 @print_debug(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%59 = load i8, i8* %6
%60 = call i32 @print_debug_hex8(i8 zeroext %59)
%61 = call i32 @print_debug(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%62 = load i32, i32* %5
%63 = call i64 @smbus_stop_condition(i32 %62)
%64 = load i32, i32* %5
%65 = add i32 %64, 1
%66 = call i32 @inb(i32 %65)
%67 = load i32, i32* %5
%68 = add i32 %67, 1
%69 = call i32 @outb(i32 %66, i32 %68)
%70 = load i32, i32* %5
%71 = add i32 %70, 1
%72 = call i32 @outb(i32 0, i32 %71)
store i8 -1, i8* %4
br label %73
73: ; preds = %54, %51
%74 = load i8, i8* %4
ret i8 %74
}

LLVM IR (-O2):
@.str = [18 x i8] c"SMBUS READ ERROR:\00"
@.str.1 = [9 x i8] c" device:\00"
@.str.2 = [2 x i8] c"\0A\00"
define zeroext i8 @do_smbus_read_byte(i32 %0, i8 zeroext %1, i8 zeroext %2) {
%4 = tail call i64 @smbus_check_stop_condition(i32 %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %28
6: ; preds = %3
%7 = tail call i64 @smbus_start_condition(i32 %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %6
%10 = tail call i64 @smbus_send_slave_address(i32 %0, i8 zeroext %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %9
%13 = tail call i32 @smbus_ack(i32 %0, i32 1)
%14 = tail call i64 @smbus_send_command(i32 %0, i8 zeroext %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %12
%17 = tail call i64 @smbus_start_condition(i32 %0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = or i8 %1, 1
%21 = tail call i64 @smbus_send_slave_address(i32 %0, i8 zeroext %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = tail call i64 @smbus_stop_condition(i32 %0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call zeroext i8 @smbus_get_result(i32 %0)
br label %40
28: ; preds = %23, %19, %16, %12, %9, %6, %3
%29 = phi i8 [ 1, %3 ], [ 2, %6 ], [ 3, %9 ], [ 4, %12 ], [ 5, %16 ], [ 6, %19 ], [ 7, %23 ]
%30 = tail call i32 @print_debug(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%31 = tail call i32 @print_debug_hex8(i8 zeroext %29)
%32 = tail call i32 @print_debug(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%33 = tail call i32 @print_debug_hex8(i8 zeroext %1)
%34 = tail call i32 @print_debug(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%35 = tail call i64 @smbus_stop_condition(i32 %0)
%36 = add i32 %0, 1
%37 = tail call i32 @inb(i32 %36)
%38 = tail call i32 @outb(i32 %37, i32 %36)
%39 = tail call i32 @outb(i32 0, i32 %36)
br label %40
40: ; preds = %28, %26
%41 = phi i8 [ -1, %28 ], [ %27, %26 ]
ret i8 %41
}