#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_cflag;
  int c_iflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
};
typedef int /*<<< orphan*/ newsertio;

/* Variables and functions */
int CLOCAL;
int CREAD;
int CS8;
int IGNBRK;
int IGNPAR;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char const *, int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial(char const *serial_dev, int baud) {
  struct termios newsertio;
  int serial_fd;
  memset(&newsertio, 0, sizeof(newsertio));
  if ((serial_fd = open(serial_dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
    perror(serial_dev);
    return -1;
  }
  newsertio.c_cflag = baud | CS8 | CLOCAL | CREAD;
  newsertio.c_iflag = IGNBRK | IGNPAR;
  newsertio.c_oflag = 0;
  newsertio.c_lflag = 0;
  newsertio.c_cc[VMIN] = 1;
  newsertio.c_cc[VTIME] = 0;
  tcflush(serial_fd, TCIFLUSH);
  tcsetattr(serial_fd, TCSANOW, &newsertio);
  fprintf(stderr, "Listening on %s\n", serial_dev);
  return serial_fd;
}


x86 assembly (clang, -O0):
<open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    11c4 <open_serial+0x64>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <open_serial+0x119>
mov    -0x14(%rbp),%eax
mov    0x2dfa(%rip),%rcx
or     (%rcx),%eax
mov    0x2da1(%rip),%rcx
or     (%rcx),%eax
mov    0x2df0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    0x2d93(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2dab(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2d66(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x3c(%rbp),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x3c(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd92(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e65(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1252 <open_serial+0xf2>
mov    %eax,%ebp
mov    0x2e18(%rip),%rax
or     (%rax),%ebx
mov    0x2dbf(%rip),%rax
or     (%rax),%ebx
mov    0x2e0e(%rip),%rax
or     (%rax),%ebx
mov    %ebx,(%rsp)
mov    0x2dca(%rip),%rax
mov    0x2db3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x4(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d8e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <tcflush@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <tcsetattr@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14,%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1247 <open_serial+0xe7>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"Listening on %s\0A\00"
define i32 @open_serial(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i32 @memset(%struct.termios* %6, i32 0, i32 32)
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %9, i32 %14)
store i32 %15, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 @perror(i8* %18)
store i32 -1, i32* %3
br label %53
20: ; preds = %2
%21 = load i32, i32* %5
%22 = load i32, i32* @CS8
%23 = or i32 %21, %22
%24 = load i32, i32* @CLOCAL
%25 = or i32 %23, %24
%26 = load i32, i32* @CREAD
%27 = or i32 %25, %26
%28 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @IGNBRK
%30 = load i32, i32* @IGNPAR
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
store i64 0, i64* %33
%34 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i64 0, i64* %34
%35 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%36 = load i32*, i32** %35
%37 = load i64, i64* @VMIN
%38 = getelementptr inbounds i32, i32* %36, i64 %37
store i32 1, i32* %38
%39 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%40 = load i32*, i32** %39
%41 = load i64, i64* @VTIME
%42 = getelementptr inbounds i32, i32* %40, i64 %41
store i32 0, i32* %42
%43 = load i32, i32* %7
%44 = load i32, i32* @TCIFLUSH
%45 = call i32 @tcflush(i32 %43, i32 %44)
%46 = load i32, i32* %7
%47 = load i32, i32* @TCSANOW
%48 = call i32 @tcsetattr(i32 %46, i32 %47, %struct.termios* %6)
%49 = load i32, i32* @stderr
%50 = load i8*, i8** %4
%51 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %50)
%52 = load i32, i32* %7
store i32 %52, i32* %3
br label %53
53: ; preds = %20, %17
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"Listening on %s\0A\00"
define i32 @open_serial(i8* %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @memset(%struct.termios* %3, i32 0, i32 32)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* %0)
br label %41
15: ; preds = %2
%16 = load i32, i32* @CS8
%17 = or i32 %16, %1
%18 = load i32, i32* @CLOCAL
%19 = or i32 %17, %18
%20 = load i32, i32* @CREAD
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @IGNBRK
%24 = load i32, i32* @IGNPAR
%25 = or i32 %24, %23
%26 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%28 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%29 = bitcast i64* %27 to i8*
%30 = load i32*, i32** %28
%31 = load i64, i64* @VMIN
%32 = getelementptr inbounds i32, i32* %30, i64 %31
store i32 1, i32* %32
%33 = load i64, i64* @VTIME
%34 = getelementptr inbounds i32, i32* %30, i64 %33
store i32 0, i32* %34
%35 = load i32, i32* @TCIFLUSH
%36 = call i32 @tcflush(i32 %11, i32 %35)
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %11, i32 %37, %struct.termios* %3)
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %41
41: ; preds = %15, %13
%42 = phi i32 [ -1, %13 ], [ %11, %15 ]
ret i32 %42
}