#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 c_cflag;
  scalar_t__ *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int B19200;
int B38400;
int B57600;
int B9600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNBRK;
int /*<<< orphan*/ TCSADRAIN;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ bzero(scalar_t__ **, int);
int /*<<< orphan*/ cfsetspeed(struct termios *, int);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int kiss_platform_serial_set_baud(int handle, int baud) {
  int baud_rate;
  struct termios tio;
  switch (baud) {
  case 2604:
    baud_rate = B9600;
    break;
  case 1302:
    baud_rate = B19200;
    break;
  case 651:
    baud_rate = B38400;
    break;
  case 434:
    baud_rate = B57600;
    break;
  default:
    return 0;
  }
  if (tcgetattr(handle, &tio) == -1) {
    return 0;
  }
  tio.c_iflag = IGNBRK;
  tio.c_oflag = 0;
  tio.c_cflag = CREAD | CLOCAL;
  tio.c_cflag |= CS8;
  tio.c_lflag = 0;
  bzero(&tio.c_cc, sizeof(tio.c_cc));
  tio.c_cc[VTIME] = 0;
  tio.c_cc[VMIN] = 0;
  cfsetspeed(&tio, baud_rate);
  if (tcsetattr(handle, TCSADRAIN, &tio) == -1) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<kiss_platform_serial_set_baud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x1b2,%eax
je     11d0 <kiss_platform_serial_set_baud+0x90>
jmp    1164 <kiss_platform_serial_set_baud+0x24>
mov    -0x3c(%rbp),%eax
sub    $0x28b,%eax
je     11bf <kiss_platform_serial_set_baud+0x7f>
jmp    1177 <kiss_platform_serial_set_baud+0x37>
mov    -0x3c(%rbp),%eax
sub    $0x516,%eax
je     11ae <kiss_platform_serial_set_baud+0x6e>
jmp    118a <kiss_platform_serial_set_baud+0x4a>
mov    -0x3c(%rbp),%eax
sub    $0xa2c,%eax
jne    11e1 <kiss_platform_serial_set_baud+0xa1>
jmp    119d <kiss_platform_serial_set_baud+0x5d>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11ed <kiss_platform_serial_set_baud+0xad>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11ed <kiss_platform_serial_set_baud+0xad>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11ed <kiss_platform_serial_set_baud+0xad>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11ed <kiss_platform_serial_set_baud+0xad>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <kiss_platform_serial_set_baud+0x189>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    120e <kiss_platform_serial_set_baud+0xce>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <kiss_platform_serial_set_baud+0x189>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    0x2d5e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x8,%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x30(%rbp),%rax
mov    0x2d35(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
mov    0x2d47(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetspeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    12c2 <kiss_platform_serial_set_baud+0x182>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <kiss_platform_serial_set_baud+0x189>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kiss_platform_serial_set_baud>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
xor    %ebp,%ebp
cmp    $0x515,%esi
jg     1171 <kiss_platform_serial_set_baud+0x31>
cmp    $0x1b2,%esi
je     118e <kiss_platform_serial_set_baud+0x4e>
cmp    $0x28b,%esi
jne    124e <kiss_platform_serial_set_baud+0x10e>
mov    0x2e19(%rip),%rax
jmp    119e <kiss_platform_serial_set_baud+0x5e>
cmp    $0xa2c,%esi
je     1197 <kiss_platform_serial_set_baud+0x57>
cmp    $0x516,%esi
jne    124e <kiss_platform_serial_set_baud+0x10e>
mov    0x2e64(%rip),%rax
jmp    119e <kiss_platform_serial_set_baud+0x5e>
mov    0x2e2b(%rip),%rax
jmp    119e <kiss_platform_serial_set_baud+0x5e>
mov    0x2e42(%rip),%rax
mov    (%rax),%r14d
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1040 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     124e <kiss_platform_serial_set_baud+0x10e>
mov    0x2e1b(%rip),%rax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2df4(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    0x10(%rsp),%rax
mov    0x2d9b(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2db1(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14d,%esi
call   1030 <cfsetspeed@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1050 <tcsetattr@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
setne  %bpl
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64*, i64, i64, i32 }
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@IGNBRK = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@CS8 = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSADRAIN = global i32 0
define i32 @kiss_platform_serial_set_baud(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
switch i32 %8, label %17 [
i32 2604, label %9
i32 1302, label %11
i32 651, label %13
i32 434, label %15
]
9: ; preds = %2
%10 = load i32, i32* @B9600
store i32 %10, i32* %6
br label %18
11: ; preds = %2
%12 = load i32, i32* @B19200
store i32 %12, i32* %6
br label %18
13: ; preds = %2
%14 = load i32, i32* @B38400
store i32 %14, i32* %6
br label %18
15: ; preds = %2
%16 = load i32, i32* @B57600
store i32 %16, i32* %6
br label %18
17: ; preds = %2
store i32 0, i32* %3
br label %54
18: ; preds = %15, %13, %11, %9
%19 = load i32, i32* %4
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %7)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 0, i32* %3
br label %54
23: ; preds = %18
%24 = load i32, i32* @IGNBRK
%25 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 4
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
store i64 0, i64* %26
%27 = load i32, i32* @CREAD
%28 = load i32, i32* @CLOCAL
%29 = or i32 %27, %28
%30 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* @CS8
%32 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%33 = load i32, i32* %32
%34 = or i32 %33, %31
store i32 %34, i32* %32
%35 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
store i64 0, i64* %35
%36 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%37 = call i32 @bzero(i64** %36, i32 8)
%38 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%39 = load i64*, i64** %38
%40 = load i64, i64* @VTIME
%41 = getelementptr inbounds i64, i64* %39, i64 %40
store i64 0, i64* %41
%42 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%43 = load i64*, i64** %42
%44 = load i64, i64* @VMIN
%45 = getelementptr inbounds i64, i64* %43, i64 %44
store i64 0, i64* %45
%46 = load i32, i32* %6
%47 = call i32 @cfsetspeed(%struct.termios* %7, i32 %46)
%48 = load i32, i32* %4
%49 = load i32, i32* @TCSADRAIN
%50 = call i32 @tcsetattr(i32 %48, i32 %49, %struct.termios* %7)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %23
store i32 0, i32* %3
br label %54
53: ; preds = %23
store i32 1, i32* %3
br label %54
54: ; preds = %53, %52, %22, %17
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.termios = type { i32, i64*, i64, i64, i32 }
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@IGNBRK = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@CS8 = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSADRAIN = global i32 0
define i32 @kiss_platform_serial_set_baud(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
switch i32 %1, label %36 [
i32 2604, label %8
i32 1302, label %5
i32 651, label %6
i32 434, label %7
]
5: ; preds = %2
br label %8
6: ; preds = %2
br label %8
7: ; preds = %2
br label %8
8: ; preds = %2, %7, %6, %5
%9 = phi i32* [ @B57600, %7 ], [ @B38400, %6 ], [ @B19200, %5 ], [ @B9600, %2 ]
%10 = load i32, i32* %9
%11 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %36, label %13
13: ; preds = %8
%14 = load i32, i32* @IGNBRK
%15 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @CREAD
%17 = load i32, i32* @CLOCAL
%18 = or i32 %17, %16
%19 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%20 = load i32, i32* @CS8
%21 = or i32 %20, %18
store i32 %21, i32* %19
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%23 = bitcast i64* %22 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %23
%24 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%25 = call i32 @bzero(i64** %24, i32 8)
%26 = load i64*, i64** %24
%27 = load i64, i64* @VTIME
%28 = getelementptr inbounds i64, i64* %26, i64 %27
store i64 0, i64* %28
%29 = load i64, i64* @VMIN
%30 = getelementptr inbounds i64, i64* %26, i64 %29
store i64 0, i64* %30
%31 = call i32 @cfsetspeed(%struct.termios* %3, i32 %10)
%32 = load i32, i32* @TCSADRAIN
%33 = call i32 @tcsetattr(i32 %0, i32 %32, %struct.termios* %3)
%34 = icmp ne i32 %33, -1
%35 = zext i1 %34 to i32
br label %36
36: ; preds = %13, %8, %2
%37 = phi i32 [ 0, %2 ], [ 0, %8 ], [ %35, %13 ]
ret i32 %37
}