#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;
  int /*<<< orphan*/ c_line;
};
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*/ printf(char *, int /*<<< orphan*/, scalar_t__, int,
                          scalar_t__, int /*<<< orphan*/);
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);
  printf("termios: %li %ld %ld %ld %ld\n", options.c_iflag, options.c_oflag,
         options.c_cflag, options.c_lflag, options.c_line);
  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    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a0 <open_port+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <open_port+0xf0>
lea    -0x40(%rbp),%rdi
mov    $0x20,%esi
call   1080 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x28(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    -0x40(%rbp),%ecx
mov    -0x38(%rbp),%r8
mov    -0x24(%rbp),%r9d
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    0x2db0(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd7(%rip),%rcx
or     (%rcx),%eax
mov    0x2d96(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x14(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1060 <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    0x2e7a(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1231 <open_port+0xd1>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x20,%esi
call   1080 <bzero@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x20(%rsp),%esi
mov    0x18(%rsp),%rdx
mov    0x8(%rsp),%ecx
mov    0x10(%rsp),%r8
mov    0x24(%rsp),%r9d
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e09(%rip),%rax
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de7(%rip),%rax
or     (%rax),%ecx
mov    0x2da6(%rip),%rax
or     (%rax),%ecx
mov    0x2ddd(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [30 x i8] c"termios: %li %ld %ld %ld %ld\0A\00"
@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 %51
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 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%26 = load i64, i64* %25
%27 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
%28 = load i32, i32* %27
%29 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %20, i64 %22, i32 %24, i64 %26, i32 %28)
%30 = load i32, i32* @B9600
%31 = load i32, i32* @CRTSCTS
%32 = or i32 %30, %31
%33 = load i32, i32* @CS8
%34 = or i32 %32, %33
%35 = load i32, i32* @CLOCAL
%36 = or i32 %34, %35
%37 = load i32, i32* @CREAD
%38 = or i32 %36, %37
%39 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* @IGNPAR
%41 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 0, i64* %42
%43 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
store i64 0, i64* %43
%44 = load i32, i32* %4
%45 = load i32, i32* @TCIFLUSH
%46 = call i32 @tcflush(i32 %44, i32 %45)
%47 = load i32, i32* %4
%48 = load i32, i32* @TCSANOW
%49 = call i32 @tcsetattr(i32 %47, i32 %48, %struct.termios* %6)
%50 = load i32, i32* %4
store i32 %50, i32* %2
br label %51
51: ; preds = %15, %14
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [30 x i8] c"termios: %li %ld %ld %ld %ld\0A\00"
@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 %38, label %9
9: ; preds = %1
%10 = call i32 @bzero(%struct.termios* %2, i32 32)
%11 = call i32 @tcgetattr(i32 %7, %struct.termios* %2)
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%15 = load i64, i64* %14
%16 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 4
%21 = load i32, i32* %20
%22 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %13, i64 %15, i32 %17, i64 %19, i32 %21)
%23 = load i32, i32* @B9600
%24 = load i32, i32* @CRTSCTS
%25 = or i32 %24, %23
%26 = load i32, i32* @CS8
%27 = or i32 %25, %26
%28 = load i32, i32* @CLOCAL
%29 = or i32 %27, %28
%30 = load i32, i32* @CREAD
%31 = or i32 %29, %30
store i32 %31, i32* %16
%32 = load i32, i32* @IGNPAR
store i32 %32, i32* %12
%33 = bitcast i64* %18 to i8*
%34 = load i32, i32* @TCIFLUSH
%35 = call i32 @tcflush(i32 %7, i32 %34)
%36 = load i32, i32* @TCSANOW
%37 = call i32 @tcsetattr(i32 %7, i32 %36, %struct.termios* %2)
br label %38
38: ; preds = %1, %9
ret i32 %7
}