#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 /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int CLOCAL;
int CREAD;
int /*<<< orphan*/ OPOST;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int open(char const *const, int, int);
int /*<<< orphan*/ tcgetattr(int const, struct termios *);
int /*<<< orphan*/ tcsetattr(int const, int /*<<< orphan*/, struct termios *);
int serial_open(const char *const dev) {
  const int fd = open(dev, O_RDWR | O_NONBLOCK | O_NOCTTY, 0666);
  if (fd < 0)
    return -1;
  struct termios attr;
  tcgetattr(fd, &attr);
  attr.c_cflag |= CLOCAL | CREAD;
  attr.c_oflag &= ~OPOST;
  tcsetattr(fd, TCSANOW, &attr);
  return fd;
}


x86 assembly (clang, -O0):
<serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117e <serial_open+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <serial_open+0x9f>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <tcgetattr@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    0x2e2e(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open>:
push   %r14
push   %rbx
push   %rax
mov    0x2ead(%rip),%rax
mov    0x2e9e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6b(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11a5 <serial_open+0x75>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1030 <tcgetattr@plt>
mov    0x2e35(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,(%rsp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x4(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <tcsetattr@plt>
jmp    11aa <serial_open+0x7a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@OPOST = global i32 0
@TCSANOW = global i32 0
define i32 @serial_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11, i32 438)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %34
16: ; preds = %1
%17 = load i32, i32* %4
%18 = call i32 @tcgetattr(i32 %17, %struct.termios* %5)
%19 = load i32, i32* @CLOCAL
%20 = load i32, i32* @CREAD
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%23 = load i32, i32* %22
%24 = or i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* @OPOST
%26 = xor i32 %25, -1
%27 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%28 = load i32, i32* %27
%29 = and i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* %4
%31 = load i32, i32* @TCSANOW
%32 = call i32 @tcsetattr(i32 %30, i32 %31, %struct.termios* %5)
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %34
34: ; preds = %16, %15
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@OPOST = global i32 0
@TCSANOW = global i32 0
define i32 @serial_open(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NONBLOCK
%5 = or i32 %4, %3
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7, i32 438)
%9 = icmp slt i32 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %1
%11 = bitcast %struct.termios* %2 to i8*
%12 = call i32 @tcgetattr(i32 %8, %struct.termios* %2)
%13 = load i32, i32* @CLOCAL
%14 = load i32, i32* @CREAD
%15 = or i32 %14, %13
%16 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = or i32 %15, %17
store i32 %18, i32* %16
%19 = load i32, i32* @OPOST
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%22 = load i32, i32* %21
%23 = and i32 %22, %20
store i32 %23, i32* %21
%24 = load i32, i32* @TCSANOW
%25 = call i32 @tcsetattr(i32 %8, i32 %24, %struct.termios* %2)
br label %26
26: ; preds = %1, %10
%27 = phi i32 [ %8, %10 ], [ -1, %1 ]
ret i32 %27
}