#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_lflag;
};

/* Variables and functions */
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int ECHO;
int ECHOE;
int /*<<< orphan*/ F_SETFL;
int ICANON;
int ISIG;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *, int, char *);
char *strerror(int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int openserial(char *dev, int baud) {
  int fd = -1;
  struct termios options;
  fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    printf("open failed on %s, %d, %s\n", dev, errno, strerror(errno));
    return -1;
  }
  fcntl(fd, F_SETFL, 0);
  tcgetattr(fd, &options);
  cfsetispeed(&options, baud);
  cfsetospeed(&options, baud);
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  tcsetattr(fd, TCSANOW, &options);
  return fd;
}


x86 assembly (clang, -O0):
<openserial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    0x2dd5(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    120b <openserial+0x8b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xe06(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <openserial+0x143>
mov    -0x18(%rbp),%edi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdi
call   1090 <cfsetispeed@plt>
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdi
call   1040 <cfsetospeed@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    0x2d6e(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    0x2d4d(%rip),%rcx
or     (%rcx),%eax
mov    0x2d3c(%rip),%rcx
or     (%rcx),%eax
mov    0x2d2b(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openserial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e5a(%rip),%rcx
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2de2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     125c <openserial+0xdc>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1090 <cfsetispeed@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1040 <cfsetospeed@plt>
mov    0x2d7a(%rip),%rax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     (%rsp),%ecx
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2da0(%rip),%rcx
or     (%rcx),%eax
mov    0x2d5f(%rip),%rcx
mov    0x2db0(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d9d(%rip),%rcx
or     (%rcx),%edx
mov    %eax,(%rsp)
mov    0x2d89(%rip),%rax
or     (%rax),%edx
not    %edx
and    %edx,0x4(%rsp)
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
jmp    1283 <openserial+0x103>
mov    0x2d2d(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   10a0 <strerror@plt>
lea    0xd8b(%rip),%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
mov    %rax,%rcx
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [27 x i8] c"open failed on %s, %d, %s\0A\00"
@errno = global i32 0
@F_SETFL = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@TCSANOW = global i32 0
define i32 @openserial(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_NDELAY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i32, i32* @errno
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %19, i8* %21)
store i32 -1, i32* %3
br label %63
23: ; preds = %2
%24 = load i32, i32* %6
%25 = load i32, i32* @F_SETFL
%26 = call i32 @fcntl(i32 %24, i32 %25, i32 0)
%27 = load i32, i32* %6
%28 = call i32 @tcgetattr(i32 %27, %struct.termios* %7)
%29 = load i32, i32* %5
%30 = call i32 @cfsetispeed(%struct.termios* %7, i32 %29)
%31 = load i32, i32* %5
%32 = call i32 @cfsetospeed(%struct.termios* %7, i32 %31)
%33 = load i32, i32* @CLOCAL
%34 = load i32, i32* @CREAD
%35 = or i32 %33, %34
%36 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%37 = load i32, i32* %36
%38 = or i32 %37, %35
store i32 %38, i32* %36
%39 = load i32, i32* @CSIZE
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = and i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* @CS8
%45 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%46 = load i32, i32* %45
%47 = or i32 %46, %44
store i32 %47, i32* %45
%48 = load i32, i32* @ICANON
%49 = load i32, i32* @ECHO
%50 = or i32 %48, %49
%51 = load i32, i32* @ECHOE
%52 = or i32 %50, %51
%53 = load i32, i32* @ISIG
%54 = or i32 %52, %53
%55 = xor i32 %54, -1
%56 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%57 = load i32, i32* %56
%58 = and i32 %57, %55
store i32 %58, i32* %56
%59 = load i32, i32* %6
%60 = load i32, i32* @TCSANOW
%61 = call i32 @tcsetattr(i32 %59, i32 %60, %struct.termios* %7)
%62 = load i32, i32* %6
store i32 %62, i32* %3
br label %63
63: ; preds = %23, %17
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [27 x i8] c"open failed on %s, %d, %s\0A\00"
@errno = global i32 0
@F_SETFL = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@TCSANOW = global i32 0
define i32 @openserial(i8* %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_NDELAY
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %13, i8* %14)
br label %46
16: ; preds = %2
%17 = load i32, i32* @F_SETFL
%18 = tail call i32 @fcntl(i32 %10, i32 %17, i32 0)
%19 = call i32 @tcgetattr(i32 %10, %struct.termios* %3)
%20 = call i32 @cfsetispeed(%struct.termios* %3, i32 %1)
%21 = call i32 @cfsetospeed(%struct.termios* %3, i32 %1)
%22 = load i32, i32* @CLOCAL
%23 = load i32, i32* @CREAD
%24 = or i32 %23, %22
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = or i32 %24, %26
%28 = load i32, i32* @CSIZE
%29 = xor i32 %28, -1
%30 = and i32 %27, %29
%31 = load i32, i32* @CS8
%32 = or i32 %31, %30
store i32 %32, i32* %25
%33 = load i32, i32* @ICANON
%34 = load i32, i32* @ECHO
%35 = or i32 %34, %33
%36 = load i32, i32* @ECHOE
%37 = or i32 %35, %36
%38 = load i32, i32* @ISIG
%39 = or i32 %37, %38
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%42 = load i32, i32* %41
%43 = and i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* @TCSANOW
%45 = call i32 @tcsetattr(i32 %10, i32 %44, %struct.termios* %3)
br label %46
46: ; preds = %16, %12
ret i32 %10
}