#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*/);
char get_digital(int fd, int channel) {
  unsigned char buffer[100];
  int got;
  char chan[8] = {'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K'};
  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 < 1)
    return (-1);
  return (buffer[0]);
}


x86 assembly (clang, -O0):
<get_digital>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xeb8(%rip),%rax
mov    %rax,-0x7c(%rbp)
cmpl   $0x1,-0xc(%rbp)
jl     1160 <get_digital+0x30>
cmpl   $0x8,-0xc(%rbp)
jle    1169 <get_digital+0x39>
movb   $0xff,-0x1(%rbp)
jmp    11cf <get_digital+0x9f>
mov    -0x8(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
cmp    $0x0,%rax
je     118d <get_digital+0x5d>
movb   $0xff,-0x1(%rbp)
jmp    11cf <get_digital+0x9f>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x7c(%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   $0x1,-0x74(%rbp)
jge    11c9 <get_digital+0x99>
movb   $0xff,-0x1(%rbp)
jmp    11cf <get_digital+0x9f>
mov    -0x70(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_digital>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %esi,%ebx
lea    -0x9(%rbx),%eax
mov    $0xff,%bpl
cmp    $0xfffffff8,%eax
jb     118d <get_digital+0x5d>
mov    %edi,%r14d
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
test   %rax,%rax
jne    118d <get_digital+0x5d>
add    $0xffffffff,%ebx
lea    0xe9b(%rip),%rax
movsbl (%rbx,%rax,1),%esi
mov    %r14d,%edi
call   1050 <send_command@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x64,%edx
call   1040 <read@plt>
mov    $0xff,%ebp
test   %eax,%eax
jle    118d <get_digital+0x5d>
mov    (%rsp),%ebp
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@__const.get_digital.chan = [8 x i8] c"ASDFGHJK"
@TCIOFLUSH = global i32 0
define signext i8 @get_digital(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 %21, 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 %21
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_digital.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, 1
%19 = load i8, i8* %4
%20 = select i1 %18, i8 -1, i8 %19
br label %21
21: ; preds = %11, %7, %2
%22 = phi i8 [ -1, %2 ], [ -1, %7 ], [ %20, %11 ]
ret i8 %22
}