#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*/ tty;
struct termios {
  int dummy;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int /*<<< orphan*/ B0;
int /*<<< orphan*/ B110;
int /*<<< orphan*/ B1200;
int /*<<< orphan*/ B134;
int /*<<< orphan*/ B150;
int /*<<< orphan*/ B1800;
int /*<<< orphan*/ B19200;
int /*<<< orphan*/ B200;
int /*<<< orphan*/ B2400;
int /*<<< orphan*/ B300;
int /*<<< orphan*/ B38400;
int /*<<< orphan*/ B4800;
int /*<<< orphan*/ B50;
int /*<<< orphan*/ B600;
int /*<<< orphan*/ B75;
int /*<<< orphan*/ B9600;
int /*<<< orphan*/ TCSANOW;
scalar_t__ cfsetispeed(struct termios *, int /*<<< orphan*/);
scalar_t__ cfsetospeed(struct termios *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ msleep(int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_baud(int fd, unsigned int baud_rate) {
  speed_t baud;
  int custom_baud = 0;
  struct termios tty;
  memset(&tty, 0, sizeof(tty));
  if (tcgetattr(fd, &tty) != 0) {
    return errno;
  }
  msleep(45);
  switch (baud_rate) {
  case 0:
    baud = B0;
    break;
  case 50:
    baud = B50;
    break;
  case 75:
    baud = B75;
    break;
  case 110:
    baud = B110;
    break;
  case 134:
    baud = B134;
    break;
  case 150:
    baud = B150;
    break;
  case 200:
    baud = B200;
    break;
  case 300:
    baud = B300;
    break;
  case 600:
    baud = B600;
    break;
  case 1200:
    baud = B1200;
    break;
  case 1800:
    baud = B1800;
    break;
  case 2400:
    baud = B2400;
    break;
  case 4800:
    baud = B4800;
    break;
  case 9600:
    baud = B9600;
    break;
  case 19200:
    baud = B19200;
    break;
  case 38400:
    baud = B38400;
    break;
  default:
    baud = B38400;
    custom_baud = 1;
    break;
  }
  if (cfsetispeed(&tty, baud) != 0 || cfsetospeed(&tty, baud) != 0) {
    return errno;
  }
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
  }
  if (custom_baud) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_baud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
je     11ac <set_baud+0x4c>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1481 <set_baud+0x321>
mov    $0x2d,%edi
call   1040 <msleep@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
test   %eax,%eax
je     12e0 <set_baud+0x180>
jmp    11c9 <set_baud+0x69>
mov    -0x1c(%rbp),%eax
sub    $0x32,%eax
je     12f1 <set_baud+0x191>
jmp    11da <set_baud+0x7a>
mov    -0x1c(%rbp),%eax
sub    $0x4b,%eax
je     1302 <set_baud+0x1a2>
jmp    11eb <set_baud+0x8b>
mov    -0x1c(%rbp),%eax
sub    $0x6e,%eax
je     1313 <set_baud+0x1b3>
jmp    11fc <set_baud+0x9c>
mov    -0x1c(%rbp),%eax
sub    $0x86,%eax
je     1324 <set_baud+0x1c4>
jmp    120f <set_baud+0xaf>
mov    -0x1c(%rbp),%eax
sub    $0x96,%eax
je     1335 <set_baud+0x1d5>
jmp    1222 <set_baud+0xc2>
mov    -0x1c(%rbp),%eax
sub    $0xc8,%eax
je     1346 <set_baud+0x1e6>
jmp    1235 <set_baud+0xd5>
mov    -0x1c(%rbp),%eax
sub    $0x12c,%eax
je     1357 <set_baud+0x1f7>
jmp    1248 <set_baud+0xe8>
mov    -0x1c(%rbp),%eax
sub    $0x258,%eax
je     1368 <set_baud+0x208>
jmp    125b <set_baud+0xfb>
mov    -0x1c(%rbp),%eax
sub    $0x4b0,%eax
je     1379 <set_baud+0x219>
jmp    126e <set_baud+0x10e>
mov    -0x1c(%rbp),%eax
sub    $0x708,%eax
je     138a <set_baud+0x22a>
jmp    1281 <set_baud+0x121>
mov    -0x1c(%rbp),%eax
sub    $0x960,%eax
je     139b <set_baud+0x23b>
jmp    1294 <set_baud+0x134>
mov    -0x1c(%rbp),%eax
sub    $0x12c0,%eax
je     13ac <set_baud+0x24c>
jmp    12a7 <set_baud+0x147>
mov    -0x1c(%rbp),%eax
sub    $0x2580,%eax
je     13bd <set_baud+0x25d>
jmp    12ba <set_baud+0x15a>
mov    -0x1c(%rbp),%eax
sub    $0x4b00,%eax
je     13ce <set_baud+0x26e>
jmp    12cd <set_baud+0x16d>
mov    -0x1c(%rbp),%eax
sub    $0x9600,%eax
je     13df <set_baud+0x27f>
jmp    13f0 <set_baud+0x290>
mov    0x2ca1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2ca8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2ccf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2c95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2c34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2c4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2c4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2c59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2be0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2bef(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2bd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2c05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2bec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2c1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2b6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1403 <set_baud+0x2a3>
mov    0x2b59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1080 <cfsetispeed@plt>
cmp    $0x0,%rax
jne    142f <set_baud+0x2cf>
mov    -0x10(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1030 <cfsetospeed@plt>
cmp    $0x0,%rax
je     1440 <set_baud+0x2e0>
mov    0x2b32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1481 <set_baud+0x321>
mov    -0x8(%rbp),%edi
mov    0x2b86(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     1464 <set_baud+0x304>
jmp    1464 <set_baud+0x304>
cmpl   $0x0,-0x14(%rbp)
je     147a <set_baud+0x31a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1481 <set_baud+0x321>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_baud>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <tcgetattr@plt>
test   %rax,%rax
jne    136c <set_baud+0x20c>
mov    $0x2d,%edi
call   1040 <msleep@plt>
cmp    $0x257,%ebp
jg     11d7 <set_baud+0x77>
cmp    $0x85,%ebp
jg     1211 <set_baud+0xb1>
cmp    $0x4a,%ebp
jg     1275 <set_baud+0x115>
test   %ebp,%ebp
je     12e6 <set_baud+0x186>
cmp    $0x32,%ebp
jne    133c <set_baud+0x1dc>
xor    %ebp,%ebp
mov    0x2dce(%rip),%rax
jmp    1348 <set_baud+0x1e8>
cmp    $0x12bf,%ebp
jg     1243 <set_baud+0xe3>
cmp    $0x707,%ebp
jg     1295 <set_baud+0x135>
cmp    $0x258,%ebp
je     12ef <set_baud+0x18f>
cmp    $0x4b0,%ebp
jne    133c <set_baud+0x1dc>
xor    %ebp,%ebp
mov    0x2d54(%rip),%rax
jmp    1348 <set_baud+0x1e8>
cmp    $0xc7,%ebp
jg     12b7 <set_baud+0x157>
cmp    $0x86,%ebp
je     12fa <set_baud+0x19a>
cmp    $0x96,%ebp
jne    133c <set_baud+0x1dc>
xor    %ebp,%ebp
mov    0x2d32(%rip),%rax
jmp    1348 <set_baud+0x1e8>
cmp    $0x4aff,%ebp
jg     12d2 <set_baud+0x172>
cmp    $0x12c0,%ebp
je     1305 <set_baud+0x1a5>
cmp    $0x2580,%ebp
jne    133c <set_baud+0x1dc>
xor    %ebp,%ebp
mov    0x2d40(%rip),%rax
jmp    1348 <set_baud+0x1e8>
cmp    $0x4b,%ebp
je     1310 <set_baud+0x1b0>
cmp    $0x6e,%ebp
jne    133c <set_baud+0x1dc>
xor    %ebp,%ebp
mov    0x2d50(%rip),%rax
jmp    1348 <set_baud+0x1e8>
cmp    $0x708,%ebp
je     131b <set_baud+0x1bb>
cmp    $0x960,%ebp
jne    133c <set_baud+0x1dc>
xor    %ebp,%ebp
mov    0x2cc6(%rip),%rax
jmp    1348 <set_baud+0x1e8>
cmp    $0xc8,%ebp
je     1326 <set_baud+0x1c6>
cmp    $0x12c,%ebp
jne    133c <set_baud+0x1dc>
xor    %ebp,%ebp
mov    0x2cd8(%rip),%rax
jmp    1348 <set_baud+0x1e8>
cmp    $0x4b00,%ebp
je     1331 <set_baud+0x1d1>
cmp    $0x9600,%ebp
jne    133c <set_baud+0x1dc>
xor    %ebp,%ebp
jmp    1341 <set_baud+0x1e1>
mov    0x2c9b(%rip),%rax
jmp    1348 <set_baud+0x1e8>
xor    %ebp,%ebp
mov    0x2cd0(%rip),%rax
jmp    1348 <set_baud+0x1e8>
xor    %ebp,%ebp
mov    0x2cbd(%rip),%rax
jmp    1348 <set_baud+0x1e8>
xor    %ebp,%ebp
mov    0x2caa(%rip),%rax
jmp    1348 <set_baud+0x1e8>
xor    %ebp,%ebp
mov    0x2cbf(%rip),%rax
jmp    1348 <set_baud+0x1e8>
xor    %ebp,%ebp
mov    0x2c5c(%rip),%rax
jmp    1348 <set_baud+0x1e8>
xor    %ebp,%ebp
mov    0x2c69(%rip),%rax
jmp    1348 <set_baud+0x1e8>
xor    %ebp,%ebp
mov    0x2cb6(%rip),%rax
jmp    1348 <set_baud+0x1e8>
mov    $0xffffffff,%ebp
mov    0x2c08(%rip),%rax
mov    (%rax),%ebx
lea    0x8(%rsp),%rdi
mov    %ebx,%esi
call   1080 <cfsetispeed@plt>
test   %rax,%rax
jne    136c <set_baud+0x20c>
lea    0x8(%rsp),%rdi
mov    %ebx,%esi
call   1030 <cfsetospeed@plt>
test   %rax,%rax
je     1380 <set_baud+0x220>
mov    0x2bf5(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2c49(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1070 <tcsetattr@plt>
jmp    1375 <set_baud+0x215>

LLVM IR (-O0):
%struct.termios = type { i32 }
@errno = global i32 0
@B0 = global i32 0
@B50 = global i32 0
@B75 = global i32 0
@B110 = global i32 0
@B134 = global i32 0
@B150 = global i32 0
@B200 = global i32 0
@B300 = global i32 0
@B600 = global i32 0
@B1200 = global i32 0
@B1800 = global i32 0
@B2400 = global i32 0
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@TCSANOW = global i32 0
define i32 @set_baud(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
store i32 0, i32* %7
%9 = call i32 @memset(%struct.termios* %8, i32 0, i32 4)
%10 = load i32, i32* %4
%11 = call i64 @tcgetattr(i32 %10, %struct.termios* %8)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* @errno
store i32 %14, i32* %3
br label %73
15: ; preds = %2
%16 = call i32 @msleep(i32 45)
%17 = load i32, i32* %5
switch i32 %17, label %50 [
i32 0, label %18
i32 50, label %20
i32 75, label %22
i32 110, label %24
i32 134, label %26
i32 150, label %28
i32 200, label %30
i32 300, label %32
i32 600, label %34
i32 1200, label %36
i32 1800, label %38
i32 2400, label %40
i32 4800, label %42
i32 9600, label %44
i32 19200, label %46
i32 38400, label %48
]
18: ; preds = %15
%19 = load i32, i32* @B0
store i32 %19, i32* %6
br label %52
20: ; preds = %15
%21 = load i32, i32* @B50
store i32 %21, i32* %6
br label %52
22: ; preds = %15
%23 = load i32, i32* @B75
store i32 %23, i32* %6
br label %52
24: ; preds = %15
%25 = load i32, i32* @B110
store i32 %25, i32* %6
br label %52
26: ; preds = %15
%27 = load i32, i32* @B134
store i32 %27, i32* %6
br label %52
28: ; preds = %15
%29 = load i32, i32* @B150
store i32 %29, i32* %6
br label %52
30: ; preds = %15
%31 = load i32, i32* @B200
store i32 %31, i32* %6
br label %52
32: ; preds = %15
%33 = load i32, i32* @B300
store i32 %33, i32* %6
br label %52
34: ; preds = %15
%35 = load i32, i32* @B600
store i32 %35, i32* %6
br label %52
36: ; preds = %15
%37 = load i32, i32* @B1200
store i32 %37, i32* %6
br label %52
38: ; preds = %15
%39 = load i32, i32* @B1800
store i32 %39, i32* %6
br label %52
40: ; preds = %15
%41 = load i32, i32* @B2400
store i32 %41, i32* %6
br label %52
42: ; preds = %15
%43 = load i32, i32* @B4800
store i32 %43, i32* %6
br label %52
44: ; preds = %15
%45 = load i32, i32* @B9600
store i32 %45, i32* %6
br label %52
46: ; preds = %15
%47 = load i32, i32* @B19200
store i32 %47, i32* %6
br label %52
48: ; preds = %15
%49 = load i32, i32* @B38400
store i32 %49, i32* %6
br label %52
50: ; preds = %15
%51 = load i32, i32* @B38400
store i32 %51, i32* %6
store i32 1, i32* %7
br label %52
52: ; preds = %50, %48, %46, %44, %42, %40, %38, %36, %34, %32, %30, %28, %26, %24, %22, %20, %18
%53 = load i32, i32* %6
%54 = call i64 @cfsetispeed(%struct.termios* %8, i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %60, label %56
56: ; preds = %52
%57 = load i32, i32* %6
%58 = call i64 @cfsetospeed(%struct.termios* %8, i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56, %52
%61 = load i32, i32* @errno
store i32 %61, i32* %3
br label %73
62: ; preds = %56
%63 = load i32, i32* %4
%64 = load i32, i32* @TCSANOW
%65 = call i64 @tcsetattr(i32 %63, i32 %64, %struct.termios* %8)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %62
br label %68
68: ; preds = %67, %62
%69 = load i32, i32* %7
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %68
store i32 -1, i32* %3
br label %73
72: ; preds = %68
store i32 0, i32* %3
br label %73
73: ; preds = %72, %71, %60, %13
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@errno = global i32 0
@B0 = global i32 0
@B50 = global i32 0
@B75 = global i32 0
@B110 = global i32 0
@B134 = global i32 0
@B150 = global i32 0
@B200 = global i32 0
@B300 = global i32 0
@B600 = global i32 0
@B1200 = global i32 0
@B1800 = global i32 0
@B2400 = global i32 0
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@TCSANOW = global i32 0
define i32 @set_baud(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @memset(%struct.termios* %3, i32 0, i32 4)
%6 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = load i32, i32* @errno
br label %42
10: ; preds = %2
%11 = call i32 @msleep(i32 45)
switch i32 %1, label %27 [
i32 0, label %28
i32 50, label %12
i32 75, label %13
i32 110, label %14
i32 134, label %15
i32 150, label %16
i32 200, label %17
i32 300, label %18
i32 600, label %19
i32 1200, label %20
i32 1800, label %21
i32 2400, label %22
i32 4800, label %23
i32 9600, label %24
i32 19200, label %25
i32 38400, label %26
]
12: ; preds = %10
br label %28
13: ; preds = %10
br label %28
14: ; preds = %10
br label %28
15: ; preds = %10
br label %28
16: ; preds = %10
br label %28
17: ; preds = %10
br label %28
18: ; preds = %10
br label %28
19: ; preds = %10
br label %28
20: ; preds = %10
br label %28
21: ; preds = %10
br label %28
22: ; preds = %10
br label %28
23: ; preds = %10
br label %28
24: ; preds = %10
br label %28
25: ; preds = %10
br label %28
26: ; preds = %10
br label %28
27: ; preds = %10
br label %28
28: ; preds = %10, %27, %26, %25, %24, %23, %22, %21, %20, %19, %18, %17, %16, %15, %14, %13, %12
%29 = phi i32* [ @B38400, %27 ], [ @B38400, %26 ], [ @B19200, %25 ], [ @B9600, %24 ], [ @B4800, %23 ], [ @B2400, %22 ], [ @B1800, %21 ], [ @B1200, %20 ], [ @B600, %19 ], [ @B300, %18 ], [ @B200, %17 ], [ @B150, %16 ], [ @B134, %15 ], [ @B110, %14 ], [ @B75, %13 ], [ @B50, %12 ], [ @B0, %10 ]
%30 = phi i32 [ -1, %27 ], [ 0, %26 ], [ 0, %25 ], [ 0, %24 ], [ 0, %23 ], [ 0, %22 ], [ 0, %21 ], [ 0, %20 ], [ 0, %19 ], [ 0, %18 ], [ 0, %17 ], [ 0, %16 ], [ 0, %15 ], [ 0, %14 ], [ 0, %13 ], [ 0, %12 ], [ %1, %10 ]
%31 = load i32, i32* %29
%32 = call i64 @cfsetispeed(%struct.termios* %3, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i64 @cfsetospeed(%struct.termios* %3, i32 %31)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34, %28
%38 = load i32, i32* @errno
br label %42
39: ; preds = %34
%40 = load i32, i32* @TCSANOW
%41 = call i64 @tcsetattr(i32 %0, i32 %40, %struct.termios* %3)
br label %42
42: ; preds = %39, %37, %8
%43 = phi i32 [ %9, %8 ], [ %38, %37 ], [ %30, %39 ]
ret i32 %43
}