#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;
};
typedef int /*<<< orphan*/ serialopts;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CS8;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial_port(const char *path) {
  struct termios serialopts;
  int serial_fd;
  serial_fd = open(path, O_RDWR | O_NOCTTY);
  if (serial_fd < 0) {
    perror("couldn't open serial port");
    return -1;
  }
  memset(&serialopts, 0, sizeof(serialopts));
  serialopts.c_cflag = CS8 | CLOCAL | CREAD;
  cfsetspeed(&serialopts, B115200);
  if (tcsetattr(serial_fd, TCSANOW, &serialopts) != 0) {
    perror("Unable to initialize serial port");
    return -1;
  }
  tcflush(serial_fd, TCIOFLUSH);
  return serial_fd;
}


x86 assembly (clang, -O0):
<open_serial_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ac <open_serial_port+0x4c>
lea    0xe65(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <open_serial_port+0xda>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    0x2dd4(%rip),%rcx
or     (%rcx),%eax
mov    0x2e03(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetspeed@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     1223 <open_serial_port+0xc3>
lea    0xe08(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <open_serial_port+0xda>
mov    -0x1c(%rbp),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial_port>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11f4 <open_serial_port+0x94>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e36(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e2c(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <cfsetspeed@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
jne    11fd <open_serial_port+0x9d>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe05(%rip),%rdi
jmp    1204 <open_serial_port+0xa4>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11ea <open_serial_port+0x8a>

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [26 x i8] c"couldn't open serial port\00"
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str.1 = [33 x i8] c"Unable to initialize serial port\00"
@TCIOFLUSH = global i32 0
define i32 @open_serial_port(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.termios
%5 = alloca i32
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 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
15: ; preds = %1
%16 = call i32 @memset(%struct.termios* %4, i32 0, i32 4)
%17 = load i32, i32* @CS8
%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* %4, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @B115200
%24 = call i32 @cfsetspeed(%struct.termios* %4, i32 %23)
%25 = load i32, i32* %5
%26 = load i32, i32* @TCSANOW
%27 = call i64 @tcsetattr(i32 %25, i32 %26, %struct.termios* %4)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
31: ; preds = %15
%32 = load i32, i32* %5
%33 = load i32, i32* @TCIOFLUSH
%34 = call i32 @tcflush(i32 %32, i32 %33)
%35 = load i32, i32* %5
store i32 %35, i32* %2
br label %36
36: ; preds = %31, %29, %13
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [26 x i8] c"couldn't open serial port\00"
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str.1 = [33 x i8] c"Unable to initialize serial port\00"
@TCIOFLUSH = global i32 0
define i32 @open_serial_port(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%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 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %29
11: ; preds = %1
%12 = call i32 @memset(%struct.termios* %2, i32 0, i32 4)
%13 = load i32, i32* @CS8
%14 = load i32, i32* @CLOCAL
%15 = or i32 %14, %13
%16 = load i32, i32* @CREAD
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @B115200
%20 = call i32 @cfsetspeed(%struct.termios* %2, i32 %19)
%21 = load i32, i32* @TCSANOW
%22 = call i64 @tcsetattr(i32 %7, i32 %21, %struct.termios* %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %11
%25 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %29
26: ; preds = %11
%27 = load i32, i32* @TCIOFLUSH
%28 = call i32 @tcflush(i32 %7, i32 %27)
br label %29
29: ; preds = %26, %24, %9
%30 = phi i32 [ -1, %9 ], [ -1, %24 ], [ %7, %26 ]
ret i32 %30
}