#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 /*<<< orphan*/ adc108s022_read_byte(int, unsigned char *);
int /*<<< orphan*/ adc108s022_write_byte(int, unsigned char);
unsigned int adc_read() {
  unsigned char chan;
  unsigned char valid = 0;
  unsigned char data;
  unsigned int retval;
  adc108s022_read_byte(0x2, &chan);
  adc108s022_write_byte(0x2, chan | 0x8);
  while (!valid) {
    adc108s022_read_byte(0x42, &valid);
  }
  adc108s022_read_byte(0x40, &data);
  retval = data;
  adc108s022_read_byte(0x41, &data);
  retval |= (data << 8);
  adc108s022_write_byte(0x2, chan);
  return retval;
}


x86 assembly (clang, -O0):
<adc_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movb   $0x0,-0x2(%rbp)
mov    $0x2,%edi
lea    -0x1(%rbp),%rsi
call   1040 <adc108s022_read_byte@plt>
movzbl -0x1(%rbp),%eax
or     $0x8,%eax
mov    $0x2,%edi
movzbl %al,%esi
call   1030 <adc108s022_write_byte@plt>
cmpb   $0x0,-0x2(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1164 <adc_read+0x44>
jmp    1177 <adc_read+0x57>
mov    $0x42,%edi
lea    -0x2(%rbp),%rsi
call   1040 <adc108s022_read_byte@plt>
jmp    114e <adc_read+0x2e>
mov    $0x40,%edi
lea    -0x3(%rbp),%rsi
call   1040 <adc108s022_read_byte@plt>
movzbl -0x3(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    $0x41,%edi
lea    -0x3(%rbp),%rsi
call   1040 <adc108s022_read_byte@plt>
movzbl -0x3(%rbp),%eax
shl    $0x8,%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    $0x2,%edi
movzbl -0x1(%rbp),%esi
call   1030 <adc108s022_write_byte@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adc_read>:
push   %rbp
push   %rbx
push   %rax
movb   $0x0,0x6(%rsp)
lea    0x7(%rsp),%rsi
mov    $0x2,%edi
call   1040 <adc108s022_read_byte@plt>
movzbl 0x7(%rsp),%esi
or     $0x8,%esi
mov    $0x2,%edi
call   1030 <adc108s022_write_byte@plt>
lea    0x6(%rsp),%rbx
xchg   %ax,%ax
mov    $0x42,%edi
mov    %rbx,%rsi
call   1040 <adc108s022_read_byte@plt>
cmpb   $0x0,0x6(%rsp)
je     1150 <adc_read+0x30>
lea    0x5(%rsp),%rbx
mov    $0x40,%edi
mov    %rbx,%rsi
call   1040 <adc108s022_read_byte@plt>
movzbl 0x5(%rsp),%ebp
mov    $0x41,%edi
mov    %rbx,%rsi
call   1040 <adc108s022_read_byte@plt>
movzbl 0x5(%rsp),%ebx
shl    $0x8,%ebx
or     %ebp,%ebx
movzbl 0x7(%rsp),%esi
mov    $0x2,%edi
call   1030 <adc108s022_write_byte@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @adc_read() {
%1 = alloca i8
%2 = alloca i8
%3 = alloca i8
%4 = alloca i32
store i8 0, i8* %2
%5 = call i32 @adc108s022_read_byte(i32 2, i8* %1)
%6 = load i8, i8* %1
%7 = zext i8 %6 to i32
%8 = or i32 %7, 8
%9 = trunc i32 %8 to i8
%10 = call i32 @adc108s022_write_byte(i32 2, i8 zeroext %9)
br label %11
11: ; preds = %15, %0
%12 = load i8, i8* %2
%13 = icmp ne i8 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @adc108s022_read_byte(i32 66, i8* %2)
br label %11
17: ; preds = %11
%18 = call i32 @adc108s022_read_byte(i32 64, i8* %3)
%19 = load i8, i8* %3
%20 = zext i8 %19 to i32
store i32 %20, i32* %4
%21 = call i32 @adc108s022_read_byte(i32 65, i8* %3)
%22 = load i8, i8* %3
%23 = zext i8 %22 to i32
%24 = shl i32 %23, 8
%25 = load i32, i32* %4
%26 = or i32 %25, %24
store i32 %26, i32* %4
%27 = load i8, i8* %1
%28 = call i32 @adc108s022_write_byte(i32 2, i8 zeroext %27)
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
define i32 @adc_read() {
%1 = alloca i8
%2 = alloca i8
%3 = alloca i8
store i8 0, i8* %2
%4 = call i32 @adc108s022_read_byte(i32 2, i8* %1)
%5 = load i8, i8* %1
%6 = or i8 %5, 8
%7 = call i32 @adc108s022_write_byte(i32 2, i8 zeroext %6)
br label %8
8: ; preds = %0, %8
%9 = call i32 @adc108s022_read_byte(i32 66, i8* %2)
%10 = load i8, i8* %2
%11 = icmp eq i8 %10, 0
br i1 %11, label %8, label %12
12: ; preds = %8
%13 = call i32 @adc108s022_read_byte(i32 64, i8* %3)
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = call i32 @adc108s022_read_byte(i32 65, i8* %3)
%17 = load i8, i8* %3
%18 = zext i8 %17 to i32
%19 = shl nuw nsw i32 %18, 8
%20 = or i32 %19, %15
%21 = load i8, i8* %1
%22 = call i32 @adc108s022_write_byte(i32 2, i8 zeroext %21)
ret i32 %20
}