#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_oflag;
  int /*<<< orphan*/ c_iflag;
};
typedef int /*<<< orphan*/ newtios;

/* Variables and functions */
int B115200;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int mwsp_connect(char *port) {
  struct termios newtios;
  int fd = open(port, O_RDWR | O_NOCTTY);
  if (fd < 0) {
    return -1;
  }
  memset(&newtios, 0, sizeof(newtios));
  newtios.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
  newtios.c_iflag = IGNPAR;
  newtios.c_oflag = 0;
  newtios.c_cc[VMIN] = 0;
  newtios.c_cc[VTIME] = 1;
  tcflush(fd, TCIOFLUSH);
  tcsetattr(fd, TCSANOW, &newtios);
  return fd;
}


x86 assembly (clang, -O0):
<mwsp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1180 <mwsp_connect+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <mwsp_connect+0xea>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    0x2de8(%rip),%rcx
or     (%rcx),%eax
mov    0x2e27(%rip),%rcx
or     (%rcx),%eax
mov    0x2dce(%rip),%rcx
or     (%rcx),%eax
mov    0x2e1d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2dac(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x34(%rbp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x34(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mwsp_connect>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e9a(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     120f <mwsp_connect+0xcf>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e25(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e3b(%rip),%rax
or     (%rax),%ecx
mov    0x2de2(%rip),%rax
or     (%rax),%ecx
mov    0x2e31(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%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    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
jmp    1214 <mwsp_connect+0xd4>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B115200 = global i32 0
@CRTSCTS = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @mwsp_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.termios
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %44
14: ; preds = %1
%15 = call i32 @memset(%struct.termios* %4, i32 0, i32 32)
%16 = load i32, i32* @B115200
%17 = load i32, i32* @CRTSCTS
%18 = or i32 %16, %17
%19 = load i32, i32* @CS8
%20 = or i32 %18, %19
%21 = load i32, i32* @CLOCAL
%22 = or i32 %20, %21
%23 = load i32, i32* @CREAD
%24 = or i32 %22, %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @IGNPAR
%27 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 3
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
store i64 0, i64* %28
%29 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%30 = load i32*, i32** %29
%31 = load i64, i64* @VMIN
%32 = getelementptr inbounds i32, i32* %30, i64 %31
store i32 0, i32* %32
%33 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%34 = load i32*, i32** %33
%35 = load i64, i64* @VTIME
%36 = getelementptr inbounds i32, i32* %34, i64 %35
store i32 1, i32* %36
%37 = load i32, i32* %5
%38 = load i32, i32* @TCIOFLUSH
%39 = call i32 @tcflush(i32 %37, i32 %38)
%40 = load i32, i32* %5
%41 = load i32, i32* @TCSANOW
%42 = call i32 @tcsetattr(i32 %40, i32 %41, %struct.termios* %4)
%43 = load i32, i32* %5
store i32 %43, i32* %2
br label %44
44: ; preds = %14, %13
%45 = load i32, i32* %2
ret i32 %45
}

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