#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 << 1))) {
    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 << 1) | 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    12bf <do_smbus_read_byte+0x10f>
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    12bf <do_smbus_read_byte+0x10f>
mov    -0x8(%rbp),%edi
movzbl -0x9(%rbp),%eax
shl    $0x1,%eax
movzbl %al,%esi
call   10c0 <smbus_send_slave_address@plt>
cmp    $0x0,%rax
je     1225 <do_smbus_read_byte+0x75>
movb   $0x3,-0xb(%rbp)
jmp    12bf <do_smbus_read_byte+0x10f>
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     1251 <do_smbus_read_byte+0xa1>
movb   $0x4,-0xb(%rbp)
jmp    12bf <do_smbus_read_byte+0x10f>
mov    -0x8(%rbp),%edi
call   1080 <smbus_start_condition@plt>
cmp    $0x0,%rax
je     126c <do_smbus_read_byte+0xbc>
movb   $0x5,-0xb(%rbp)
jmp    12bf <do_smbus_read_byte+0x10f>
mov    -0x8(%rbp),%edi
movzbl -0x9(%rbp),%eax
shl    $0x1,%eax
or     $0x1,%eax
movzbl %al,%esi
call   10c0 <smbus_send_slave_address@plt>
cmp    $0x0,%rax
je     1294 <do_smbus_read_byte+0xe4>
movb   $0x6,-0xb(%rbp)
jmp    12bf <do_smbus_read_byte+0x10f>
mov    -0x8(%rbp),%edi
call   1060 <smbus_stop_condition@plt>
cmp    $0x0,%rax
je     12af <do_smbus_read_byte+0xff>
movb   $0x7,-0xb(%rbp)
jmp    12bf <do_smbus_read_byte+0x10f>
mov    -0x8(%rbp),%edi
call   1040 <smbus_get_result@plt>
mov    %al,-0x1(%rbp)
jmp    1326 <do_smbus_read_byte+0x176>
lea    0xd3a(%rip),%rdi
call   10d0 <print_debug@plt>
movzbl -0xb(%rbp),%edi
call   1030 <print_debug_hex8@plt>
lea    0xd37(%rip),%rdi
call   10d0 <print_debug@plt>
movzbl -0x9(%rbp),%edi
call   1030 <print_debug_hex8@plt>
lea    0xd2b(%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,%ebp
mov    %esi,%r14d
mov    %edi,%ebx
call   1090 <smbus_check_stop_condition@plt>
mov    $0x1,%r12b
test   %rax,%rax
jne    1246 <do_smbus_read_byte+0x96>
mov    %ebx,%edi
call   1080 <smbus_start_condition@plt>
mov    $0x2,%r12b
test   %rax,%rax
jne    1246 <do_smbus_read_byte+0x96>
lea    (%r14,%r14,1),%r15d
movzbl %r15b,%esi
mov    %ebx,%edi
call   10c0 <smbus_send_slave_address@plt>
mov    $0x3,%r12b
test   %rax,%rax
jne    1246 <do_smbus_read_byte+0x96>
mov    %ebx,%edi
mov    $0x1,%esi
call   10a0 <smbus_ack@plt>
movzbl %bpl,%esi
mov    %ebx,%edi
call   1050 <smbus_send_command@plt>
mov    $0x4,%r12b
test   %rax,%rax
jne    1246 <do_smbus_read_byte+0x96>
mov    %ebx,%edi
call   1080 <smbus_start_condition@plt>
mov    $0x5,%r12b
test   %rax,%rax
jne    1246 <do_smbus_read_byte+0x96>
or     $0x1,%r15b
movzbl %r15b,%esi
mov    %ebx,%edi
call   10c0 <smbus_send_slave_address@plt>
mov    $0x6,%r12b
test   %rax,%rax
jne    1246 <do_smbus_read_byte+0x96>
mov    %ebx,%edi
call   1060 <smbus_stop_condition@plt>
mov    $0x7,%r12b
test   %rax,%rax
je     12aa <do_smbus_read_byte+0xfa>
lea    0xdb3(%rip),%rdi
call   10d0 <print_debug@plt>
movzbl %r12b,%edi
call   1030 <print_debug_hex8@plt>
lea    0xdb0(%rip),%rdi
call   10d0 <print_debug@plt>
movzbl %r14b,%edi
call   1030 <print_debug_hex8@plt>
lea    0xda4(%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 %58
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 %58
18: ; preds = %13
%19 = load i32, i32* %5
%20 = load i8, i8* %6
%21 = zext i8 %20 to i32
%22 = shl i32 %21, 1
%23 = trunc i32 %22 to i8
%24 = call i64 @smbus_send_slave_address(i32 %19, i8 zeroext %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %18
store i8 3, i8* %8
br label %58
27: ; preds = %18
%28 = load i32, i32* %5
%29 = call i32 @smbus_ack(i32 %28, i32 1)
%30 = load i32, i32* %5
%31 = load i8, i8* %7
%32 = call i64 @smbus_send_command(i32 %30, i8 zeroext %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
store i8 4, i8* %8
br label %58
35: ; preds = %27
%36 = load i32, i32* %5
%37 = call i64 @smbus_start_condition(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i8 5, i8* %8
br label %58
40: ; preds = %35
%41 = load i32, i32* %5
%42 = load i8, i8* %6
%43 = zext i8 %42 to i32
%44 = shl i32 %43, 1
%45 = or i32 %44, 1
%46 = trunc i32 %45 to i8
%47 = call i64 @smbus_send_slave_address(i32 %41, i8 zeroext %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %40
store i8 6, i8* %8
br label %58
50: ; preds = %40
%51 = load i32, i32* %5
%52 = call i64 @smbus_stop_condition(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
store i8 7, i8* %8
br label %58
55: ; preds = %50
%56 = load i32, i32* %5
%57 = call zeroext i8 @smbus_get_result(i32 %56)
store i8 %57, i8* %4
br label %77
58: ; preds = %54, %49, %39, %34, %26, %17, %12
%59 = call i32 @print_debug(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%60 = load i8, i8* %8
%61 = call i32 @print_debug_hex8(i8 zeroext %60)
%62 = call i32 @print_debug(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%63 = load i8, i8* %6
%64 = call i32 @print_debug_hex8(i8 zeroext %63)
%65 = call i32 @print_debug(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%66 = load i32, i32* %5
%67 = call i64 @smbus_stop_condition(i32 %66)
%68 = load i32, i32* %5
%69 = add i32 %68, 1
%70 = call i32 @inb(i32 %69)
%71 = load i32, i32* %5
%72 = add i32 %71, 1
%73 = call i32 @outb(i32 %70, i32 %72)
%74 = load i32, i32* %5
%75 = add i32 %74, 1
%76 = call i32 @outb(i32 0, i32 %75)
store i8 -1, i8* %4
br label %77
77: ; preds = %58, %55
%78 = load i8, i8* %4
ret i8 %78
}

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 %29
6: ; preds = %3
%7 = tail call i64 @smbus_start_condition(i32 %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %6
%10 = shl i8 %1, 1
%11 = tail call i64 @smbus_send_slave_address(i32 %0, i8 zeroext %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %9
%14 = tail call i32 @smbus_ack(i32 %0, i32 1)
%15 = tail call i64 @smbus_send_command(i32 %0, i8 zeroext %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = tail call i64 @smbus_start_condition(i32 %0)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %17
%21 = or i8 %10, 1
%22 = tail call i64 @smbus_send_slave_address(i32 %0, i8 zeroext %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = tail call i64 @smbus_stop_condition(i32 %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call zeroext i8 @smbus_get_result(i32 %0)
br label %41
29: ; preds = %24, %20, %17, %13, %9, %6, %3
%30 = phi i8 [ 1, %3 ], [ 2, %6 ], [ 3, %9 ], [ 4, %13 ], [ 5, %17 ], [ 6, %20 ], [ 7, %24 ]
%31 = tail call i32 @print_debug(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%32 = tail call i32 @print_debug_hex8(i8 zeroext %30)
%33 = tail call i32 @print_debug(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @print_debug_hex8(i8 zeroext %1)
%35 = tail call i32 @print_debug(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%36 = tail call i64 @smbus_stop_condition(i32 %0)
%37 = add i32 %0, 1
%38 = tail call i32 @inb(i32 %37)
%39 = tail call i32 @outb(i32 %38, i32 %37)
%40 = tail call i32 @outb(i32 0, i32 %37)
br label %41
41: ; preds = %29, %27
%42 = phi i8 [ -1, %29 ], [ %28, %27 ]
ret i8 %42
}