#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 dummy;
};

/* 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(const char *device, int rate) {
  int fd = open(device, O_RDWR | O_NOCTTY);
  struct termios attr;
  tcgetattr(fd, &attr);
  cfmakeraw(&attr);
  cfsetispeed(&attr, rate);
  cfsetospeed(&attr, rate);
  if (tcsetattr(fd, TCSAFLUSH, &attr) < 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%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),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11e5 <open_serial+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <open_serial+0x8b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    0x2e76(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %eax,%edi
mov    %rbp,%rsi
call   1050 <tcgetattr@plt>
mov    %rbp,%rdi
call   1040 <cfmakeraw@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1080 <cfsetispeed@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1030 <cfsetospeed@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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(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 = call i32 @open(i8* %8, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = call i32 @tcgetattr(i32 %13, %struct.termios* %7)
%15 = call i32 @cfmakeraw(%struct.termios* %7)
%16 = load i32, i32* %5
%17 = call i32 @cfsetispeed(%struct.termios* %7, i32 %16)
%18 = load i32, i32* %5
%19 = call i32 @cfsetospeed(%struct.termios* %7, i32 %18)
%20 = load i32, i32* %6
%21 = load i32, i32* @TCSAFLUSH
%22 = call i64 @tcsetattr(i32 %20, i32 %21, %struct.termios* %7)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 -1, i32* %3
br label %27
25: ; preds = %2
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %27
27: ; preds = %25, %24
%28 = load i32, i32* %3
ret i32 %28
}

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(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 = tail call i32 @open(i8* %0, i32 %6)
%8 = bitcast %struct.termios* %3 to i8*
%9 = call i32 @tcgetattr(i32 %7, %struct.termios* %3)
%10 = call i32 @cfmakeraw(%struct.termios* %3)
%11 = call i32 @cfsetispeed(%struct.termios* %3, i32 %1)
%12 = call i32 @cfsetospeed(%struct.termios* %3, i32 %1)
%13 = load i32, i32* @TCSAFLUSH
%14 = call i64 @tcsetattr(i32 %7, i32 %13, %struct.termios* %3)
%15 = icmp slt i64 %14, 0
%16 = select i1 %15, i32 -1, i32 %7
ret i32 %16
}