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

/* Variables and functions */
int B115200;
int B1200;
int B19200;
int B230400;
int B2400;
int B300;
int B38400;
int B4800;
int B57600;
int B600;
int B9600;
int CLOCAL;
int CREAD;
int CS8;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int open(char const *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int uart_open(char const *device, int bps, struct termios *saveattr) {
  int fd;
  int speed;
  struct termios attr;
  fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd < 0) {
    return -1;
  }
  tcgetattr(fd, &attr);
  *saveattr = attr;
  speed = B115200;
  switch (bps) {
  case 300:
    speed = B300;
    break;
  case 600:
    speed = B600;
    break;
  case 1200:
    speed = B1200;
    break;
  case 2400:
    speed = B2400;
    break;
  case 4800:
    speed = B4800;
    break;
  case 9600:
    speed = B9600;
    break;
  case 19200:
    speed = B19200;
    break;
  case 38400:
    speed = B38400;
    break;
  case 57600:
    speed = B57600;
    break;
  case 115200:
    speed = B115200;
    break;
  case 230400:
    speed = B230400;
    break;
  }
  cfsetispeed(&attr, speed);
  cfsetospeed(&attr, speed);
  cfmakeraw(&attr);
  attr.c_cflag |= CS8 | CLOCAL | CREAD;
  attr.c_iflag = 0;
  attr.c_oflag = 0;
  attr.c_lflag = 0;
  attr.c_cc[VMIN] = 1;
  attr.c_cc[VTIME] = 0;
  tcsetattr(fd, TCSANOW, &attr);
  return fd;
}


x86 assembly (clang, -O0):
<uart_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b0 <uart_open+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    140c <uart_open+0x2ac>
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,0x20(%rax)
movups -0x50(%rbp),%xmm0
movups -0x40(%rbp),%xmm1
movups %xmm1,0x10(%rax)
movups %xmm0,(%rax)
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
sub    $0x12c,%eax
je     12b7 <uart_open+0x157>
jmp    11f9 <uart_open+0x99>
mov    -0x54(%rbp),%eax
sub    $0x258,%eax
je     12c8 <uart_open+0x168>
jmp    120c <uart_open+0xac>
mov    -0x54(%rbp),%eax
sub    $0x4b0,%eax
je     12d9 <uart_open+0x179>
jmp    121f <uart_open+0xbf>
mov    -0x54(%rbp),%eax
sub    $0x960,%eax
je     12ea <uart_open+0x18a>
jmp    1232 <uart_open+0xd2>
mov    -0x54(%rbp),%eax
sub    $0x12c0,%eax
je     12fb <uart_open+0x19b>
jmp    1245 <uart_open+0xe5>
mov    -0x54(%rbp),%eax
sub    $0x2580,%eax
je     130c <uart_open+0x1ac>
jmp    1258 <uart_open+0xf8>
mov    -0x54(%rbp),%eax
sub    $0x4b00,%eax
je     131d <uart_open+0x1bd>
jmp    126b <uart_open+0x10b>
mov    -0x54(%rbp),%eax
sub    $0x9600,%eax
je     132e <uart_open+0x1ce>
jmp    127e <uart_open+0x11e>
mov    -0x54(%rbp),%eax
sub    $0xe100,%eax
je     133f <uart_open+0x1df>
jmp    1291 <uart_open+0x131>
mov    -0x54(%rbp),%eax
sub    $0x1c200,%eax
je     1350 <uart_open+0x1f0>
jmp    12a4 <uart_open+0x144>
mov    -0x54(%rbp),%eax
sub    $0x38400,%eax
je     1361 <uart_open+0x201>
jmp    136d <uart_open+0x20d>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2c90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2cbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2ca5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2ccc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2c0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2c4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2c11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    136d <uart_open+0x20d>
mov    0x2be8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    -0x50(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x28(%rbp),%esi
lea    -0x50(%rbp),%rdi
call   1030 <cfsetospeed@plt>
lea    -0x50(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    0x2c0b(%rip),%rax
mov    (%rax),%eax
mov    0x2baa(%rip),%rcx
or     (%rcx),%eax
mov    0x2c01(%rip),%rcx
or     (%rcx),%eax
or     -0x50(%rbp),%eax
mov    %eax,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x48(%rbp),%rax
mov    0x2bc6(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x48(%rbp),%rax
mov    0x2b79(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x24(%rbp),%edi
mov    0x2bd5(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e71(%rip),%rcx
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rdx,%rbx
mov    %esi,%ebp
mov    0x2e52(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1070 <open@plt>
test   %eax,%eax
js     1203 <uart_open+0xa3>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1050 <tcgetattr@plt>
movups 0x8(%rsp),%xmm0
movups 0x18(%rsp),%xmm1
movups %xmm0,(%rbx)
movups %xmm1,0x10(%rbx)
mov    0x28(%rsp),%rax
mov    %rax,0x20(%rbx)
mov    0x2da5(%rip),%rax
cmp    $0x257f,%ebp
jg     120e <uart_open+0xae>
cmp    $0x4af,%ebp
jle    1237 <uart_open+0xd7>
cmp    $0x4b0,%ebp
je     1269 <uart_open+0x109>
cmp    $0x960,%ebp
je     1272 <uart_open+0x112>
cmp    $0x12c0,%ebp
jne    129d <uart_open+0x13d>
mov    0x2dc2(%rip),%rax
jmp    129d <uart_open+0x13d>
mov    $0xffffffff,%r14d
jmp    132b <uart_open+0x1cb>
cmp    $0x95ff,%ebp
jle    1250 <uart_open+0xf0>
cmp    $0x9600,%ebp
je     127b <uart_open+0x11b>
cmp    $0xe100,%ebp
je     1284 <uart_open+0x124>
cmp    $0x38400,%ebp
jne    129d <uart_open+0x13d>
mov    0x2d1b(%rip),%rax
jmp    129d <uart_open+0x13d>
cmp    $0x12c,%ebp
je     128d <uart_open+0x12d>
cmp    $0x258,%ebp
jne    129d <uart_open+0x13d>
mov    0x2d7a(%rip),%rax
jmp    129d <uart_open+0x13d>
cmp    $0x2580,%ebp
je     1296 <uart_open+0x136>
cmp    $0x4b00,%ebp
jne    129d <uart_open+0x13d>
mov    0x2d89(%rip),%rax
jmp    129d <uart_open+0x13d>
mov    0x2d00(%rip),%rax
jmp    129d <uart_open+0x13d>
mov    0x2cff(%rip),%rax
jmp    129d <uart_open+0x13d>
mov    0x2cbe(%rip),%rax
jmp    129d <uart_open+0x13d>
mov    0x2d05(%rip),%rax
jmp    129d <uart_open+0x13d>
mov    0x2d1c(%rip),%rax
jmp    129d <uart_open+0x13d>
mov    0x2d1b(%rip),%rax
mov    (%rax),%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
call   1080 <cfsetispeed@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <cfsetospeed@plt>
mov    %rbp,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2cd9(%rip),%rax
mov    0x2c7a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2ccf(%rip),%rax
or     (%rax),%ecx
or     %ecx,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x10(%rsp),%rax
mov    0x2c9c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2c53(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2cb2(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbp,%rdx
call   1060 <tcsetattr@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@B115200 = global i32 0
@B300 = global i32 0
@B600 = global i32 0
@B1200 = global i32 0
@B2400 = global i32 0
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B230400 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @uart_open(i8* %0, i32 %1, %struct.termios* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.termios*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.termios
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.termios* %2, %struct.termios** %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_NOCTTY
%14 = or i32 %12, %13
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %11, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %80
21: ; preds = %3
%22 = load i32, i32* %8
%23 = call i32 @tcgetattr(i32 %22, %struct.termios* %10)
%24 = load %struct.termios*, %struct.termios** %7
%25 = bitcast %struct.termios* %24 to i8*
%26 = bitcast %struct.termios* %10 to i8*
%27 = load i32, i32* @B115200
store i32 %27, i32* %9
%28 = load i32, i32* %6
switch i32 %28, label %51 [
i32 300, label %29
i32 600, label %31
i32 1200, label %33
i32 2400, label %35
i32 4800, label %37
i32 9600, label %39
i32 19200, label %41
i32 38400, label %43
i32 57600, label %45
i32 115200, label %47
i32 230400, label %49
]
29: ; preds = %21
%30 = load i32, i32* @B300
store i32 %30, i32* %9
br label %51
31: ; preds = %21
%32 = load i32, i32* @B600
store i32 %32, i32* %9
br label %51
33: ; preds = %21
%34 = load i32, i32* @B1200
store i32 %34, i32* %9
br label %51
35: ; preds = %21
%36 = load i32, i32* @B2400
store i32 %36, i32* %9
br label %51
37: ; preds = %21
%38 = load i32, i32* @B4800
store i32 %38, i32* %9
br label %51
39: ; preds = %21
%40 = load i32, i32* @B9600
store i32 %40, i32* %9
br label %51
41: ; preds = %21
%42 = load i32, i32* @B19200
store i32 %42, i32* %9
br label %51
43: ; preds = %21
%44 = load i32, i32* @B38400
store i32 %44, i32* %9
br label %51
45: ; preds = %21
%46 = load i32, i32* @B57600
store i32 %46, i32* %9
br label %51
47: ; preds = %21
%48 = load i32, i32* @B115200
store i32 %48, i32* %9
br label %51
49: ; preds = %21
%50 = load i32, i32* @B230400
store i32 %50, i32* %9
br label %51
51: ; preds = %21, %49, %47, %45, %43, %41, %39, %37, %35, %33, %31, %29
%52 = load i32, i32* %9
%53 = call i32 @cfsetispeed(%struct.termios* %10, i32 %52)
%54 = load i32, i32* %9
%55 = call i32 @cfsetospeed(%struct.termios* %10, i32 %54)
%56 = call i32 @cfmakeraw(%struct.termios* %10)
%57 = load i32, i32* @CS8
%58 = load i32, i32* @CLOCAL
%59 = or i32 %57, %58
%60 = load i32, i32* @CREAD
%61 = or i32 %59, %60
%62 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 0
%63 = load i32, i32* %62
%64 = or i32 %63, %61
store i32 %64, i32* %62
%65 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 4
store i64 0, i64* %65
%66 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 3
store i64 0, i64* %66
%67 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 2
store i64 0, i64* %67
%68 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 1
%69 = load i32*, i32** %68
%70 = load i64, i64* @VMIN
%71 = getelementptr inbounds i32, i32* %69, i64 %70
store i32 1, i32* %71
%72 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 1
%73 = load i32*, i32** %72
%74 = load i64, i64* @VTIME
%75 = getelementptr inbounds i32, i32* %73, i64 %74
store i32 0, i32* %75
%76 = load i32, i32* %8
%77 = load i32, i32* @TCSANOW
%78 = call i32 @tcsetattr(i32 %76, i32 %77, %struct.termios* %10)
%79 = load i32, i32* %8
store i32 %79, i32* %4
br label %80
80: ; preds = %51, %20
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@B115200 = global i32 0
@B300 = global i32 0
@B600 = global i32 0
@B1200 = global i32 0
@B2400 = global i32 0
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B230400 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @uart_open(i8* nocapture %0, i32 %1, %struct.termios* nocapture writeonly %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %50, label %13
13: ; preds = %3
%14 = call i32 @tcgetattr(i32 %11, %struct.termios* %4)
%15 = bitcast %struct.termios* %2 to i8*
switch i32 %1, label %26 [
i32 300, label %16
i32 600, label %17
i32 1200, label %18
i32 2400, label %19
i32 4800, label %20
i32 9600, label %21
i32 19200, label %22
i32 38400, label %23
i32 57600, label %24
i32 230400, label %25
]
16: ; preds = %13
br label %26
17: ; preds = %13
br label %26
18: ; preds = %13
br label %26
19: ; preds = %13
br label %26
20: ; preds = %13
br label %26
21: ; preds = %13
br label %26
22: ; preds = %13
br label %26
23: ; preds = %13
br label %26
24: ; preds = %13
br label %26
25: ; preds = %13
br label %26
26: ; preds = %13, %25, %24, %23, %22, %21, %20, %19, %18, %17, %16
%27 = phi i32* [ @B230400, %25 ], [ @B57600, %24 ], [ @B38400, %23 ], [ @B19200, %22 ], [ @B9600, %21 ], [ @B4800, %20 ], [ @B2400, %19 ], [ @B1200, %18 ], [ @B600, %17 ], [ @B300, %16 ], [ @B115200, %13 ]
%28 = load i32, i32* %27
%29 = call i32 @cfsetispeed(%struct.termios* %4, i32 %28)
%30 = call i32 @cfsetospeed(%struct.termios* %4, i32 %28)
%31 = call i32 @cfmakeraw(%struct.termios* %4)
%32 = load i32, i32* @CS8
%33 = load i32, i32* @CLOCAL
%34 = or i32 %33, %32
%35 = load i32, i32* @CREAD
%36 = or i32 %34, %35
%37 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%38 = load i32, i32* %37
%39 = or i32 %36, %38
store i32 %39, i32* %37
%40 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 2
%41 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%42 = bitcast i64* %40 to i8*
%43 = load i32*, i32** %41
%44 = load i64, i64* @VMIN
%45 = getelementptr inbounds i32, i32* %43, i64 %44
store i32 1, i32* %45
%46 = load i64, i64* @VTIME
%47 = getelementptr inbounds i32, i32* %43, i64 %46
store i32 0, i32* %47
%48 = load i32, i32* @TCSANOW
%49 = call i32 @tcsetattr(i32 %11, i32 %48, %struct.termios* %4)
br label %50
50: ; preds = %3, %26
%51 = phi i32 [ %11, %26 ], [ -1, %3 ]
ret i32 %51
}