#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_cc;
};

/* Variables and functions */
int B115200;
int B38400;
int B9600;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int skytraq_setspeed(int fd, int baud) {
  int b;
  int ret;
  struct termios term;
  switch (baud) {
  case 9600:
    b = B9600;
    break;
  case 38400:
    b = B38400;
    break;
  case 115200:
    b = B115200;
    break;
  default:
    fprintf(stderr, "Invalid baudrate %d\n", baud);
    return -1;
  }
  ret = tcgetattr(fd, &term);
  cfmakeraw(&term);
  term.c_cc[VMIN] = 0;
  term.c_cc[VTIME] = 1;
  cfsetspeed(&term, b);
  ret = tcsetattr(fd, TCSAFLUSH, &term);
  return ret;
}


x86 assembly (clang, -O0):
<skytraq_setspeed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x2580,%eax
je     119a <skytraq_setspeed+0x4a>
jmp    1174 <skytraq_setspeed+0x24>
mov    -0x24(%rbp),%eax
sub    $0x9600,%eax
je     11ab <skytraq_setspeed+0x5b>
jmp    1187 <skytraq_setspeed+0x37>
mov    -0x24(%rbp),%eax
sub    $0x1c200,%eax
je     11bc <skytraq_setspeed+0x6c>
jmp    11cd <skytraq_setspeed+0x7d>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11f1 <skytraq_setspeed+0xa1>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11f1 <skytraq_setspeed+0xa1>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11f1 <skytraq_setspeed+0xa1>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe20(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <skytraq_setspeed+0x10d>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    -0x20(%rbp),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x10(%rbp),%esi
lea    -0x20(%rbp),%rdi
call   1030 <cfsetspeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skytraq_setspeed>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
cmp    $0x2580,%esi
je     117e <skytraq_setspeed+0x2e>
mov    %esi,%edx
cmp    $0x1c200,%esi
je     1187 <skytraq_setspeed+0x37>
cmp    $0x9600,%edx
jne    11ef <skytraq_setspeed+0x9f>
mov    0x2e2c(%rip),%rax
jmp    118e <skytraq_setspeed+0x3e>
mov    0x2e5b(%rip),%rax
jmp    118e <skytraq_setspeed+0x3e>
mov    0x2e32(%rip),%rax
mov    (%rax),%ebp
lea    0x8(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <tcgetattr@plt>
mov    %rbx,%rdi
call   1050 <cfmakeraw@plt>
mov    0x8(%rsp),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2df3(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <cfsetspeed@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1070 <tcsetattr@plt>
jmp    1209 <skytraq_setspeed+0xb9>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32* }
@B9600 = global i32 0
@B38400 = global i32 0
@B115200 = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Invalid baudrate %d\0A\00"
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define i32 @skytraq_setspeed(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
switch i32 %9, label %16 [
i32 9600, label %10
i32 38400, label %12
i32 115200, label %14
]
10: ; preds = %2
%11 = load i32, i32* @B9600
store i32 %11, i32* %6
br label %20
12: ; preds = %2
%13 = load i32, i32* @B38400
store i32 %13, i32* %6
br label %20
14: ; preds = %2
%15 = load i32, i32* @B115200
store i32 %15, i32* %6
br label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i32, i32* %5
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 -1, i32* %3
br label %38
20: ; preds = %14, %12, %10
%21 = load i32, i32* %4
%22 = call i32 @tcgetattr(i32 %21, %struct.termios* %8)
store i32 %22, i32* %7
%23 = call i32 @cfmakeraw(%struct.termios* %8)
%24 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%25 = load i32*, i32** %24
%26 = load i64, i64* @VMIN
%27 = getelementptr inbounds i32, i32* %25, i64 %26
store i32 0, i32* %27
%28 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%29 = load i32*, i32** %28
%30 = load i64, i64* @VTIME
%31 = getelementptr inbounds i32, i32* %29, i64 %30
store i32 1, i32* %31
%32 = load i32, i32* %6
%33 = call i32 @cfsetspeed(%struct.termios* %8, i32 %32)
%34 = load i32, i32* %4
%35 = load i32, i32* @TCSAFLUSH
%36 = call i32 @tcsetattr(i32 %34, i32 %35, %struct.termios* %8)
store i32 %36, i32* %7
%37 = load i32, i32* %7
store i32 %37, i32* %3
br label %38
38: ; preds = %20, %16
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.termios = type { i32* }
@B9600 = global i32 0
@B38400 = global i32 0
@B115200 = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Invalid baudrate %d\0A\00"
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define i32 @skytraq_setspeed(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
switch i32 %1, label %7 [
i32 9600, label %10
i32 38400, label %5
i32 115200, label %6
]
5: ; preds = %2
br label %10
6: ; preds = %2
br label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %24
10: ; preds = %2, %6, %5
%11 = phi i32* [ @B115200, %6 ], [ @B38400, %5 ], [ @B9600, %2 ]
%12 = load i32, i32* %11
%13 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%14 = call i32 @cfmakeraw(%struct.termios* %3)
%15 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%16 = load i32*, i32** %15
%17 = load i64, i64* @VMIN
%18 = getelementptr inbounds i32, i32* %16, i64 %17
store i32 0, i32* %18
%19 = load i64, i64* @VTIME
%20 = getelementptr inbounds i32, i32* %16, i64 %19
store i32 1, i32* %20
%21 = call i32 @cfsetspeed(%struct.termios* %3, i32 %12)
%22 = load i32, i32* @TCSAFLUSH
%23 = call i32 @tcsetattr(i32 %0, i32 %22, %struct.termios* %3)
br label %24
24: ; preds = %10, %7
%25 = phi i32 [ -1, %7 ], [ %23, %10 ]
ret i32 %25
}