#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCIOFLUSH;
int read(int /*<<< orphan*/, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ tcflush(int /*<<< orphan*/, int /*<<< orphan*/);
const char *autobaud(void) {
  int rfds;
  struct timeval timeout;
  char c;
  const char *type = "9600-baud";
  (void)tcflush(0, TCIOFLUSH);
  rfds = 1 << 0;
  timeout.tv_sec = 5;
  timeout.tv_usec = 0;
  if (select(32, (fd_set *)&rfds, (fd_set *)NULL, (fd_set *)NULL, &timeout) <=
      0)
    return (type);
  if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
    return (type);
  timeout.tv_sec = 0;
  timeout.tv_usec = 20;
  (void)select(32, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL, &timeout);
  (void)tcflush(0, TCIOFLUSH);
  switch (c & 0377) {
  case 0200:
    type = "300-baud";
    break;
  case 0346:
    type = "1200-baud";
    break;
  case 015:
  case 0215:
    type = "2400-baud";
    break;
  default:
    type = "4800-baud";
    break;
  case 0377:
    type = "9600-baud";
    break;
  }
  return (type);
}


x86 assembly (clang, -O0):
<autobaud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec1(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <tcflush@plt>
movl   $0x1,-0xc(%rbp)
movl   $0x5,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x20,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
jg     1198 <autobaud+0x68>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b6 <autobaud+0x186>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cltq
cmp    $0x1,%rax
je     11c8 <autobaud+0x98>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b6 <autobaud+0x186>
movl   $0x0,-0x18(%rbp)
movl   $0x14,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,-0x30(%rbp)
mov    %eax,%ecx
mov    $0x20,%edi
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1050 <select@plt>
mov    -0x30(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xd,%eax
je     1283 <autobaud+0x153>
jmp    1217 <autobaud+0xe7>
mov    -0x2c(%rbp),%eax
sub    $0x80,%eax
je     1263 <autobaud+0x133>
jmp    122a <autobaud+0xfa>
mov    -0x2c(%rbp),%eax
sub    $0x8d,%eax
je     1283 <autobaud+0x153>
jmp    123d <autobaud+0x10d>
mov    -0x2c(%rbp),%eax
sub    $0xe6,%eax
je     1273 <autobaud+0x143>
jmp    1250 <autobaud+0x120>
mov    -0x2c(%rbp),%eax
sub    $0xff,%eax
je     12a3 <autobaud+0x173>
jmp    1293 <autobaud+0x163>
lea    0xda0(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    12ae <autobaud+0x17e>
lea    0xd99(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    12ae <autobaud+0x17e>
lea    0xd93(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    12ae <autobaud+0x17e>
lea    0xd8d(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    12ae <autobaud+0x17e>
lea    0xd56(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<autobaud>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2ea2(%rip),%r14
mov    (%r14),%esi
xor    %edi,%edi
call   1030 <tcflush@plt>
movl   $0x1,0x14(%rsp)
movq   $0x5,0x8(%rsp)
lea    0x14(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    $0x20,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
lea    0xe88(%rip),%rbx
test   %rax,%rax
jle    120a <autobaud+0xda>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    120a <autobaud+0xda>
movabs $0x1400000000,%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%r8
mov    $0x20,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
mov    (%r14),%esi
xor    %edi,%edi
call   1030 <tcflush@plt>
mov    0x7(%rsp),%al
cmp    $0xe5,%al
jle    11e9 <autobaud+0xb9>
cmp    $0xd,%al
je     11f1 <autobaud+0xc1>
cmp    $0xff,%al
je     120a <autobaud+0xda>
cmp    $0xe6,%al
jne    1203 <autobaud+0xd3>
lea    0xe2c(%rip),%rbx
jmp    120a <autobaud+0xda>
cmp    $0x80,%al
je     11fa <autobaud+0xca>
cmp    $0x8d,%al
jne    1203 <autobaud+0xd3>
lea    0xe25(%rip),%rbx
jmp    120a <autobaud+0xda>
lea    0xe09(%rip),%rbx
jmp    120a <autobaud+0xda>
lea    0xe1d(%rip),%rbx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [10 x i8] c"9600-baud\00"
@TCIOFLUSH = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [9 x i8] c"300-baud\00"
@.str.2 = [10 x i8] c"1200-baud\00"
@.str.3 = [10 x i8] c"2400-baud\00"
@.str.4 = [10 x i8] c"4800-baud\00"
define i8* @autobaud() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i8
%5 = alloca i8*
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i32, i32* @TCIOFLUSH
%7 = call i32 @tcflush(i32 0, i32 %6)
store i32 1, i32* %2
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 5, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i32 0, i32* %9
%10 = call i64 @select(i32 32, i32* %2, i32* null, i32* null, %struct.timeval* %3)
%11 = icmp sle i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = load i8*, i8** %5
store i8* %13, i8** %1
br label %37
14: ; preds = %0
%15 = load i32, i32* @STDIN_FILENO
%16 = call i32 @read(i32 %15, i8* %4, i32 1)
%17 = sext i32 %16 to i64
%18 = icmp ne i64 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i8*, i8** %5
store i8* %20, i8** %1
br label %37
21: ; preds = %14
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 0, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i32 20, i32* %23
%24 = call i64 @select(i32 32, i32* null, i32* null, i32* null, %struct.timeval* %3)
%25 = load i32, i32* @TCIOFLUSH
%26 = call i32 @tcflush(i32 0, i32 %25)
%27 = load i8, i8* %4
%28 = sext i8 %27 to i32
%29 = and i32 %28, 255
switch i32 %29, label %33 [
i32 128, label %30
i32 230, label %31
i32 13, label %32
i32 141, label %32
i32 255, label %34
]
30: ; preds = %21
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %5
br label %35
31: ; preds = %21
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %5
br label %35
32: ; preds = %21, %21
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8** %5
br label %35
33: ; preds = %21
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8** %5
br label %35
34: ; preds = %21
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %5
br label %35
35: ; preds = %34, %33, %32, %31, %30
%36 = load i8*, i8** %5
store i8* %36, i8** %1
br label %37
37: ; preds = %35, %19, %12
%38 = load i8*, i8** %1
ret i8* %38
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [10 x i8] c"9600-baud\00"
@TCIOFLUSH = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [9 x i8] c"300-baud\00"
@.str.2 = [10 x i8] c"1200-baud\00"
@.str.3 = [10 x i8] c"2400-baud\00"
@.str.4 = [10 x i8] c"4800-baud\00"
define i8* @autobaud() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i8
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.timeval* %2 to i8*
%6 = load i32, i32* @TCIOFLUSH
%7 = tail call i32 @tcflush(i32 0, i32 %6)
store i32 1, i32* %1
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 5, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i32 0, i32* %9
%10 = call i64 @select(i32 32, i32* %1, i32* null, i32* null, %struct.timeval* %2)
%11 = icmp slt i64 %10, 1
br i1 %11, label %25, label %12
12: ; preds = %0
%13 = load i32, i32* @STDIN_FILENO
%14 = call i32 @read(i32 %13, i8* %3, i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %25
16: ; preds = %12
store i32 0, i32* %8
store i32 20, i32* %9
%17 = call i64 @select(i32 32, i32* null, i32* null, i32* null, %struct.timeval* %2)
%18 = load i32, i32* @TCIOFLUSH
%19 = call i32 @tcflush(i32 0, i32 %18)
%20 = load i8, i8* %3
switch i8 %20, label %23 [
i8 -128, label %25
i8 -26, label %21
i8 13, label %22
i8 -115, label %22
i8 -1, label %24
]
21: ; preds = %16
br label %25
22: ; preds = %16, %16
br label %25
23: ; preds = %16
br label %25
24: ; preds = %16
br label %25
25: ; preds = %21, %22, %23, %24, %16, %12, %0
%26 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %12 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), %23 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %24 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %22 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %21 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %16 ]
ret i8* %26
}