#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 /*<<< orphan*/ c_cflag;
};

/* Variables and functions */
int /*<<< orphan*/ CS8;
int /*<<< orphan*/ CSIZE;
int /*<<< orphan*/ CSTOPB;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ PARENB;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetspeed(struct termios *, int);
int open(char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial_device(char *path) {
  int fd = open(path, O_RDWR | O_NONBLOCK);
  if (fd == -1)
    return -1;
  struct termios options;
  tcgetattr(fd, &options);
  cfsetspeed(&options, 460800);
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  tcsetattr(fd, TCSANOW, &options);
  return fd;
}


x86 assembly (clang, -O0):
<open_serial_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1180 <open_serial_device+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <open_serial_device+0xba>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <tcgetattr@plt>
lea    -0x18(%rbp),%rdi
mov    $0x70800,%esi
call   1030 <cfsetspeed@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial_device>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%rax
mov    0x2e8e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11bb <open_serial_device+0x7b>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    %r14,%rdi
mov    $0x70800,%esi
call   1030 <cfsetspeed@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    0x2e1c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e2b(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    (%rsp),%eax
mov    0x2e25(%rip),%rcx
or     (%rcx),%eax
mov    %eax,(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
define i32 @open_serial_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %41
14: ; preds = %1
%15 = load i32, i32* %4
%16 = call i32 @tcgetattr(i32 %15, %struct.termios* %5)
%17 = call i32 @cfsetspeed(%struct.termios* %5, i32 460800)
%18 = load i32, i32* @PARENB
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @CSTOPB
%24 = xor i32 %23, -1
%25 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%26 = load i32, i32* %25
%27 = and i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* @CSIZE
%29 = xor i32 %28, -1
%30 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%31 = load i32, i32* %30
%32 = and i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @CS8
%34 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%35 = load i32, i32* %34
%36 = or i32 %35, %33
store i32 %36, i32* %34
%37 = load i32, i32* %4
%38 = load i32, i32* @TCSANOW
%39 = call i32 @tcsetattr(i32 %37, i32 %38, %struct.termios* %5)
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %41
41: ; preds = %14, %13
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
define i32 @open_serial_device(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NONBLOCK
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %25, label %8
8: ; preds = %1
%9 = bitcast %struct.termios* %2 to i8*
%10 = call i32 @tcgetattr(i32 %6, %struct.termios* %2)
%11 = call i32 @cfsetspeed(%struct.termios* %2, i32 460800)
%12 = load i32, i32* @PARENB
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @CSTOPB
%16 = or i32 %12, %15
%17 = load i32, i32* @CSIZE
%18 = or i32 %16, %17
%19 = xor i32 %18, -1
%20 = and i32 %14, %19
%21 = load i32, i32* @CS8
%22 = or i32 %21, %20
store i32 %22, i32* %13
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %6, i32 %23, %struct.termios* %2)
br label %25
25: ; preds = %1, %8
ret i32 %6
}