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

/* Variables and functions */
int /*<<< orphan*/ B9600;
int /*<<< orphan*/ FNDELAY;
int /*<<< orphan*/ F_SETFL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial(char *serial_device) {
  int serfd = open(serial_device, O_RDWR | O_NOCTTY | O_NDELAY);
  if (serfd < 0)
    return serfd;
  fcntl(serfd, F_SETFL, FNDELAY);
  struct termios options;
  tcgetattr(serfd, &options);
  cfsetispeed(&options, B9600);
  cfsetospeed(&options, B9600);
  cfmakeraw(&options);
  options.c_cc[VMIN] = 0;
  options.c_cc[VTIME] = 10;
  tcsetattr(serfd, TCSANOW, &options);
  return serfd;
}


x86 assembly (clang, -O0):
<open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b8 <open_serial+0x48>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <open_serial+0xe0>
mov    -0x14(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1090 <cfsetispeed@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1040 <cfsetospeed@plt>
lea    -0x20(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    -0x20(%rbp),%rax
mov    0x2dba(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2d75(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    -0x14(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <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>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     122a <open_serial+0xba>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
lea    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
mov    0x2e06(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1090 <cfsetispeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1040 <cfsetospeed@plt>
mov    %r14,%rdi
call   1050 <cfmakeraw@plt>
mov    0x8(%rsp),%rax
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d93(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32* }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@F_SETFL = global i32 0
@FNDELAY = global i32 0
@B9600 = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @open_serial(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_NOCTTY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_NDELAY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i32, i32* %4
store i32 %16, i32* %2
br label %41
17: ; preds = %1
%18 = load i32, i32* %4
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* @FNDELAY
%21 = call i32 @fcntl(i32 %18, i32 %19, i32 %20)
%22 = load i32, i32* %4
%23 = call i32 @tcgetattr(i32 %22, %struct.termios* %5)
%24 = load i32, i32* @B9600
%25 = call i32 @cfsetispeed(%struct.termios* %5, i32 %24)
%26 = load i32, i32* @B9600
%27 = call i32 @cfsetospeed(%struct.termios* %5, i32 %26)
%28 = call i32 @cfmakeraw(%struct.termios* %5)
%29 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%30 = load i32*, i32** %29
%31 = load i64, i64* @VMIN
%32 = getelementptr inbounds i32, i32* %30, i64 %31
store i32 0, i32* %32
%33 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%34 = load i32*, i32** %33
%35 = load i64, i64* @VTIME
%36 = getelementptr inbounds i32, i32* %34, i64 %35
store i32 10, i32* %36
%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 = %17, %15
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.termios = type { i32* }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@F_SETFL = global i32 0
@FNDELAY = global i32 0
@B9600 = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @open_serial(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_NDELAY
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %29, label %10
10: ; preds = %1
%11 = load i32, i32* @F_SETFL
%12 = load i32, i32* @FNDELAY
%13 = tail call i32 @fcntl(i32 %8, i32 %11, i32 %12)
%14 = bitcast %struct.termios* %2 to i8*
%15 = call i32 @tcgetattr(i32 %8, %struct.termios* %2)
%16 = load i32, i32* @B9600
%17 = call i32 @cfsetispeed(%struct.termios* %2, i32 %16)
%18 = load i32, i32* @B9600
%19 = call i32 @cfsetospeed(%struct.termios* %2, i32 %18)
%20 = call i32 @cfmakeraw(%struct.termios* %2)
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%22 = load i32*, i32** %21
%23 = load i64, i64* @VMIN
%24 = getelementptr inbounds i32, i32* %22, i64 %23
store i32 0, i32* %24
%25 = load i64, i64* @VTIME
%26 = getelementptr inbounds i32, i32* %22, i64 %25
store i32 10, i32* %26
%27 = load i32, i32* @TCSANOW
%28 = call i32 @tcsetattr(i32 %8, i32 %27, %struct.termios* %2)
br label %29
29: ; preds = %1, %10
ret i32 %8
}