#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_iflag;
  int *c_cc;
  scalar_t__ c_oflag;
  scalar_t__ c_lflag;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int ICRNL;
int IGNBRK;
int INLCR;
int IXANY;
int IXOFF;
int IXON;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int PARENB;
int PARODD;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
scalar_t__ cfsetispeed(struct termios *, int /*<<< orphan*/);
scalar_t__ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char const *, int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_open(const char *device, speed_t speed) {
  int fd = open(device, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd > 0) {
    struct termios tty;
    memset(&tty, 0, sizeof tty);
    if (tcgetattr(fd, &tty) != 0) {
      return -1;
    }
    if (cfsetospeed(&tty, speed) != 0) {
      return -1;
    }
    if (cfsetispeed(&tty, speed) != 0) {
      return -1;
    }
    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
    tty.c_iflag &= ~IGNBRK;
    tty.c_iflag &= ~INLCR;
    tty.c_iflag &= ~ICRNL;
    tty.c_lflag = 0;
    tty.c_oflag = 0;
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 5;
    tty.c_iflag &= ~(IXON | IXOFF | IXANY);
    tty.c_cflag |= (CLOCAL | CREAD);
    tty.c_cflag &= ~(PARENB | PARODD);
    tty.c_cflag &= ~CSTOPB;
    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
      return -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
or     (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    133c <serial_open+0x1dc>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     11d2 <serial_open+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <serial_open+0x1e2>
mov    -0x14(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
cmp    $0x0,%rax
je     11f4 <serial_open+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <serial_open+0x1e2>
mov    -0x14(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1080 <cfsetispeed@plt>
cmp    $0x0,%rax
je     1216 <serial_open+0xb6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <serial_open+0x1e2>
mov    -0x38(%rbp),%eax
mov    0x2d68(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2d6a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d0c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2cc7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
mov    0x2d15(%rip),%rcx
or     (%rcx),%eax
mov    0x2cbc(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2c7a(%rip),%rax
mov    (%rax),%eax
mov    0x2cc9(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cd2(%rip),%rax
mov    (%rax),%eax
mov    0x2cb9(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c4f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2c9a(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     1337 <serial_open+0x1d7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <serial_open+0x1e2>
jmp    133c <serial_open+0x1dc>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e79(%rip),%rcx
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%r14d
mov    0x2e4c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    12c4 <serial_open+0x164>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <tcgetattr@plt>
test   %rax,%rax
jne    11d8 <serial_open+0x78>
mov    %rsp,%rdi
mov    %r14d,%esi
call   1030 <cfsetospeed@plt>
test   %rax,%rax
jne    11d8 <serial_open+0x78>
mov    %rsp,%rdi
mov    %r14d,%esi
call   1080 <cfsetispeed@plt>
test   %rax,%rax
je     11e2 <serial_open+0x82>
mov    $0xffffffff,%eax
jmp    12c6 <serial_open+0x166>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
not    %eax
and    (%rsp),%eax
mov    0x2da1(%rip),%rcx
or     (%rcx),%eax
mov    0x2d68(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dcf(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,(%rsp)
mov    0x2dcb(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2d5f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d1e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2d6d(%rip),%rax
mov    0x2d6e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d13(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2cd4(%rip),%rax
mov    0x2d25(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     (%rsp),%ecx
mov    0x2d2f(%rip),%rax
mov    0x2d18(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    0x2cb5(%rip),%rax
or     (%rax),%edx
not    %edx
and    %ecx,%edx
mov    %edx,(%rsp)
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <tcsetattr@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    12c6 <serial_open+0x166>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@IGNBRK = global i32 0
@INLCR = global i32 0
@ICRNL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@CSTOPB = global i32 0
@TCSANOW = global i32 0
define i32 @serial_open(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 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_SYNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %100
17: ; preds = %2
%18 = call i32 @memset(%struct.termios* %7, i32 0, i32 32)
%19 = load i32, i32* %6
%20 = call i64 @tcgetattr(i32 %19, %struct.termios* %7)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %3
br label %102
23: ; preds = %17
%24 = load i32, i32* %5
%25 = call i64 @cfsetospeed(%struct.termios* %7, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 -1, i32* %3
br label %102
28: ; preds = %23
%29 = load i32, i32* %5
%30 = call i64 @cfsetispeed(%struct.termios* %7, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %3
br label %102
33: ; preds = %28
%34 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @CSIZE
%37 = xor i32 %36, -1
%38 = and i32 %35, %37
%39 = load i32, i32* @CS8
%40 = or i32 %38, %39
%41 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* @IGNBRK
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* @INLCR
%48 = xor i32 %47, -1
%49 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%50 = load i32, i32* %49
%51 = and i32 %50, %48
store i32 %51, i32* %49
%52 = load i32, i32* @ICRNL
%53 = xor i32 %52, -1
%54 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%55 = load i32, i32* %54
%56 = and i32 %55, %53
store i32 %56, i32* %54
%57 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 4
store i64 0, i64* %57
%58 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
store i64 0, i64* %58
%59 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%60 = load i32*, i32** %59
%61 = load i64, i64* @VMIN
%62 = getelementptr inbounds i32, i32* %60, i64 %61
store i32 1, i32* %62
%63 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%64 = load i32*, i32** %63
%65 = load i64, i64* @VTIME
%66 = getelementptr inbounds i32, i32* %64, i64 %65
store i32 5, i32* %66
%67 = load i32, i32* @IXON
%68 = load i32, i32* @IXOFF
%69 = or i32 %67, %68
%70 = load i32, i32* @IXANY
%71 = or i32 %69, %70
%72 = xor i32 %71, -1
%73 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%74 = load i32, i32* %73
%75 = and i32 %74, %72
store i32 %75, i32* %73
%76 = load i32, i32* @CLOCAL
%77 = load i32, i32* @CREAD
%78 = or i32 %76, %77
%79 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%80 = load i32, i32* %79
%81 = or i32 %80, %78
store i32 %81, i32* %79
%82 = load i32, i32* @PARENB
%83 = load i32, i32* @PARODD
%84 = or i32 %82, %83
%85 = xor i32 %84, -1
%86 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%87 = load i32, i32* %86
%88 = and i32 %87, %85
store i32 %88, i32* %86
%89 = load i32, i32* @CSTOPB
%90 = xor i32 %89, -1
%91 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%92 = load i32, i32* %91
%93 = and i32 %92, %90
store i32 %93, i32* %91
%94 = load i32, i32* %6
%95 = load i32, i32* @TCSANOW
%96 = call i64 @tcsetattr(i32 %94, i32 %95, %struct.termios* %7)
%97 = icmp ne i64 %96, 0
br i1 %97, label %98, label %99
98: ; preds = %33
store i32 -1, i32* %3
br label %102
99: ; preds = %33
br label %100
100: ; preds = %99, %2
%101 = load i32, i32* %6
store i32 %101, i32* %3
br label %102
102: ; preds = %100, %98, %32, %27, %22
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@IGNBRK = global i32 0
@INLCR = global i32 0
@ICRNL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@CSTOPB = global i32 0
@TCSANOW = global i32 0
define i32 @serial_open(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_SYNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %70
11: ; preds = %2
%12 = bitcast %struct.termios* %3 to i8*
%13 = call i32 @memset(%struct.termios* %3, i32 0, i32 32)
%14 = call i64 @tcgetattr(i32 %9, %struct.termios* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %71
16: ; preds = %11
%17 = call i64 @cfsetospeed(%struct.termios* %3, i32 %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %72
19: ; preds = %16
%20 = call i64 @cfsetispeed(%struct.termios* %3, i32 %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %73
22: ; preds = %19
%23 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @CSIZE
%26 = xor i32 %25, -1
%27 = and i32 %24, %26
%28 = load i32, i32* @CS8
%29 = or i32 %27, %28
store i32 %29, i32* %23
%30 = load i32, i32* @IGNBRK
%31 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%32 = load i32, i32* %31
%33 = load i32, i32* @INLCR
%34 = or i32 %30, %33
%35 = load i32, i32* @ICRNL
%36 = or i32 %34, %35
%37 = xor i32 %36, -1
%38 = and i32 %32, %37
store i32 %38, i32* %31
%39 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%40 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%41 = bitcast i64* %39 to i8*
%42 = load i32*, i32** %40
%43 = load i64, i64* @VMIN
%44 = getelementptr inbounds i32, i32* %42, i64 %43
store i32 1, i32* %44
%45 = load i64, i64* @VTIME
%46 = getelementptr inbounds i32, i32* %42, i64 %45
store i32 5, i32* %46
%47 = load i32, i32* @IXON
%48 = load i32, i32* @IXOFF
%49 = or i32 %48, %47
%50 = load i32, i32* @IXANY
%51 = or i32 %49, %50
%52 = xor i32 %51, -1
%53 = load i32, i32* %31
%54 = and i32 %53, %52
store i32 %54, i32* %31
%55 = load i32, i32* @CLOCAL
%56 = load i32, i32* @CREAD
%57 = or i32 %56, %55
%58 = load i32, i32* %23
%59 = or i32 %57, %58
%60 = load i32, i32* @PARENB
%61 = load i32, i32* @PARODD
%62 = or i32 %61, %60
%63 = load i32, i32* @CSTOPB
%64 = or i32 %62, %63
%65 = xor i32 %64, -1
%66 = and i32 %59, %65
store i32 %66, i32* %23
%67 = load i32, i32* @TCSANOW
%68 = call i64 @tcsetattr(i32 %9, i32 %67, %struct.termios* %3)
%69 = icmp eq i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %22, %2
br label %74
71: ; preds = %11
br label %74
72: ; preds = %16
br label %74
73: ; preds = %19
br label %74
74: ; preds = %73, %72, %71, %22, %70
%75 = phi i32 [ %9, %70 ], [ -1, %22 ], [ -1, %71 ], [ -1, %72 ], [ -1, %73 ]
ret i32 %75
}