#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 */
typedef int uint8_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int ltc185x_create_cmd(int, int, int, int);
int /*<<< orphan*/ ltc185x_en();
int /*<<< orphan*/ ltc185x_read_ch(int /*<<< orphan*/ *, int, int, int, int);
int /*<<< orphan*/ ltc185x_start_conv();
int /*<<< orphan*/ ltc185x_xfer(int, int /*<<< orphan*/ *);
int ltc185x_scan_ch(uint16_t *data, uint8_t *ch, uint8_t ch_count,
                    uint8_t range, uint8_t mode, uint8_t polarity) {
  int ch_index = 0;
  uint8_t cmd;
  uint16_t tmp;
  if (ch_count > (8) || ch_count == 0)
    return -1;
  if (ch_count == 1) {
    ltc185x_read_ch(data, *ch, range, mode, polarity);
    return 1;
  }
  ltc185x_en();
  cmd = ltc185x_create_cmd(ch[ch_index], range, mode, polarity);
  ltc185x_xfer(cmd, &tmp);
  ltc185x_start_conv();
  ltc185x_xfer(cmd, &tmp);
  ltc185x_start_conv();
  for (; ch_index < ch_count; ch_index++) {
    if (ch_index + 1 < ch_count)
      cmd = ltc185x_create_cmd(ch[ch_index + 1], range, mode, polarity);
    if (ch_index < ch_count) {
      ltc185x_xfer(cmd, &data[ch_index]);
      ltc185x_start_conv();
    }
  }
  return ch_index;
}


x86 assembly (clang, -O0):
<ltc185x_scan_ch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jg     1189 <ltc185x_scan_ch+0x39>
cmpl   $0x0,-0x1c(%rbp)
jne    1195 <ltc185x_scan_ch+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <ltc185x_scan_ch+0x13c>
cmpl   $0x1,-0x1c(%rbp)
jne    11c4 <ltc185x_scan_ch+0x74>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1050 <ltc185x_read_ch@plt>
movl   $0x1,-0x4(%rbp)
jmp    128c <ltc185x_scan_ch+0x13c>
mov    $0x0,%al
call   1030 <ltc185x_en@plt>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
call   1060 <ltc185x_create_cmd@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1040 <ltc185x_xfer@plt>
mov    $0x0,%al
call   1070 <ltc185x_start_conv@plt>
mov    -0x30(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1040 <ltc185x_xfer@plt>
mov    $0x0,%al
call   1070 <ltc185x_start_conv@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1286 <ltc185x_scan_ch+0x136>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jge    1249 <ltc185x_scan_ch+0xf9>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
call   1060 <ltc185x_create_cmd@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1273 <ltc185x_scan_ch+0x123>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <ltc185x_xfer@plt>
mov    $0x0,%al
call   1070 <ltc185x_start_conv@plt>
jmp    1278 <ltc185x_scan_ch+0x128>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    120d <ltc185x_scan_ch+0xbd>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltc185x_scan_ch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,0xc(%rsp)
mov    %r8d,0x8(%rsp)
mov    %ecx,0x4(%rsp)
mov    $0xffffffff,%r15d
cmp    $0x8,%edx
jg     126c <ltc185x_scan_ch+0x11c>
mov    %edx,%ebx
test   %edx,%edx
je     126c <ltc185x_scan_ch+0x11c>
mov    %rsi,%r12
mov    %rdi,%rbp
cmp    $0x1,%ebx
jne    11b4 <ltc185x_scan_ch+0x64>
mov    (%r12),%esi
mov    %rbp,%rdi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
call   1050 <ltc185x_read_ch@plt>
mov    $0x1,%r15d
jmp    126c <ltc185x_scan_ch+0x11c>
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <ltc185x_en@plt>
mov    (%r12),%edi
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
call   1060 <ltc185x_create_cmd@plt>
mov    %eax,%r14d
lea    0x14(%rsp),%r13
mov    %eax,%edi
mov    %r13,%rsi
call   1040 <ltc185x_xfer@plt>
xor    %eax,%eax
call   1070 <ltc185x_start_conv@plt>
mov    %r14d,%edi
mov    %r13,%rsi
call   1040 <ltc185x_xfer@plt>
xor    %eax,%eax
call   1070 <ltc185x_start_conv@plt>
test   %ebx,%ebx
jle    126c <ltc185x_scan_ch+0x11c>
mov    %ebx,0x10(%rsp)
mov    %ebx,%r15d
mov    %r15,%rbx
neg    %rbx
mov    $0x1,%r13d
jmp    1248 <ltc185x_scan_ch+0xf8>
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbp,%rsi
call   1040 <ltc185x_xfer@plt>
xor    %eax,%eax
call   1070 <ltc185x_start_conv@plt>
add    $0x4,%rbp
lea    (%rbx,%r13,1),%rax
add    $0x1,%rax
add    $0x1,%r13
cmp    $0x1,%rax
je     1267 <ltc185x_scan_ch+0x117>
cmp    %r15,%r13
jae    1220 <ltc185x_scan_ch+0xd0>
mov    (%r12,%r13,4),%edi
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
call   1060 <ltc185x_create_cmd@plt>
mov    %eax,%r14d
jmp    1220 <ltc185x_scan_ch+0xd0>
mov    0x10(%rsp),%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ltc185x_scan_ch(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32* %0, i32** %8
store i32* %1, i32** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 0, i32* %14
%17 = load i32, i32* %10
%18 = icmp sgt i32 %17, 8
br i1 %18, label %22, label %19
19: ; preds = %6
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19, %6
store i32 -1, i32* %7
br label %89
23: ; preds = %19
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i32*, i32** %8
%28 = load i32*, i32** %9
%29 = load i32, i32* %28
%30 = load i32, i32* %11
%31 = load i32, i32* %12
%32 = load i32, i32* %13
%33 = call i32 @ltc185x_read_ch(i32* %27, i32 %29, i32 %30, i32 %31, i32 %32)
store i32 1, i32* %7
br label %89
34: ; preds = %23
%35 = call i32 (...) @ltc185x_en()
%36 = load i32*, i32** %9
%37 = load i32, i32* %14
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = load i32, i32* %11
%42 = load i32, i32* %12
%43 = load i32, i32* %13
%44 = call i32 @ltc185x_create_cmd(i32 %40, i32 %41, i32 %42, i32 %43)
store i32 %44, i32* %15
%45 = load i32, i32* %15
%46 = call i32 @ltc185x_xfer(i32 %45, i32* %16)
%47 = call i32 (...) @ltc185x_start_conv()
%48 = load i32, i32* %15
%49 = call i32 @ltc185x_xfer(i32 %48, i32* %16)
%50 = call i32 (...) @ltc185x_start_conv()
br label %51
51: ; preds = %84, %34
%52 = load i32, i32* %14
%53 = load i32, i32* %10
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %87
55: ; preds = %51
%56 = load i32, i32* %14
%57 = add nsw i32 %56, 1
%58 = load i32, i32* %10
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %71
60: ; preds = %55
%61 = load i32*, i32** %9
%62 = load i32, i32* %14
%63 = add nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %61, i64 %64
%66 = load i32, i32* %65
%67 = load i32, i32* %11
%68 = load i32, i32* %12
%69 = load i32, i32* %13
%70 = call i32 @ltc185x_create_cmd(i32 %66, i32 %67, i32 %68, i32 %69)
store i32 %70, i32* %15
br label %71
71: ; preds = %60, %55
%72 = load i32, i32* %14
%73 = load i32, i32* %10
%74 = icmp slt i32 %72, %73
br i1 %74, label %75, label %83
75: ; preds = %71
%76 = load i32, i32* %15
%77 = load i32*, i32** %8
%78 = load i32, i32* %14
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %77, i64 %79
%81 = call i32 @ltc185x_xfer(i32 %76, i32* %80)
%82 = call i32 (...) @ltc185x_start_conv()
br label %83
83: ; preds = %75, %71
br label %84
84: ; preds = %83
%85 = load i32, i32* %14
%86 = add nsw i32 %85, 1
store i32 %86, i32* %14
br label %51
87: ; preds = %51
%88 = load i32, i32* %14
store i32 %88, i32* %7
br label %89
89: ; preds = %87, %26, %22
%90 = load i32, i32* %7
ret i32 %90
}

LLVM IR (-O2):
define i32 @ltc185x_scan_ch(i32* %0, i32* nocapture %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
%9 = icmp sgt i32 %2, 8
%10 = icmp eq i32 %2, 0
%11 = or i1 %9, %10
br i1 %11, label %44, label %12
12: ; preds = %6
%13 = icmp eq i32 %2, 1
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = load i32, i32* %1
%16 = tail call i32 @ltc185x_read_ch(i32* %0, i32 %15, i32 %3, i32 %4, i32 %5)
br label %44
17: ; preds = %12
%18 = tail call i32 (...) @ltc185x_en()
%19 = load i32, i32* %1
%20 = tail call i32 @ltc185x_create_cmd(i32 %19, i32 %3, i32 %4, i32 %5)
%21 = call i32 @ltc185x_xfer(i32 %20, i32* %7)
%22 = call i32 (...) @ltc185x_start_conv()
%23 = call i32 @ltc185x_xfer(i32 %20, i32* %7)
%24 = call i32 (...) @ltc185x_start_conv()
%25 = icmp sgt i32 %2, 0
br i1 %25, label %26, label %44
26: ; preds = %17
%27 = zext i32 %2 to i64
%28 = zext i32 %2 to i64
br label %29
29: ; preds = %26, %38
%30 = phi i64 [ 0, %26 ], [ %32, %38 ]
%31 = phi i32 [ %20, %26 ], [ %39, %38 ]
%32 = add nuw nsw i64 %30, 1
%33 = icmp ult i64 %32, %27
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = getelementptr inbounds i32, i32* %1, i64 %32
%36 = load i32, i32* %35
%37 = call i32 @ltc185x_create_cmd(i32 %36, i32 %3, i32 %4, i32 %5)
br label %38
38: ; preds = %29, %34
%39 = phi i32 [ %37, %34 ], [ %31, %29 ]
%40 = getelementptr inbounds i32, i32* %0, i64 %30
%41 = call i32 @ltc185x_xfer(i32 %39, i32* %40)
%42 = call i32 (...) @ltc185x_start_conv()
%43 = icmp eq i64 %32, %28
br i1 %43, label %44, label %29
44: ; preds = %38, %17, %6, %14
%45 = phi i32 [ 1, %14 ], [ -1, %6 ], [ 0, %17 ], [ %2, %38 ]
ret i32 %45
}