#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 termios {
  int /*<<< orphan*/ c_cflag;
  int /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ B19200;
int /*<<< orphan*/ B38400;
int /*<<< orphan*/ B4800;
int /*<<< orphan*/ B57600;
int /*<<< orphan*/ B9600;
int /*<<< orphan*/ CREAD;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ crash(char *, ...);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void setserial(int port, int baudrate, char *dev) {
  struct termios t;
  if (tcgetattr(port, &t) < 0)
    crash("tcgetattr(%s)", dev);
  cfmakeraw(&t);
  t.c_lflag |= ICANON;
  t.c_cflag |= CREAD;
  switch (baudrate) {
  case 0:
    break;
  case 4800:
    cfsetspeed(&t, B4800);
    break;
  case 9600:
    cfsetspeed(&t, B9600);
    break;
  case 19200:
    cfsetspeed(&t, B19200);
    break;
  case 38400:
    cfsetspeed(&t, B38400);
    break;
  case 57600:
    cfsetspeed(&t, B57600);
    break;
  case 115200:
    cfsetspeed(&t, B115200);
    break;
  default:
    crash("Unsupported baudrate: %d", baudrate);
  }
  tcflush(port, TCIFLUSH);
  if (tcsetattr(port, TCSANOW, &t) < 0)
    crash("tcsetattr(%s)", dev);
}


x86 assembly (clang, -O0):
<setserial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
jge    119a <setserial+0x3a>
mov    -0x10(%rbp),%rsi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <crash@plt>
lea    -0x18(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
test   %eax,%eax
je     124a <setserial+0xea>
jmp    11d8 <setserial+0x78>
mov    -0x1c(%rbp),%eax
sub    $0x12c0,%eax
je     124f <setserial+0xef>
jmp    11eb <setserial+0x8b>
mov    -0x1c(%rbp),%eax
sub    $0x2580,%eax
je     1266 <setserial+0x106>
jmp    11fe <setserial+0x9e>
mov    -0x1c(%rbp),%eax
sub    $0x4b00,%eax
je     127d <setserial+0x11d>
jmp    1211 <setserial+0xb1>
mov    -0x1c(%rbp),%eax
sub    $0x9600,%eax
je     1294 <setserial+0x134>
jmp    1224 <setserial+0xc4>
mov    -0x1c(%rbp),%eax
sub    $0xe100,%eax
je     12ab <setserial+0x14b>
jmp    1237 <setserial+0xd7>
mov    -0x1c(%rbp),%eax
sub    $0x1c200,%eax
je     12c2 <setserial+0x162>
jmp    12d9 <setserial+0x179>
jmp    12ea <setserial+0x18a>
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetspeed@plt>
jmp    12ea <setserial+0x18a>
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetspeed@plt>
jmp    12ea <setserial+0x18a>
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetspeed@plt>
jmp    12ea <setserial+0x18a>
mov    0x2cf5(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetspeed@plt>
jmp    12ea <setserial+0x18a>
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetspeed@plt>
jmp    12ea <setserial+0x18a>
mov    0x2cdf(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetspeed@plt>
jmp    12ea <setserial+0x18a>
mov    -0x8(%rbp),%esi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1030 <crash@plt>
mov    -0x4(%rbp),%edi
mov    0x2cbc(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
jge    132c <setserial+0x1cc>
mov    -0x10(%rbp),%rsi
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1030 <crash@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setserial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1070 <tcgetattr@plt>
test   %rax,%rax
jns    118f <setserial+0x2f>
lea    0xe7b(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <crash@plt>
lea    0x8(%rsp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
or     %eax,0xc(%rsp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
or     %eax,0x8(%rsp)
cmp    $0x4aff,%ebx
jle    11dc <setserial+0x7c>
cmp    $0xe0ff,%ebx
jg     11f9 <setserial+0x99>
cmp    $0x4b00,%ebx
je     1212 <setserial+0xb2>
cmp    $0x9600,%ebx
jne    1224 <setserial+0xc4>
mov    0x2db6(%rip),%rax
jmp    123d <setserial+0xdd>
test   %ebx,%ebx
je     1249 <setserial+0xe9>
cmp    $0x12c0,%ebx
je     1236 <setserial+0xd6>
cmp    $0x2580,%ebx
jne    1224 <setserial+0xc4>
mov    0x2dd9(%rip),%rax
jmp    123d <setserial+0xdd>
cmp    $0xe100,%ebx
je     121b <setserial+0xbb>
cmp    $0x1c200,%ebx
jne    1224 <setserial+0xc4>
mov    0x2d98(%rip),%rax
jmp    123d <setserial+0xdd>
mov    0x2dd7(%rip),%rax
jmp    123d <setserial+0xdd>
mov    0x2d9e(%rip),%rax
jmp    123d <setserial+0xdd>
lea    0xde3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <crash@plt>
jmp    1249 <setserial+0xe9>
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1050 <cfsetspeed@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1080 <tcsetattr@plt>
test   %rax,%rax
jns    1284 <setserial+0x124>
lea    0xdad(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <crash@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@.str = [14 x i8] c"tcgetattr(%s)\00"
@ICANON = global i32 0
@CREAD = global i32 0
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B115200 = global i32 0
@.str.1 = [25 x i8] c"Unsupported baudrate: %d\00"
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [14 x i8] c"tcsetattr(%s)\00"
define void @setserial(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i32, i32* %4
%9 = call i64 @tcgetattr(i32 %8, %struct.termios* %7)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = call i32 (i8*, ...) @crash(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %3
%15 = call i32 @cfmakeraw(%struct.termios* %7)
%16 = load i32, i32* @ICANON
%17 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%18 = load i32, i32* %17
%19 = or i32 %18, %16
store i32 %19, i32* %17
%20 = load i32, i32* @CREAD
%21 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%22 = load i32, i32* %21
%23 = or i32 %22, %20
store i32 %23, i32* %21
%24 = load i32, i32* %5
switch i32 %24, label %44 [
i32 0, label %25
i32 4800, label %26
i32 9600, label %29
i32 19200, label %32
i32 38400, label %35
i32 57600, label %38
i32 115200, label %41
]
25: ; preds = %14
br label %47
26: ; preds = %14
%27 = load i32, i32* @B4800
%28 = call i32 @cfsetspeed(%struct.termios* %7, i32 %27)
br label %47
29: ; preds = %14
%30 = load i32, i32* @B9600
%31 = call i32 @cfsetspeed(%struct.termios* %7, i32 %30)
br label %47
32: ; preds = %14
%33 = load i32, i32* @B19200
%34 = call i32 @cfsetspeed(%struct.termios* %7, i32 %33)
br label %47
35: ; preds = %14
%36 = load i32, i32* @B38400
%37 = call i32 @cfsetspeed(%struct.termios* %7, i32 %36)
br label %47
38: ; preds = %14
%39 = load i32, i32* @B57600
%40 = call i32 @cfsetspeed(%struct.termios* %7, i32 %39)
br label %47
41: ; preds = %14
%42 = load i32, i32* @B115200
%43 = call i32 @cfsetspeed(%struct.termios* %7, i32 %42)
br label %47
44: ; preds = %14
%45 = load i32, i32* %5
%46 = call i32 (i8*, ...) @crash(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %44, %41, %38, %35, %32, %29, %26, %25
%48 = load i32, i32* %4
%49 = load i32, i32* @TCIFLUSH
%50 = call i32 @tcflush(i32 %48, i32 %49)
%51 = load i32, i32* %4
%52 = load i32, i32* @TCSANOW
%53 = call i64 @tcsetattr(i32 %51, i32 %52, %struct.termios* %7)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %47
%56 = load i8*, i8** %6
%57 = call i32 (i8*, ...) @crash(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %55, %47
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@.str = [14 x i8] c"tcgetattr(%s)\00"
@ICANON = global i32 0
@CREAD = global i32 0
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B115200 = global i32 0
@.str.1 = [25 x i8] c"Unsupported baudrate: %d\00"
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [14 x i8] c"tcsetattr(%s)\00"
define void @setserial(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = call i64 @tcgetattr(i32 %0, %struct.termios* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = call i32 (i8*, ...) @crash(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %10
10: ; preds = %8, %3
%11 = call i32 @cfmakeraw(%struct.termios* %4)
%12 = load i32, i32* @ICANON
%13 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%14 = load i32, i32* %13
%15 = or i32 %14, %12
store i32 %15, i32* %13
%16 = load i32, i32* @CREAD
%17 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%18 = load i32, i32* %17
%19 = or i32 %18, %16
store i32 %19, i32* %17
switch i32 %1, label %38 [
i32 0, label %40
i32 4800, label %20
i32 9600, label %23
i32 19200, label %26
i32 38400, label %29
i32 57600, label %32
i32 115200, label %35
]
20: ; preds = %10
%21 = load i32, i32* @B4800
%22 = call i32 @cfsetspeed(%struct.termios* %4, i32 %21)
br label %40
23: ; preds = %10
%24 = load i32, i32* @B9600
%25 = call i32 @cfsetspeed(%struct.termios* %4, i32 %24)
br label %40
26: ; preds = %10
%27 = load i32, i32* @B19200
%28 = call i32 @cfsetspeed(%struct.termios* %4, i32 %27)
br label %40
29: ; preds = %10
%30 = load i32, i32* @B38400
%31 = call i32 @cfsetspeed(%struct.termios* %4, i32 %30)
br label %40
32: ; preds = %10
%33 = load i32, i32* @B57600
%34 = call i32 @cfsetspeed(%struct.termios* %4, i32 %33)
br label %40
35: ; preds = %10
%36 = load i32, i32* @B115200
%37 = call i32 @cfsetspeed(%struct.termios* %4, i32 %36)
br label %40
38: ; preds = %10
%39 = call i32 (i8*, ...) @crash(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %40
40: ; preds = %38, %35, %32, %29, %26, %23, %20, %10
%41 = load i32, i32* @TCIFLUSH
%42 = call i32 @tcflush(i32 %0, i32 %41)
%43 = load i32, i32* @TCSANOW
%44 = call i64 @tcsetattr(i32 %0, i32 %43, %struct.termios* %4)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = call i32 (i8*, ...) @crash(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %48
48: ; preds = %46, %40
ret void
}