#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 */

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int open(char const *, int);
scalar_t__ serial_set_attribs(int);
int lux_serial_open(const char *path) {
  int fd = open(path, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0)
    return -1;
  if (serial_set_attribs(fd) < 0)
    return -2;
  return fd;
}


x86 assembly (clang, -O0):
<lux_serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2eb1(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
mov    0x2e97(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1169 <lux_serial_open+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    118d <lux_serial_open+0x6d>
mov    -0x14(%rbp),%edi
call   1030 <serial_set_attribs@plt>
cmp    $0x0,%rax
jge    1187 <lux_serial_open+0x67>
movl   $0xfffffffe,-0x4(%rbp)
jmp    118d <lux_serial_open+0x6d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lux_serial_open>:
push   %rbx
mov    0x2ec0(%rip),%rax
mov    0x2ea1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ea6(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
test   %eax,%eax
js     115b <lux_serial_open+0x3b>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <serial_set_attribs@plt>
test   %rax,%rax
mov    $0xfffffffe,%eax
cmovns %ebx,%eax
pop    %rbx
ret
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
define i32 @lux_serial_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %22
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i64 @serial_set_attribs(i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -2, i32* %2
br label %22
20: ; preds = %15
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %22
22: ; preds = %20, %19, %14
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
define i32 @lux_serial_open(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_SYNC
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = tail call i64 @serial_set_attribs(i32 %7)
%11 = icmp slt i64 %10, 0
%12 = select i1 %11, i32 -2, i32 %7
br label %13
13: ; preds = %9, %1
%14 = phi i32 [ -1, %1 ], [ %12, %9 ]
ret i32 %14
}