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

/* Variables and functions */
int B9600;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ bzero(struct termios *, int);
int open(char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_port(char *device) {
  int fd;
  struct termios *current;
  struct termios options;
  fd = open(device, O_RDWR | O_NOCTTY);
  if (fd == -1)
    return -1;
  bzero(&options, sizeof(options));
  tcgetattr(fd, &options);
  options.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &options);
  return (fd);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1190 <open_port+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <open_port+0xd2>
lea    -0x40(%rbp),%rdi
mov    $0x20,%esi
call   1070 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    0x2dde(%rip),%rcx
or     (%rcx),%eax
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc4(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfb(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x14(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e8a(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11fe <open_port+0xae>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x20,%esi
call   1070 <bzero@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    0x2e3c(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e1a(%rip),%rax
or     (%rax),%ecx
mov    0x2dd9(%rip),%rax
or     (%rax),%ecx
mov    0x2e10(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B9600 = global i32 0
@CRTSCTS = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @open_port(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios*
%6 = alloca %struct.termios
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %40
15: ; preds = %1
%16 = call i32 @bzero(%struct.termios* %6, i32 32)
%17 = load i32, i32* %4
%18 = call i32 @tcgetattr(i32 %17, %struct.termios* %6)
%19 = load i32, i32* @B9600
%20 = load i32, i32* @CRTSCTS
%21 = or i32 %19, %20
%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* @IGNPAR
%30 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 0, i64* %31
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
store i64 0, i64* %32
%33 = load i32, i32* %4
%34 = load i32, i32* @TCIFLUSH
%35 = call i32 @tcflush(i32 %33, i32 %34)
%36 = load i32, i32* %4
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %36, i32 %37, %struct.termios* %6)
%39 = load i32, i32* %4
store i32 %39, i32* %2
br label %40
40: ; preds = %15, %14
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B9600 = global i32 0
@CRTSCTS = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @open_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 eq i32 %7, -1
br i1 %8, label %30, label %9
9: ; preds = %1
%10 = call i32 @bzero(%struct.termios* %2, i32 32)
%11 = call i32 @tcgetattr(i32 %7, %struct.termios* %2)
%12 = load i32, i32* @B9600
%13 = load i32, i32* @CRTSCTS
%14 = or i32 %13, %12
%15 = load i32, i32* @CS8
%16 = or i32 %14, %15
%17 = load i32, i32* @CLOCAL
%18 = or i32 %16, %17
%19 = load i32, i32* @CREAD
%20 = or i32 %18, %19
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @IGNPAR
%23 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%25 = bitcast i64* %24 to i8*
%26 = load i32, i32* @TCIFLUSH
%27 = call i32 @tcflush(i32 %7, i32 %26)
%28 = load i32, i32* @TCSANOW
%29 = call i32 @tcsetattr(i32 %7, i32 %28, %struct.termios* %2)
br label %30
30: ; preds = %1, %9
ret i32 %7
}