#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*/ tcflag_t;
struct termios {
  int /*<<< orphan*/ c_cflag;
};

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int open(char const *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial_and_set_cflags(const char *device, int rate, tcflag_t set) {
  int fd = open(device, O_RDWR | O_NOCTTY);
  struct termios attr;
  if (fd < 0)
    return -1;
  tcgetattr(fd, &attr);
  cfmakeraw(&attr);
  cfsetispeed(&attr, rate);
  cfsetospeed(&attr, rate);
  attr.c_cflag |= set;
  if (tcsetattr(fd, TCSAFLUSH, &attr) < 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<open_serial_and_set_cflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a6 <open_serial_and_set_cflags+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <open_serial_and_set_cflags+0xad>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <tcgetattr@plt>
lea    -0x20(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0x18(%rbp),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1207 <open_serial_and_set_cflags+0xa7>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <open_serial_and_set_cflags+0xad>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial_and_set_cflags>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    0x2e75(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11d6 <open_serial_and_set_cflags+0x76>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %eax,%edi
mov    %r15,%rsi
call   1050 <tcgetattr@plt>
mov    %r15,%rdi
call   1040 <cfmakeraw@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   1080 <cfsetispeed@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   1030 <cfsetospeed@plt>
or     %r14d,(%rsp)
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebp,%eax
jmp    11db <open_serial_and_set_cflags+0x7b>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCSAFLUSH = global i32 0
define i32 @open_serial_and_set_cflags(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.termios
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %37
18: ; preds = %3
%19 = load i32, i32* %8
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %9)
%21 = call i32 @cfmakeraw(%struct.termios* %9)
%22 = load i32, i32* %6
%23 = call i32 @cfsetispeed(%struct.termios* %9, i32 %22)
%24 = load i32, i32* %6
%25 = call i32 @cfsetospeed(%struct.termios* %9, i32 %24)
%26 = load i32, i32* %7
%27 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%28 = load i32, i32* %27
%29 = or i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* %8
%31 = load i32, i32* @TCSAFLUSH
%32 = call i64 @tcsetattr(i32 %30, i32 %31, %struct.termios* %9)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %18
store i32 -1, i32* %4
br label %37
35: ; preds = %18
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %34, %17
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCSAFLUSH = global i32 0
define i32 @open_serial_and_set_cflags(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.termios
%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 = bitcast %struct.termios* %4 to i8*
%10 = icmp slt i32 %8, 0
br i1 %10, label %23, label %11
11: ; preds = %3
%12 = call i32 @tcgetattr(i32 %8, %struct.termios* %4)
%13 = call i32 @cfmakeraw(%struct.termios* %4)
%14 = call i32 @cfsetispeed(%struct.termios* %4, i32 %1)
%15 = call i32 @cfsetospeed(%struct.termios* %4, i32 %1)
%16 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = or i32 %17, %2
store i32 %18, i32* %16
%19 = load i32, i32* @TCSAFLUSH
%20 = call i64 @tcsetattr(i32 %8, i32 %19, %struct.termios* %4)
%21 = icmp slt i64 %20, 0
%22 = select i1 %21, i32 -1, i32 %8
br label %23
23: ; preds = %11, %3
%24 = phi i32 [ -1, %3 ], [ %22, %11 ]
ret i32 %24
}