#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);
int /*<<< orphan*/ adc_read();
int /*<<< orphan*/ printf(char *);
void adc_chan(unsigned int chan) {
  unsigned char data;
  unsigned char chan_change = 0;
  if (chan > 0x7) {
    printf("Warning: channel out of range; aborting.\n");
    return;
  }
  adc108s022_read_byte(0x2, &data);
  if ((data & 7) != (chan & 7)) {
    chan_change = 1;
  }
  adc108s022_write_byte(0x2, (unsigned char)(chan & 0x7));
  if (chan_change) {
    adc_read();
  }
}


x86 assembly (clang, -O0):
<adc_chan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movb   $0x0,-0x6(%rbp)
cmpl   $0x7,-0x4(%rbp)
jbe    116a <adc_chan+0x2a>
lea    0xea0(%rip),%rdi
call   1040 <printf@plt>
jmp    11b5 <adc_chan+0x75>
mov    $0x2,%edi
lea    -0x5(%rbp),%rsi
call   1050 <adc108s022_read_byte@plt>
movzbl -0x5(%rbp),%eax
and    $0x7,%eax
mov    -0x4(%rbp),%ecx
and    $0x7,%ecx
cmp    %ecx,%eax
je     1191 <adc_chan+0x51>
movb   $0x1,-0x6(%rbp)
mov    -0x4(%rbp),%eax
and    $0x7,%eax
mov    $0x2,%edi
movzbl %al,%esi
call   1030 <adc108s022_write_byte@plt>
cmpb   $0x0,-0x6(%rbp)
je     11b5 <adc_chan+0x75>
mov    $0x0,%al
call   1060 <adc_read@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adc_chan>:
cmp    $0x8,%edi
jb     1151 <adc_chan+0x11>
lea    0xeb4(%rip),%rdi
jmp    1030 <puts@plt>
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x7(%rsp),%rsi
mov    $0x2,%edi
call   1050 <adc108s022_read_byte@plt>
movzbl 0x7(%rsp),%ebp
and    $0x7,%ebp
movzbl %bl,%esi
mov    $0x2,%edi
call   1040 <adc108s022_write_byte@plt>
cmp    %ebx,%ebp
je     1185 <adc_chan+0x45>
xor    %eax,%eax
call   1060 <adc_read@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Warning: channel out of range; aborting.\0A\00"
define void @adc_chan(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i8
store i32 %0, i32* %2
store i8 0, i8* %4
%5 = load i32, i32* %2
%6 = icmp ugt i32 %5, 7
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %27
9: ; preds = %1
%10 = call i32 @adc108s022_read_byte(i32 2, i8* %3)
%11 = load i8, i8* %3
%12 = zext i8 %11 to i32
%13 = and i32 %12, 7
%14 = load i32, i32* %2
%15 = and i32 %14, 7
%16 = icmp ne i32 %13, %15
br i1 %16, label %17, label %18
17: ; preds = %9
store i8 1, i8* %4
br label %18
18: ; preds = %17, %9
%19 = load i32, i32* %2
%20 = and i32 %19, 7
%21 = trunc i32 %20 to i8
%22 = call i32 @adc108s022_write_byte(i32 2, i8 zeroext %21)
%23 = load i8, i8* %4
%24 = icmp ne i8 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 (...) @adc_read()
br label %27
27: ; preds = %7, %25, %18
ret void
}

LLVM IR (-O2):
@str = [41 x i8] c"Warning: channel out of range; aborting.\00"
define void @adc_chan(i32 %0) {
%2 = alloca i8
%3 = icmp ugt i32 %0, 7
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %16
6: ; preds = %1
%7 = call i32 @adc108s022_read_byte(i32 2, i8* %2)
%8 = load i8, i8* %2
%9 = and i8 %8, 7
%10 = zext i8 %9 to i32
%11 = icmp eq i32 %10, %0
%12 = trunc i32 %0 to i8
%13 = call i32 @adc108s022_write_byte(i32 2, i8 zeroext %12)
br i1 %11, label %16, label %14
14: ; preds = %6
%15 = call i32 (...) @adc_read()
br label %16
16: ; preds = %6, %14, %4
ret void
}