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

/* Variables and functions */
unsigned int CLOCAL;
unsigned int CREAD;
unsigned int CS8;
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ECHOE;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char const *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int openSerialPort(const char *device, unsigned int baud) {
  int fd;
  struct termios newtio;
  memset(&newtio, 0, sizeof(newtio));
  if ((fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
    return -1;
  } else {
    newtio.c_cflag = baud | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR;
    newtio.c_oflag = 0;
    newtio.c_lflag = 0;
    newtio.c_lflag &= ~(ICANON);
    newtio.c_lflag &= ~(ECHO);
    newtio.c_lflag &= ~(ECHOE);
    newtio.c_cc[VMIN] = 1;
    newtio.c_cc[VTIME] = 1;
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &newtio);
  }
  return fd;
}


x86 assembly (clang, -O0):
<openSerialPort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
or     (%rax),%esi
mov    0x2e64(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    119b <openSerialPort+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <openSerialPort+0x123>
mov    -0x14(%rbp),%eax
mov    0x2e13(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc2(%rip),%rcx
or     (%rcx),%eax
mov    0x2e09(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d98(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
mov    0x2d63(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x18(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openSerialPort>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e87(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     120f <openSerialPort+0xcf>
mov    %eax,%ebx
mov    0x2e3a(%rip),%rax
or     (%rax),%ebp
mov    0x2df1(%rip),%rax
or     (%rax),%ebp
mov    0x2e30(%rip),%rax
or     (%rax),%ebp
mov    %ebp,(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0x0,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2df1(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <tcsetattr@plt>
jmp    1214 <openSerialPort+0xd4>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i32, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @openSerialPort(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i32 @memset(%struct.termios* %7, i32 0, i32 40)
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %9, i32 %14)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %62
18: ; preds = %2
%19 = load i32, i32* %5
%20 = load i32, i32* @CS8
%21 = or i32 %19, %20
%22 = load i32, i32* @CLOCAL
%23 = or i32 %21, %22
%24 = load i32, i32* @CREAD
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @IGNPAR
%28 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 4
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
store i32 0, i32* %30
%31 = load i32, i32* @ICANON
%32 = xor i32 %31, -1
%33 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%34 = load i32, i32* %33
%35 = and i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* @ECHO
%37 = xor i32 %36, -1
%38 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%39 = load i32, i32* %38
%40 = and i32 %39, %37
store i32 %40, i32* %38
%41 = load i32, i32* @ECHOE
%42 = xor i32 %41, -1
%43 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%44 = load i32, i32* %43
%45 = and i32 %44, %42
store i32 %45, i32* %43
%46 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%47 = load i32*, i32** %46
%48 = load i64, i64* @VMIN
%49 = getelementptr inbounds i32, i32* %47, i64 %48
store i32 1, i32* %49
%50 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%51 = load i32*, i32** %50
%52 = load i64, i64* @VTIME
%53 = getelementptr inbounds i32, i32* %51, i64 %52
store i32 1, i32* %53
%54 = load i32, i32* %6
%55 = load i32, i32* @TCIFLUSH
%56 = call i32 @tcflush(i32 %54, i32 %55)
%57 = load i32, i32* %6
%58 = load i32, i32* @TCSANOW
%59 = call i32 @tcsetattr(i32 %57, i32 %58, %struct.termios* %7)
br label %60
60: ; preds = %18
%61 = load i32, i32* %6
store i32 %61, i32* %3
br label %62
62: ; preds = %60, %17
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i32, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @openSerialPort(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @memset(%struct.termios* %3, i32 0, i32 40)
%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 = call i32 @open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %35, label %13
13: ; preds = %2
%14 = load i32, i32* @CS8
%15 = or i32 %14, %1
%16 = load i32, i32* @CLOCAL
%17 = or i32 %15, %16
%18 = load i32, i32* @CREAD
%19 = or i32 %17, %18
%20 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @IGNPAR
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 4
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
store i32 0, i32* %24
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%26 = load i32*, i32** %25
%27 = load i64, i64* @VMIN
%28 = getelementptr inbounds i32, i32* %26, i64 %27
store i32 1, i32* %28
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %26, i64 %29
store i32 1, i32* %30
%31 = load i32, i32* @TCIFLUSH
%32 = call i32 @tcflush(i32 %11, i32 %31)
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %11, i32 %33, %struct.termios* %3)
br label %35
35: ; preds = %2, %13
%36 = phi i32 [ %11, %13 ], [ -1, %2 ]
ret i32 %36
}