#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_cc;
  int c_cflag;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int /*<<< orphan*/ F_SETFL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
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 *path, int speed) {
  int fd;
  struct termios opt;
  int flags = O_RDWR | O_NOCTTY | O_NDELAY;
  if ((fd = open(path, flags)) < 0)
    return (-1);
  fcntl(fd, F_SETFL, 0);
  tcgetattr(fd, &opt);
  cfmakeraw(&opt);
  cfsetspeed(&opt, (speed_t)speed);
  opt.c_cc[VMIN] = 128;
  opt.c_cc[VTIME] = 1;
  opt.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
  opt.c_cflag |= (CS8 | CLOCAL | CREAD);
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &opt);
  return (fd);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    0x2e11(%rip),%rcx
or     (%rcx),%eax
mov    0x2df8(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11c1 <open_port+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <open_port+0x129>
mov    -0x18(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <tcgetattr@plt>
lea    -0x28(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    -0x14(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1050 <cfsetspeed@plt>
mov    -0x28(%rbp),%rax
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x80,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2d73(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    0x2d41(%rip),%rcx
or     (%rcx),%eax
mov    0x2d80(%rip),%rcx
or     (%rcx),%eax
mov    0x2d37(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    0x2d0c(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6b(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x18(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e69(%rip),%rcx
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%ebp
mov    0x2dfd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1090 <open@plt>
test   %eax,%eax
js     126b <open_port+0xfb>
mov    %eax,%ebx
mov    0x2deb(%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   1070 <tcgetattr@plt>
mov    %r14,%rdi
call   1060 <cfmakeraw@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <cfsetspeed@plt>
mov    (%rsp),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x80,(%rax,%rcx,4)
mov    0x2d95(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2ddc(%rip),%rax
mov    0x2d65(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2da2(%rip),%rax
or     (%rax),%ecx
mov    0x2d59(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    0x2d9a(%rip),%rax
mov    0x2d33(%rip),%rdx
mov    0x2d94(%rip),%rsi
or     (%rax),%ecx
or     (%rdx),%ecx
or     (%rsi),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
jmp    1270 <open_port+0x100>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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
@F_SETFL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CRTSCTS = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @open_port(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%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
store i32 %13, i32* %8
%14 = load i8*, i8** %4
%15 = load i32, i32* %8
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
br label %62
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i32, i32* @F_SETFL
%22 = call i32 @fcntl(i32 %20, i32 %21, i32 0)
%23 = load i32, i32* %6
%24 = call i32 @tcgetattr(i32 %23, %struct.termios* %7)
%25 = call i32 @cfmakeraw(%struct.termios* %7)
%26 = load i32, i32* %5
%27 = call i32 @cfsetspeed(%struct.termios* %7, i32 %26)
%28 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%29 = load i32*, i32** %28
%30 = load i64, i64* @VMIN
%31 = getelementptr inbounds i32, i32* %29, i64 %30
store i32 128, i32* %31
%32 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%33 = load i32*, i32** %32
%34 = load i64, i64* @VTIME
%35 = getelementptr inbounds i32, i32* %33, i64 %34
store i32 1, i32* %35
%36 = load i32, i32* @PARENB
%37 = load i32, i32* @CSTOPB
%38 = or i32 %36, %37
%39 = load i32, i32* @CSIZE
%40 = or i32 %38, %39
%41 = load i32, i32* @CRTSCTS
%42 = or i32 %40, %41
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* @CS8
%48 = load i32, i32* @CLOCAL
%49 = or i32 %47, %48
%50 = load i32, i32* @CREAD
%51 = or i32 %49, %50
%52 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%53 = load i32, i32* %52
%54 = or i32 %53, %51
store i32 %54, i32* %52
%55 = load i32, i32* %6
%56 = load i32, i32* @TCIFLUSH
%57 = call i32 @tcflush(i32 %55, i32 %56)
%58 = load i32, i32* %6
%59 = load i32, i32* @TCSANOW
%60 = call i32 @tcsetattr(i32 %58, i32 %59, %struct.termios* %7)
%61 = load i32, i32* %6
store i32 %61, i32* %3
br label %62
62: ; preds = %19, %18
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@F_SETFL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CRTSCTS = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @open_port(i8* nocapture %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 slt i32 %10, 0
br i1 %11, label %45, label %12
12: ; preds = %2
%13 = load i32, i32* @F_SETFL
%14 = tail call i32 @fcntl(i32 %10, i32 %13, i32 0)
%15 = call i32 @tcgetattr(i32 %10, %struct.termios* %3)
%16 = call i32 @cfmakeraw(%struct.termios* %3)
%17 = call i32 @cfsetspeed(%struct.termios* %3, i32 %1)
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%19 = load i32*, i32** %18
%20 = load i64, i64* @VMIN
%21 = getelementptr inbounds i32, i32* %19, i64 %20
store i32 128, i32* %21
%22 = load i64, i64* @VTIME
%23 = getelementptr inbounds i32, i32* %19, i64 %22
store i32 1, i32* %23
%24 = load i32, i32* @PARENB
%25 = load i32, i32* @CSTOPB
%26 = or i32 %25, %24
%27 = load i32, i32* @CSIZE
%28 = or i32 %26, %27
%29 = load i32, i32* @CRTSCTS
%30 = or i32 %28, %29
%31 = xor i32 %30, -1
%32 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%33 = load i32, i32* %32
%34 = and i32 %33, %31
%35 = load i32, i32* @CS8
%36 = load i32, i32* @CLOCAL
%37 = load i32, i32* @CREAD
%38 = or i32 %35, %34
%39 = or i32 %38, %36
%40 = or i32 %39, %37
store i32 %40, i32* %32
%41 = load i32, i32* @TCIFLUSH
%42 = call i32 @tcflush(i32 %10, i32 %41)
%43 = load i32, i32* @TCSANOW
%44 = call i32 @tcsetattr(i32 %10, i32 %43, %struct.termios* %3)
br label %45
45: ; preds = %2, %12
%46 = phi i32 [ %10, %12 ], [ -1, %2 ]
ret i32 %46
}