#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 /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ TCIOFLUSH;
int read(int, unsigned char *, int);
int /*<<< orphan*/ send_command(int, char);
scalar_t__ tcflush(int, int /*<<< orphan*/);
int get_analog(int fd, int channel) {
  unsigned char buffer[100];
  int got, intvalue;
  char chan[8] = {'Z', 'X', 'C', 'V', 'B', 'N', 'M', ','};
  if ((channel < 1) || (channel > 8))
    return (-1);
  if (tcflush(fd, TCIOFLUSH) != 0)
    return (-1);
  send_command(fd, chan[channel - 1]);
  got = read(fd, buffer, sizeof(buffer));
  if (got < 2)
    return (-1);
  intvalue = buffer[0] * 256 + buffer[1];
  return (intvalue);
}


x86 assembly (clang, -O0):
<get_analog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xeb8(%rip),%rax
mov    %rax,-0x80(%rbp)
cmpl   $0x1,-0xc(%rbp)
jl     1160 <get_analog+0x30>
cmpl   $0x8,-0xc(%rbp)
jle    116c <get_analog+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <get_analog+0xb8>
mov    -0x8(%rbp),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
cmp    $0x0,%rax
je     1193 <get_analog+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <get_analog+0xb8>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x80(%rbp,%rax,1),%esi
call   1050 <send_command@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   1040 <read@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x2,-0x74(%rbp)
jge    11d2 <get_analog+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <get_analog+0xb8>
movzbl -0x70(%rbp),%eax
shl    $0x8,%eax
movzbl -0x6f(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_analog>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %esi,%ebx
lea    -0x9(%rbx),%eax
mov    $0xffffffff,%r14d
cmp    $0xfffffff8,%eax
jb     1192 <get_analog+0x62>
mov    %edi,%ebp
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
test   %rax,%rax
jne    1192 <get_analog+0x62>
add    $0xffffffff,%ebx
lea    0xe99(%rip),%rax
movsbl (%rbx,%rax,1),%esi
mov    %ebp,%edi
call   1050 <send_command@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x64,%edx
call   1040 <read@plt>
cmp    $0x2,%eax
jl     1192 <get_analog+0x62>
movzwl (%rsp),%r14d
shl    $0x10,%r14d
bswap  %r14d
mov    %r14d,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.get_analog.chan = [8 x i8] c"ZXCVBNM,"
@TCIOFLUSH = global i32 0
define i32 @get_analog(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca [8 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = bitcast [8 x i8]* %9 to i8*
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 1
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 8
br i1 %15, label %16, label %17
16: ; preds = %13, %2
store i32 -1, i32* %3
br label %47
17: ; preds = %13
%18 = load i32, i32* %4
%19 = load i32, i32* @TCIOFLUSH
%20 = call i64 @tcflush(i32 %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %3
br label %47
23: ; preds = %17
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = call i32 @send_command(i32 %24, i8 signext %29)
%31 = load i32, i32* %4
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 100)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 2
br i1 %35, label %36, label %37
36: ; preds = %23
store i32 -1, i32* %3
br label %47
37: ; preds = %23
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = mul nsw i32 %40, 256
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 1
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = add nsw i32 %41, %44
store i32 %45, i32* %8
%46 = load i32, i32* %8
store i32 %46, i32* %3
br label %47
47: ; preds = %37, %36, %22, %16
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@__const.get_analog.chan = [8 x i8] c"ZXCVBNM,"
@TCIOFLUSH = global i32 0
define i32 @get_analog(i32 %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = add i32 %1, -9
%6 = icmp ult i32 %5, -8
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = load i32, i32* @TCIOFLUSH
%9 = tail call i64 @tcflush(i32 %0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %7
%12 = add nsw i32 %1, -1
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds [8 x i8], [8 x i8]* @__const.get_analog.chan, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = tail call i32 @send_command(i32 %0, i8 signext %15)
%17 = call i32 @read(i32 %0, i8* %4, i32 100)
%18 = icmp slt i32 %17, 2
br i1 %18, label %27, label %19
19: ; preds = %11
%20 = load i8, i8* %4
%21 = zext i8 %20 to i32
%22 = shl nuw nsw i32 %21, 8
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 1
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = or i32 %22, %25
br label %27
27: ; preds = %11, %7, %2, %19
%28 = phi i32 [ %26, %19 ], [ -1, %2 ], [ -1, %7 ], [ -1, %11 ]
ret i32 %28
}