#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_oflag;
  int c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ B9600;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int ECHO;
int ICANON;
int IGNBRK;
int IGNPAR;
int ISIG;
int IXANY;
int IXOFF;
int IXON;
int ONLCR;
int OPOST;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ dprintf(int /*<<< orphan*/, char *, ...);
int open(char const *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial(const char *file) {
  int fd;
  dprintf(STDERR_FILENO, "Opening %s\n", file);
  struct termios newtio;
  fd = open(file, O_RDWR | O_NONBLOCK | O_NOCTTY);
  if (fd == -1) {
    dprintf(STDERR_FILENO, "Unable to open serial port\n");
    return fd;
  }
  tcgetattr(fd, &newtio);
  cfsetospeed(&newtio, B9600);
  cfsetispeed(&newtio, B9600);
  newtio.c_cflag |= CLOCAL | CREAD;
  newtio.c_cflag &= ~PARENB;
  newtio.c_cflag &= ~CSTOPB;
  newtio.c_cflag &= ~CSIZE;
  newtio.c_cflag |= CS8;
  newtio.c_cflag &= ~CRTSCTS;
  newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
  newtio.c_iflag |= IGNPAR | IGNBRK;
  newtio.c_oflag &= ~(ONLCR | OPOST);
  newtio.c_lflag &= ~(ISIG | ICANON | ECHO);
  tcsetattr(fd, TCSANOW, &newtio);
  return fd;
}


x86 assembly (clang, -O0):
<open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
mov    0x2dcc(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11da <open_serial+0x7a>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    130f <open_serial+0x1af>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    0x2d7e(%rip),%rcx
or     (%rcx),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    0x2d57(%rip),%rcx
or     (%rcx),%eax
mov    0x2ce6(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cac(%rip),%rax
mov    (%rax),%eax
mov    0x2cb3(%rip),%rcx
or     (%rcx),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2c9c(%rip),%rax
mov    (%rax),%eax
mov    0x2d0b(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cd1(%rip),%rax
mov    (%rax),%eax
mov    0x2c68(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc7(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cba(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2df5(%rip),%r14
mov    (%r14),%edi
lea    0xe83(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <dprintf@plt>
mov    0x2e5a(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12b1 <open_serial+0x151>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2dd7(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1080 <cfsetispeed@plt>
mov    0x2d42(%rip),%rax
mov    0x2dab(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     (%rsp),%ecx
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    0x2d2c(%rip),%rdx
or     (%rdx),%eax
mov    0x2d7b(%rip),%rdx
or     (%rdx),%eax
not    %eax
and    %ecx,%eax
mov    0x2d76(%rip),%rcx
or     (%rcx),%eax
mov    0x2d15(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,(%rsp)
mov    0x2d95(%rip),%rax
mov    0x2da6(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d33(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    0x4(%rsp),%ecx
mov    0x2cfc(%rip),%rax
mov    0x2d05(%rip),%rdx
or     (%rax),%ecx
or     (%rdx),%ecx
mov    %ecx,0x4(%rsp)
mov    0x2cee(%rip),%rax
mov    0x2d5f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x2d26(%rip),%rax
mov    0x2cbf(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d1c(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0xc(%rsp)
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
jmp    12c2 <open_serial+0x162>
mov    (%r14),%edi
lea    0xd51(%rip),%rsi
xor    %eax,%eax
call   1040 <dprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32 }
@STDERR_FILENO = global i32 0
@.str = [12 x i8] c"Opening %s\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [28 x i8] c"Unable to open serial port\0A\00"
@B9600 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CRTSCTS = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@IGNPAR = global i32 0
@IGNBRK = global i32 0
@ONLCR = global i32 0
@OPOST = global i32 0
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @open_serial(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
store i8* %0, i8** %3
%6 = load i32, i32* @STDERR_FILENO
%7 = load i8*, i8** %3
%8 = call i32 (i32, i8*, ...) @dprintf(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %10, %11
%13 = load i32, i32* @O_NOCTTY
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %9, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %1
%19 = load i32, i32* @STDERR_FILENO
%20 = call i32 (i32, i8*, ...) @dprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %94
22: ; preds = %1
%23 = load i32, i32* %4
%24 = call i32 @tcgetattr(i32 %23, %struct.termios* %5)
%25 = load i32, i32* @B9600
%26 = call i32 @cfsetospeed(%struct.termios* %5, i32 %25)
%27 = load i32, i32* @B9600
%28 = call i32 @cfsetispeed(%struct.termios* %5, i32 %27)
%29 = load i32, i32* @CLOCAL
%30 = load i32, i32* @CREAD
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%33 = load i32, i32* %32
%34 = or i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* @PARENB
%36 = xor i32 %35, -1
%37 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%38 = load i32, i32* %37
%39 = and i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* @CSTOPB
%41 = xor i32 %40, -1
%42 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%43 = load i32, i32* %42
%44 = and i32 %43, %41
store i32 %44, i32* %42
%45 = load i32, i32* @CSIZE
%46 = xor i32 %45, -1
%47 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%48 = load i32, i32* %47
%49 = and i32 %48, %46
store i32 %49, i32* %47
%50 = load i32, i32* @CS8
%51 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%52 = load i32, i32* %51
%53 = or i32 %52, %50
store i32 %53, i32* %51
%54 = load i32, i32* @CRTSCTS
%55 = xor i32 %54, -1
%56 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%57 = load i32, i32* %56
%58 = and i32 %57, %55
store i32 %58, i32* %56
%59 = load i32, i32* @IXON
%60 = load i32, i32* @IXOFF
%61 = or i32 %59, %60
%62 = load i32, i32* @IXANY
%63 = or i32 %61, %62
%64 = xor i32 %63, -1
%65 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%66 = load i32, i32* %65
%67 = and i32 %66, %64
store i32 %67, i32* %65
%68 = load i32, i32* @IGNPAR
%69 = load i32, i32* @IGNBRK
%70 = or i32 %68, %69
%71 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%72 = load i32, i32* %71
%73 = or i32 %72, %70
store i32 %73, i32* %71
%74 = load i32, i32* @ONLCR
%75 = load i32, i32* @OPOST
%76 = or i32 %74, %75
%77 = xor i32 %76, -1
%78 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
%79 = load i32, i32* %78
%80 = and i32 %79, %77
store i32 %80, i32* %78
%81 = load i32, i32* @ISIG
%82 = load i32, i32* @ICANON
%83 = or i32 %81, %82
%84 = load i32, i32* @ECHO
%85 = or i32 %83, %84
%86 = xor i32 %85, -1
%87 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 3
%88 = load i32, i32* %87
%89 = and i32 %88, %86
store i32 %89, i32* %87
%90 = load i32, i32* %4
%91 = load i32, i32* @TCSANOW
%92 = call i32 @tcsetattr(i32 %90, i32 %91, %struct.termios* %5)
%93 = load i32, i32* %4
store i32 %93, i32* %2
br label %94
94: ; preds = %22, %18
%95 = load i32, i32* %2
ret i32 %95
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32 }
@STDERR_FILENO = global i32 0
@.str = [12 x i8] c"Opening %s\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [28 x i8] c"Unable to open serial port\0A\00"
@B9600 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CRTSCTS = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@IGNPAR = global i32 0
@IGNBRK = global i32 0
@ONLCR = global i32 0
@OPOST = global i32 0
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @open_serial(i8* %0) {
%2 = alloca %struct.termios
%3 = load i32, i32* @STDERR_FILENO
%4 = tail call i32 (i32, i8*, ...) @dprintf(i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = bitcast %struct.termios* %2 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %6
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* @STDERR_FILENO
%15 = tail call i32 (i32, i8*, ...) @dprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %71
16: ; preds = %1
%17 = call i32 @tcgetattr(i32 %11, %struct.termios* %2)
%18 = load i32, i32* @B9600
%19 = call i32 @cfsetospeed(%struct.termios* %2, i32 %18)
%20 = load i32, i32* @B9600
%21 = call i32 @cfsetispeed(%struct.termios* %2, i32 %20)
%22 = load i32, i32* @CLOCAL
%23 = load i32, i32* @CREAD
%24 = or i32 %23, %22
%25 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%26 = load i32, i32* %25
%27 = or i32 %24, %26
%28 = load i32, i32* @PARENB
%29 = load i32, i32* @CSTOPB
%30 = or i32 %28, %29
%31 = load i32, i32* @CSIZE
%32 = or i32 %30, %31
%33 = xor i32 %32, -1
%34 = and i32 %27, %33
%35 = load i32, i32* @CS8
%36 = or i32 %35, %34
%37 = load i32, i32* @CRTSCTS
%38 = xor i32 %37, -1
%39 = and i32 %36, %38
store i32 %39, i32* %25
%40 = load i32, i32* @IXON
%41 = load i32, i32* @IXOFF
%42 = or i32 %41, %40
%43 = load i32, i32* @IXANY
%44 = or i32 %42, %43
%45 = xor i32 %44, -1
%46 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%47 = load i32, i32* %46
%48 = and i32 %47, %45
%49 = load i32, i32* @IGNPAR
%50 = load i32, i32* @IGNBRK
%51 = or i32 %49, %48
%52 = or i32 %51, %50
store i32 %52, i32* %46
%53 = load i32, i32* @ONLCR
%54 = load i32, i32* @OPOST
%55 = or i32 %54, %53
%56 = xor i32 %55, -1
%57 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%58 = load i32, i32* %57
%59 = and i32 %58, %56
store i32 %59, i32* %57
%60 = load i32, i32* @ISIG
%61 = load i32, i32* @ICANON
%62 = or i32 %61, %60
%63 = load i32, i32* @ECHO
%64 = or i32 %62, %63
%65 = xor i32 %64, -1
%66 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%67 = load i32, i32* %66
%68 = and i32 %67, %65
store i32 %68, i32* %66
%69 = load i32, i32* @TCSANOW
%70 = call i32 @tcsetattr(i32 %11, i32 %69, %struct.termios* %2)
br label %71
71: ; preds = %16, %13
ret i32 %11
}