#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 CLOCAL;
int CREAD;
int CS8;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetspeed(struct termios *, int);
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, 921600);
  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    1236 <open_serial_port+0xd6>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    0x2ddc(%rip),%rcx
or     (%rcx),%eax
mov    0x2e03(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xe1000,%esi
call   1050 <cfsetspeed@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     121f <open_serial_port+0xbf>
lea    0xe0c(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <open_serial_port+0xd6>
mov    -0x1c(%rbp),%edi
mov    0x2d8f(%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     11f0 <open_serial_port+0x90>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e36(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e2c(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    %r14,%rdi
mov    $0xe1000,%esi
call   1050 <cfsetspeed@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
jne    11f9 <open_serial_port+0x99>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe09(%rip),%rdi
jmp    1200 <open_serial_port+0xa0>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11e6 <open_serial_port+0x86>

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
@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 %35
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 = call i32 @cfsetspeed(%struct.termios* %4, i32 921600)
%24 = load i32, i32* %5
%25 = load i32, i32* @TCSANOW
%26 = call i64 @tcsetattr(i32 %24, i32 %25, %struct.termios* %4)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = 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 %35
30: ; preds = %15
%31 = load i32, i32* %5
%32 = load i32, i32* @TCIOFLUSH
%33 = call i32 @tcflush(i32 %31, i32 %32)
%34 = load i32, i32* %5
store i32 %34, i32* %2
br label %35
35: ; preds = %30, %28, %13
%36 = load i32, i32* %2
ret i32 %36
}

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
@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 %28
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 = call i32 @cfsetspeed(%struct.termios* %2, i32 921600)
%20 = load i32, i32* @TCSANOW
%21 = call i64 @tcsetattr(i32 %7, i32 %20, %struct.termios* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %11
%24 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %28
25: ; preds = %11
%26 = load i32, i32* @TCIOFLUSH
%27 = call i32 @tcflush(i32 %7, i32 %26)
br label %28
28: ; preds = %25, %23, %9
%29 = phi i32 [ -1, %9 ], [ -1, %23 ], [ %7, %25 ]
ret i32 %29
}