#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 /*<<< orphan*/ c_cflag;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
  scalar_t__ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ CLOCAL;
int /*<<< orphan*/ CREAD;
int /*<<< orphan*/ CS8;
int /*<<< orphan*/ HUPCL;
scalar_t__ OPOST;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ close(int);
int open(char const *, int);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_open(const char *tty, int flags) {
  struct termios tp;
  int fd;
  if ((fd = open(tty, flags)) == -1)
    goto err;
  if (tcgetattr(fd, &tp) == -1)
    goto tc_err;
  tp.c_lflag = 0;
  tp.c_iflag = 0;
  tp.c_oflag = 0;
  tp.c_cflag = 0;
  tp.c_cflag |= B115200;
  tp.c_cflag |= CS8;
  tp.c_cflag |= CLOCAL;
  tp.c_cflag |= CREAD;
  tp.c_oflag = OPOST;
  tp.c_cflag |= HUPCL;
  tp.c_cc[VMIN] = 1;
  tp.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSANOW, &tp) == -1)
    goto tc_err;
  return (fd);
tc_err:
  close(fd);
err:
  return (-1);
}


x86 assembly (clang, -O0):
<serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
jne    116c <serial_open+0x2c>
jmp    125e <serial_open+0x11e>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1186 <serial_open+0x46>
jmp    1256 <serial_open+0x116>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2dbf(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x40(%rbp),%rax
mov    0x2d92(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x44(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    124b <serial_open+0x10b>
jmp    1256 <serial_open+0x116>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1265 <serial_open+0x125>
mov    -0x44(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
call   1060 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11ff <serial_open+0xbf>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1040 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11f8 <serial_open+0xb8>
mov    0x2e43(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xorps  %xmm0,%xmm0
mov    0x2e0e(%rip),%rax
or     (%rax),%ecx
movups %xmm0,0x18(%rsp)
mov    0x2e40(%rip),%rax
or     (%rax),%ecx
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    0x2df8(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    (%rsp),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dde(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     11f8 <serial_open+0xb8>
mov    %ebp,%ebx
jmp    11ff <serial_open+0xbf>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32, i64, i64, i64 }
@B115200 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@OPOST = global i64 0
@HUPCL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @serial_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %7
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
br label %63
13: ; preds = %2
%14 = load i32, i32* %7
%15 = call i32 @tcgetattr(i32 %14, %struct.termios* %6)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %13
br label %60
18: ; preds = %13
%19 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i64 0, i64* %20
%21 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 0, i64* %21
%22 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
store i32 0, i32* %22
%23 = load i32, i32* @B115200
%24 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @CS8
%28 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%29 = load i32, i32* %28
%30 = or i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @CLOCAL
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%33 = load i32, i32* %32
%34 = or i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* @CREAD
%36 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%37 = load i32, i32* %36
%38 = or i32 %37, %35
store i32 %38, i32* %36
%39 = load i64, i64* @OPOST
%40 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 %39, i64* %40
%41 = load i32, i32* @HUPCL
%42 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%43 = load i32, i32* %42
%44 = or i32 %43, %41
store i32 %44, i32* %42
%45 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%46 = load i32*, i32** %45
%47 = load i64, i64* @VMIN
%48 = getelementptr inbounds i32, i32* %46, i64 %47
store i32 1, i32* %48
%49 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%50 = load i32*, i32** %49
%51 = load i64, i64* @VTIME
%52 = getelementptr inbounds i32, i32* %50, i64 %51
store i32 0, i32* %52
%53 = load i32, i32* %7
%54 = load i32, i32* @TCSANOW
%55 = call i32 @tcsetattr(i32 %53, i32 %54, %struct.termios* %6)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %18
br label %60
58: ; preds = %18
%59 = load i32, i32* %7
store i32 %59, i32* %3
br label %64
60: ; preds = %57, %17
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %60, %12
store i32 -1, i32* %3
br label %64
64: ; preds = %63, %58
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32, i64, i64, i64 }
@B115200 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@OPOST = global i64 0
@HUPCL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @serial_open(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = tail call i32 @open(i8* %0, i32 %1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %36, label %7
7: ; preds = %2
%8 = call i32 @tcgetattr(i32 %5, %struct.termios* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %34, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%12 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%13 = getelementptr inbounds i64, i64* %11, i64 1
%14 = bitcast i64* %13 to i8*
%15 = load i32, i32* @B115200
%16 = load i32, i32* @CS8
%17 = or i32 %16, %15
%18 = load i32, i32* @CLOCAL
%19 = or i32 %18, %17
%20 = load i32, i32* @CREAD
%21 = or i32 %20, %19
%22 = load i64, i64* @OPOST
store i64 %22, i64* %11
%23 = load i32, i32* @HUPCL
%24 = or i32 %23, %21
store i32 %24, i32* %12
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%26 = load i32*, i32** %25
%27 = load i64, i64* @VMIN
%28 = getelementptr inbounds i32, i32* %26, i64 %27
store i32 1, i32* %28
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %26, i64 %29
store i32 0, i32* %30
%31 = load i32, i32* @TCSANOW
%32 = call i32 @tcsetattr(i32 %5, i32 %31, %struct.termios* %3)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %10, %7
%35 = call i32 @close(i32 %5)
br label %36
36: ; preds = %34, %2, %10
%37 = phi i32 [ %5, %10 ], [ -1, %2 ], [ -1, %34 ]
ret i32 %37
}