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

/* Variables and functions */
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ OPOST;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ close(int);
int open(char const *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_open(const char *device_name) {
  int fd = open(device_name, O_RDWR | O_NDELAY | O_SYNC | O_NOCTTY);
  if (fd < 0)
    return fd;
  struct termios tio = {0};
  tcgetattr(fd, &tio);
  tio.c_cc[VMIN] = 4;
  tio.c_cc[VTIME] = 5;
  tio.c_oflag &= ~OPOST;
  tio.c_cflag |= CLOCAL | CS8 | CREAD;
  if (tcsetattr(fd, TCSANOW, &tio)) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a1 <serial_open+0x51>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1253 <serial_open+0x103>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x30(%rbp),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x4,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2dbb(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    0x2db7(%rip),%rcx
or     (%rcx),%eax
mov    0x2db6(%rip),%rcx
or     (%rcx),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     124d <serial_open+0xfd>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <serial_open+0x103>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e9a(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     120f <serial_open+0xcf>
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    (%rsp),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x4,(%rax,%rcx,4)
mov    0x2de5(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0xc(%rsp)
mov    0x2db5(%rip),%rax
mov    0x2de6(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de3(%rip),%rax
or     (%rax),%ecx
or     %ecx,0x8(%rsp)
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
test   %rax,%rax
je     120f <serial_open+0xcf>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32, i32, i32 }
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@O_SYNC = global i32 0
@O_NOCTTY = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@OPOST = global i32 0
@CLOCAL = global i32 0
@CS8 = global i32 0
@CREAD = 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_NDELAY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_SYNC
%11 = or i32 %9, %10
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %6, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = load i32, i32* %4
store i32 %18, i32* %2
br label %53
19: ; preds = %1
%20 = bitcast %struct.termios* %5 to i8*
%21 = load i32, i32* %4
%22 = call i32 @tcgetattr(i32 %21, %struct.termios* %5)
%23 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%24 = load i32*, i32** %23
%25 = load i64, i64* @VMIN
%26 = getelementptr inbounds i32, i32* %24, i64 %25
store i32 4, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%28 = load i32*, i32** %27
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 5, i32* %30
%31 = load i32, i32* @OPOST
%32 = xor i32 %31, -1
%33 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
%34 = load i32, i32* %33
%35 = and i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* @CLOCAL
%37 = load i32, i32* @CS8
%38 = or i32 %36, %37
%39 = load i32, i32* @CREAD
%40 = or i32 %38, %39
%41 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%42 = load i32, i32* %41
%43 = or i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* %4
%45 = load i32, i32* @TCSANOW
%46 = call i64 @tcsetattr(i32 %44, i32 %45, %struct.termios* %5)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %19
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %2
br label %53
51: ; preds = %19
%52 = load i32, i32* %4
store i32 %52, i32* %2
br label %53
53: ; preds = %51, %48, %17
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32, i32, i32 }
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@O_SYNC = global i32 0
@O_NOCTTY = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@OPOST = global i32 0
@CLOCAL = global i32 0
@CS8 = global i32 0
@CREAD = 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_NDELAY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_SYNC
%7 = or i32 %5, %6
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %41, label %12
12: ; preds = %1
%13 = bitcast %struct.termios* %2 to i8*
%14 = call i32 @tcgetattr(i32 %10, %struct.termios* %2)
%15 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%16 = load i32*, i32** %15
%17 = load i64, i64* @VMIN
%18 = getelementptr inbounds i32, i32* %16, i64 %17
store i32 4, i32* %18
%19 = load i64, i64* @VTIME
%20 = getelementptr inbounds i32, i32* %16, i64 %19
store i32 5, i32* %20
%21 = load i32, i32* @OPOST
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%26 = load i32, i32* @CLOCAL
%27 = load i32, i32* @CS8
%28 = or i32 %27, %26
%29 = load i32, i32* @CREAD
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%32 = load i32, i32* %31
%33 = or i32 %30, %32
store i32 %33, i32* %31
%34 = load i32, i32* @TCSANOW
%35 = call i64 @tcsetattr(i32 %10, i32 %34, %struct.termios* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %12
%38 = call i32 @close(i32 %10)
br label %39
39: ; preds = %12, %37
%40 = phi i32 [ -1, %37 ], [ %10, %12 ]
br label %41
41: ; preds = %1, %39
%42 = phi i32 [ %40, %39 ], [ %10, %1 ]
ret i32 %42
}