#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 c_cflag;
  int c_iflag;
  int *c_cc;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int HUPCL;
int IXANY;
int IXOFF;
int IXON;
int O_NDELAY;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int PARENB;
int PARODD;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ _convertBaudRate(int);
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char const *, int);
int /*<<< orphan*/ printf(char *, char const *, ...);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int openSerial(const char *port, unsigned int bits __attribute__((unused)),
               unsigned int stop __attribute__((unused)),
               char parity __attribute__((unused)), int baudrate, bool rtscts) {
  struct termios tty;
  printf("Opening serial port %s baudrate %d rtscts %d\n", port, baudrate,
         rtscts);
  speed_t speed = _convertBaudRate(baudrate);
  int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
  if (0 > fd) {
    printf("Error opening serial port %s\n", port);
    return fd;
  }
  memset(&tty, 0, sizeof(tty));
  tcgetattr(fd, &tty);
  cfmakeraw(&tty);
  cfsetispeed(&tty, speed);
  cfsetospeed(&tty, speed);
  tty.c_cflag &= ~(PARENB | PARODD);
  tty.c_cflag &= ~CSIZE;
  tty.c_cflag |= CS8;
  tty.c_cflag |= CLOCAL | CREAD;
  tty.c_cflag &= ~HUPCL;
  if (rtscts) {
    tty.c_cflag |= CRTSCTS;
  } else {
    tty.c_cflag &= ~CRTSCTS;
  }
  tty.c_iflag &= ~(IXON | IXOFF | IXANY);
  tty.c_cc[VMIN] = 1;
  tty.c_cc[VTIME] = 5;
  tcsetattr(fd, TCSANOW, &tty);
  tcflush(fd, TCIOFLUSH);
  return fd;
}


x86 assembly (clang, -O0):
<openSerial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %cl,%al
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
call   1070 <_convertBaudRate@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
mov    0x2d79(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x40(%rbp)
xor    %eax,%eax
cmp    -0x40(%rbp),%eax
jle    123a <openSerial+0x9a>
mov    -0x10(%rbp),%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1387 <openSerial+0x1e7>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    -0x40(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1090 <tcgetattr@plt>
lea    -0x38(%rbp),%rdi
call   1080 <cfmakeraw@plt>
mov    -0x3c(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   10c0 <cfsetispeed@plt>
mov    -0x3c(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    0x2d31(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c8e(%rip),%rax
mov    (%rax),%eax
mov    0x2ced(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c8e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12fb <openSerial+0x15b>
mov    0x2c62(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    130d <openSerial+0x16d>
mov    0x2c4e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cbc(%rip),%rax
mov    (%rax),%eax
mov    0x2cc3(%rip),%rcx
or     (%rcx),%eax
mov    0x2c62(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2c64(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2c17(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x40(%rbp),%edi
mov    0x2c5b(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   10a0 <tcsetattr@plt>
mov    -0x40(%rbp),%edi
mov    0x2bf6(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openSerial>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r9d,%r15d
mov    %r8d,%ebx
mov    %rdi,%rbp
lea    0xe44(%rip),%rdi
mov    %rbp,%rsi
mov    %r8d,%edx
mov    %r9d,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <_convertBaudRate@plt>
mov    0x2e0e(%rip),%rcx
mov    0x2d9f(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2d8c(%rip),%rcx
or     (%rcx),%esi
mov    %eax,%ebx
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
call   10b0 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
js     1316 <openSerial+0x176>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    %r12d,%edi
mov    %r14,%rsi
call   1090 <tcgetattr@plt>
mov    %r14,%rdi
call   1080 <cfmakeraw@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   10c0 <cfsetispeed@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <cfsetospeed@plt>
mov    0x2d7c(%rip),%rax
mov    0x2d65(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d3a(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
mov    0x2d3c(%rip),%rax
or     (%rax),%ecx
mov    0x2cd3(%rip),%rax
mov    0x2d34(%rip),%rdx
or     (%rax),%ecx
or     (%rdx),%ecx
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2cbc(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,%edx
not    %edx
and    %eax,%edx
or     %ecx,%eax
test   %r15d,%r15d
cmove  %edx,%eax
mov    %eax,(%rsp)
mov    0x2d22(%rip),%rax
mov    0x2d2b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2cc8(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x8(%rsp),%rax
mov    0x2ccc(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2c83(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   10a0 <tcsetattr@plt>
mov    0x2c66(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1040 <tcflush@plt>
jmp    1327 <openSerial+0x187>
lea    0xd11(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32* }
@.str = [46 x i8] c"Opening serial port %s baudrate %d rtscts %d\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [30 x i8] c"Error opening serial port %s\0A\00"
@PARENB = global i32 0
@PARODD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@HUPCL = global i32 0
@CRTSCTS = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@TCIOFLUSH = global i32 0
define i32 @openSerial(i8* %0, i32 %1, i32 %2, i8 signext %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.termios
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i8 %3, i8* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
%17 = load i8*, i8** %8
%18 = load i32, i32* %12
%19 = load i32, i32* %13
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18, i32 %19)
%21 = load i32, i32* %12
%22 = call i32 @_convertBaudRate(i32 %21)
store i32 %22, i32* %15
%23 = load i8*, i8** %8
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NOCTTY
%26 = or i32 %24, %25
%27 = load i32, i32* @O_NDELAY
%28 = or i32 %26, %27
%29 = load i32, i32* @O_NONBLOCK
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %23, i32 %30)
store i32 %31, i32* %16
%32 = load i32, i32* %16
%33 = icmp sgt i32 0, %32
br i1 %33, label %34, label %38
34: ; preds = %6
%35 = load i8*, i8** %8
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %16
store i32 %37, i32* %7
br label %112
38: ; preds = %6
%39 = call i32 @memset(%struct.termios* %14, i32 0, i32 16)
%40 = load i32, i32* %16
%41 = call i32 @tcgetattr(i32 %40, %struct.termios* %14)
%42 = call i32 @cfmakeraw(%struct.termios* %14)
%43 = load i32, i32* %15
%44 = call i32 @cfsetispeed(%struct.termios* %14, i32 %43)
%45 = load i32, i32* %15
%46 = call i32 @cfsetospeed(%struct.termios* %14, i32 %45)
%47 = load i32, i32* @PARENB
%48 = load i32, i32* @PARODD
%49 = or i32 %47, %48
%50 = xor i32 %49, -1
%51 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 0
%52 = load i32, i32* %51
%53 = and i32 %52, %50
store i32 %53, i32* %51
%54 = load i32, i32* @CSIZE
%55 = xor i32 %54, -1
%56 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 0
%57 = load i32, i32* %56
%58 = and i32 %57, %55
store i32 %58, i32* %56
%59 = load i32, i32* @CS8
%60 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 0
%61 = load i32, i32* %60
%62 = or i32 %61, %59
store i32 %62, i32* %60
%63 = load i32, i32* @CLOCAL
%64 = load i32, i32* @CREAD
%65 = or i32 %63, %64
%66 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 0
%67 = load i32, i32* %66
%68 = or i32 %67, %65
store i32 %68, i32* %66
%69 = load i32, i32* @HUPCL
%70 = xor i32 %69, -1
%71 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 0
%72 = load i32, i32* %71
%73 = and i32 %72, %70
store i32 %73, i32* %71
%74 = load i32, i32* %13
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %38
%77 = load i32, i32* @CRTSCTS
%78 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 0
%79 = load i32, i32* %78
%80 = or i32 %79, %77
store i32 %80, i32* %78
br label %87
81: ; preds = %38
%82 = load i32, i32* @CRTSCTS
%83 = xor i32 %82, -1
%84 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 0
%85 = load i32, i32* %84
%86 = and i32 %85, %83
store i32 %86, i32* %84
br label %87
87: ; preds = %81, %76
%88 = load i32, i32* @IXON
%89 = load i32, i32* @IXOFF
%90 = or i32 %88, %89
%91 = load i32, i32* @IXANY
%92 = or i32 %90, %91
%93 = xor i32 %92, -1
%94 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 1
%95 = load i32, i32* %94
%96 = and i32 %95, %93
store i32 %96, i32* %94
%97 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 2
%98 = load i32*, i32** %97
%99 = load i64, i64* @VMIN
%100 = getelementptr inbounds i32, i32* %98, i64 %99
store i32 1, i32* %100
%101 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 2
%102 = load i32*, i32** %101
%103 = load i64, i64* @VTIME
%104 = getelementptr inbounds i32, i32* %102, i64 %103
store i32 5, i32* %104
%105 = load i32, i32* %16
%106 = load i32, i32* @TCSANOW
%107 = call i32 @tcsetattr(i32 %105, i32 %106, %struct.termios* %14)
%108 = load i32, i32* %16
%109 = load i32, i32* @TCIOFLUSH
%110 = call i32 @tcflush(i32 %108, i32 %109)
%111 = load i32, i32* %16
store i32 %111, i32* %7
br label %112
112: ; preds = %87, %34
%113 = load i32, i32* %7
ret i32 %113
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32* }
@.str = [46 x i8] c"Opening serial port %s baudrate %d rtscts %d\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [30 x i8] c"Error opening serial port %s\0A\00"
@PARENB = global i32 0
@PARODD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@HUPCL = global i32 0
@CRTSCTS = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@TCIOFLUSH = global i32 0
define i32 @openSerial(i8* %0, i32 %1, i32 %2, i8 signext %3, i32 %4, i32 %5) {
%7 = alloca %struct.termios
%8 = bitcast %struct.termios* %7 to i8*
%9 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %4, i32 %5)
%10 = tail call i32 @_convertBaudRate(i32 %4)
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %12, %11
%14 = load i32, i32* @O_NDELAY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %0, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %6
%21 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %71
22: ; preds = %6
%23 = call i32 @memset(%struct.termios* %7, i32 0, i32 16)
%24 = call i32 @tcgetattr(i32 %18, %struct.termios* %7)
%25 = call i32 @cfmakeraw(%struct.termios* %7)
%26 = call i32 @cfsetispeed(%struct.termios* %7, i32 %10)
%27 = call i32 @cfsetospeed(%struct.termios* %7, i32 %10)
%28 = load i32, i32* @PARENB
%29 = load i32, i32* @PARODD
%30 = or i32 %29, %28
%31 = getelementptr inbounds %struct.termios, %struct.termios* %7, i64 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @CSIZE
%34 = or i32 %30, %33
%35 = xor i32 %34, -1
%36 = and i32 %32, %35
%37 = load i32, i32* @CS8
%38 = or i32 %37, %36
%39 = load i32, i32* @CLOCAL
%40 = load i32, i32* @CREAD
%41 = or i32 %39, %38
%42 = or i32 %41, %40
%43 = load i32, i32* @HUPCL
%44 = xor i32 %43, -1
%45 = and i32 %42, %44
%46 = icmp eq i32 %5, 0
%47 = load i32, i32* @CRTSCTS
%48 = xor i32 %47, -1
%49 = and i32 %45, %48
%50 = or i32 %47, %45
%51 = select i1 %46, i32 %49, i32 %50
store i32 %51, i32* %31
%52 = load i32, i32* @IXON
%53 = load i32, i32* @IXOFF
%54 = or i32 %53, %52
%55 = load i32, i32* @IXANY
%56 = or i32 %54, %55
%57 = xor i32 %56, -1
%58 = getelementptr inbounds %struct.termios, %struct.termios* %7, i64 0, i32 1
%59 = load i32, i32* %58
%60 = and i32 %59, %57
store i32 %60, i32* %58
%61 = getelementptr inbounds %struct.termios, %struct.termios* %7, i64 0, i32 2
%62 = load i32*, i32** %61
%63 = load i64, i64* @VMIN
%64 = getelementptr inbounds i32, i32* %62, i64 %63
store i32 1, i32* %64
%65 = load i64, i64* @VTIME
%66 = getelementptr inbounds i32, i32* %62, i64 %65
store i32 5, i32* %66
%67 = load i32, i32* @TCSANOW
%68 = call i32 @tcsetattr(i32 %18, i32 %67, %struct.termios* %7)
%69 = load i32, i32* @TCIOFLUSH
%70 = call i32 @tcflush(i32 %18, i32 %69)
br label %71
71: ; preds = %22, %20
ret i32 %18
}