#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*/ tio;
struct termios {
  int c_cflag;
  int c_ispeed;
  int c_ospeed;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int B57600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_RDWR;
int PARENB;
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*/);
scalar_t__ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int openserial(const char *device, int speed) {
  struct termios tio;
  int fd;
  fd = open(device, O_RDWR | O_NOCTTY);
  if (fd < 0)
    return -1;
  if (tcgetattr(fd, &tio))
    memset(&tio, 0, sizeof(tio));
  tio.c_cflag = B57600 | CS8 | CLOCAL | CREAD | PARENB;
  tio.c_ispeed = B57600;
  tio.c_ospeed = B57600;
  tio.c_iflag = IGNPAR;
  tio.c_oflag = 0;
  tio.c_lflag = 0;
  tio.c_cc[VTIME] = 1;
  tio.c_cc[VMIN] = 0;
  tcsetattr(fd, TCSANOW, &tio);
  tcflush(fd, TCIFLUSH);
  tio.c_cflag = speed | CS8 | CLOCAL | CREAD | PARENB;
  tio.c_ispeed = speed;
  tio.c_ospeed = speed;
  tcsetattr(fd, TCSANOW, &tio);
  tcflush(fd, TCIFLUSH);
  return fd;
}


x86 assembly (clang, -O0):
<openserial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1193 <openserial+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <openserial+0x17f>
mov    -0x4c(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     11b9 <openserial+0x69>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    0x2dff(%rip),%rcx
or     (%rcx),%eax
mov    0x2dae(%rip),%rcx
or     (%rcx),%eax
mov    0x2df5(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfc(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d70(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
mov    0x2d83(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x4c(%rbp),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x4c(%rbp),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x14(%rbp),%eax
mov    0x2d51(%rip),%rcx
or     (%rcx),%eax
mov    0x2d00(%rip),%rcx
or     (%rcx),%eax
mov    0x2d47(%rip),%rcx
or     (%rcx),%eax
mov    0x2d4e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x4c(%rbp),%edi
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x4c(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openserial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    0x2e81(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     128e <openserial+0x13e>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
je     11a3 <openserial+0x53>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%ecx
or     %eax,%ecx
mov    0x2dc2(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e09(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e10(%rip),%r15
or     (%r15),%ecx
mov    %ecx,0x8(%rsp)
mov    %eax,0xc(%rsp)
mov    %eax,0x10(%rsp)
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
mov    0x18(%rsp),%rax
mov    0x2d98(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2daf(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2db6(%rip),%r12
mov    (%r12),%esi
lea    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    0x2d6c(%rip),%r13
mov    0x0(%r13),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
or     %ebp,%eax
mov    0x2d27(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6e(%rip),%rcx
or     (%rcx),%eax
or     (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,0xc(%rsp)
mov    %ebp,0x10(%rsp)
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    0x0(%r13),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
jmp    1293 <openserial+0x143>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32*, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B57600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@TCIFLUSH = global i32 0
define i32 @openserial(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %54, label %10
10: ; preds = %2
%11 = call i64 @tcgetattr(i32 %8, %struct.termios* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = call i32 @memset(%struct.termios* %3, i32 0, i32 48)
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* @B57600
%17 = load i32, i32* @CS8
%18 = or i32 %17, %16
%19 = load i32, i32* @CLOCAL
%20 = or i32 %18, %19
%21 = load i32, i32* @CREAD
%22 = or i32 %20, %21
%23 = load i32, i32* @PARENB
%24 = or i32 %22, %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
store i32 %16, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
store i32 %16, i32* %27
%28 = load i32, i32* @IGNPAR
%29 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 6
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 4
%31 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%32 = bitcast i64* %30 to i8*
%33 = load i32*, i32** %31
%34 = load i64, i64* @VTIME
%35 = getelementptr inbounds i32, i32* %33, i64 %34
store i32 1, i32* %35
%36 = load i64, i64* @VMIN
%37 = getelementptr inbounds i32, i32* %33, i64 %36
store i32 0, i32* %37
%38 = load i32, i32* @TCSANOW
%39 = call i32 @tcsetattr(i32 %8, i32 %38, %struct.termios* %3)
%40 = load i32, i32* @TCIFLUSH
%41 = call i32 @tcflush(i32 %8, i32 %40)
%42 = load i32, i32* @CS8
%43 = or i32 %42, %1
%44 = load i32, i32* @CLOCAL
%45 = or i32 %43, %44
%46 = load i32, i32* @CREAD
%47 = or i32 %45, %46
%48 = load i32, i32* @PARENB
%49 = or i32 %47, %48
store i32 %49, i32* %25
store i32 %1, i32* %26
store i32 %1, i32* %27
%50 = load i32, i32* @TCSANOW
%51 = call i32 @tcsetattr(i32 %8, i32 %50, %struct.termios* %3)
%52 = load i32, i32* @TCIFLUSH
%53 = call i32 @tcflush(i32 %8, i32 %52)
br label %54
54: ; preds = %2, %15
%55 = phi i32 [ %8, %15 ], [ -1, %2 ]
ret i32 %55
}