#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_iflag;
  int c_oflag;
  int c_cflag;
  int c_lflag;
  int *c_cc;
};

/* Variables and functions */
int B9600;
int CBAUD;
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int ECHO;
int ECHOCTL;
int ECHOE;
int ECHOK;
int ECHOKE;
int ECHONL;
int /*<<< orphan*/ F_OK;
int ICANON;
int ICRNL;
int IEXTEN;
int INLCR;
int ISIG;
int IXOFF;
int IXON;
int OCRNL;
int ONLCR;
int ONLRET;
int OPOST;
int O_NONBLOCK;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int tty_open(char *name, struct termios *sv_tios) {
  int ret;
  int fd;
  struct termios tios;
  if (access(name, F_OK) || (fd = open(name, O_RDWR | O_NONBLOCK)) < 0) {
    return -1;
  }
  if ((ret = tcgetattr(fd, &tios)) < 0) {
    close(fd);
    return -1;
  }
  *sv_tios = tios;
  tios.c_iflag &= ~(IXON | IXOFF | INLCR | ICRNL);
  tios.c_oflag &= ~(OPOST | ONLCR | OCRNL);
  tios.c_oflag |= ONLRET;
  tios.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | PARENB);
  tios.c_cflag |= CLOCAL | CREAD | B9600 | CS8;
  tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOCTL |
                    ISIG | IEXTEN);
  tios.c_cc[VMIN] = 0;
  tios.c_cc[VTIME] = 10;
  if ((ret = tcsetattr(fd, TCSAFLUSH, &tios)) < 0) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tty_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    11a3 <tty_open+0x53>
mov    -0x10(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11af <tty_open+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1364 <tty_open+0x214>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11db <tty_open+0x8b>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1364 <tty_open+0x214>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rcx
mov    %rcx,0x10(%rax)
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    0x2dba(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc1(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc0(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    0x2cf5(%rip),%rcx
or     (%rcx),%eax
mov    0x2db4(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2ca3(%rip),%rax
mov    (%rax),%eax
mov    0x2cf2(%rip),%rcx
or     (%rcx),%eax
mov    0x2c89(%rip),%rcx
or     (%rcx),%eax
mov    0x2d20(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c5e(%rip),%rax
mov    (%rax),%eax
mov    0x2cdd(%rip),%rcx
or     (%rcx),%eax
mov    0x2cdc(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc3(%rip),%rcx
or     (%rcx),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c5c(%rip),%rax
mov    (%rax),%eax
mov    0x2cd3(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc2(%rip),%rcx
or     (%rcx),%eax
mov    0x2c69(%rip),%rcx
or     (%rcx),%eax
mov    0x2c68(%rip),%rcx
or     (%rcx),%eax
mov    0x2c4f(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc6(%rip),%rcx
or     (%rcx),%eax
mov    0x2c8d(%rip),%rcx
or     (%rcx),%eax
mov    0x2c54(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2c4e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2bf9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    -0x20(%rbp),%edi
mov    0x2c8d(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    135e <tty_open+0x20e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1364 <tty_open+0x214>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1185 <tty_open+0x35>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e54(%rip),%rax
mov    0x2e25(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1178 <tty_open+0x28>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
call   1040 <tcgetattr@plt>
test   %eax,%eax
js     1314 <tty_open+0x1c4>
mov    0x10(%rsp),%rax
mov    %rax,0x10(%r15)
movups (%rsp),%xmm0
movups %xmm0,(%r15)
mov    0x2dd0(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dee(%rip),%rax
or     (%rax),%ecx
mov    0x2ded(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2daf(%rip),%rax
mov    0x2d28(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de5(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    0x4(%rsp),%ecx
mov    0x2cfe(%rip),%rax
or     (%rax),%ecx
mov    0x2cdd(%rip),%rdx
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
or     (%rdx),%eax
mov    0x2cc3(%rip),%rdx
or     (%rdx),%eax
mov    %ecx,0x4(%rsp)
mov    0x2d56(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2c97(%rip),%rcx
mov    0x2d18(%rip),%rdx
mov    0x2d19(%rip),%rsi
or     (%rcx),%eax
or     (%rdx),%eax
or     (%rsi),%eax
mov    0x2cfc(%rip),%rcx
or     (%rcx),%eax
mov    0x2c9b(%rip),%rcx
mov    0x2d14(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d01(%rip),%rcx
or     (%rcx),%edx
mov    0x2ca8(%rip),%rcx
or     (%rcx),%edx
mov    0x2ca7(%rip),%rcx
or     (%rcx),%edx
mov    0x2c8e(%rip),%rcx
or     (%rcx),%edx
mov    0x2d05(%rip),%rcx
or     (%rcx),%edx
mov    %eax,0x8(%rsp)
mov    0x2cc8(%rip),%rax
or     (%rax),%edx
mov    0x2c8f(%rip),%rax
or     (%rax),%edx
not    %edx
and    %edx,0xc(%rsp)
mov    0x10(%rsp),%rax
mov    0x2c8b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2c3a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    0x2cd1(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1050 <tcsetattr@plt>
test   %eax,%eax
mov    $0xffffffff,%ebp
cmovns %r14d,%ebp
jmp    1178 <tty_open+0x28>
mov    %r14d,%edi
call   1030 <close@plt>
jmp    1178 <tty_open+0x28>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32, i32* }
@F_OK = global i32 0
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@INLCR = global i32 0
@ICRNL = global i32 0
@OPOST = global i32 0
@ONLCR = global i32 0
@OCRNL = global i32 0
@ONLRET = global i32 0
@CBAUD = global i32 0
@CSIZE = global i32 0
@CSTOPB = global i32 0
@PARENB = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@B9600 = global i32 0
@CS8 = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHOKE = global i32 0
@ECHONL = global i32 0
@ECHOCTL = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define i32 @tty_open(i8* %0, %struct.termios* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.termios*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
store i8* %0, i8** %4
store %struct.termios* %1, %struct.termios** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @F_OK
%11 = call i64 @access(i8* %9, i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
store i32 %18, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13, %2
store i32 -1, i32* %3
br label %113
21: ; preds = %13
%22 = load i32, i32* %7
%23 = call i32 @tcgetattr(i32 %22, %struct.termios* %8)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %113
28: ; preds = %21
%29 = load %struct.termios*, %struct.termios** %5
%30 = bitcast %struct.termios* %29 to i8*
%31 = bitcast %struct.termios* %8 to i8*
%32 = load i32, i32* @IXON
%33 = load i32, i32* @IXOFF
%34 = or i32 %32, %33
%35 = load i32, i32* @INLCR
%36 = or i32 %34, %35
%37 = load i32, i32* @ICRNL
%38 = or i32 %36, %37
%39 = xor i32 %38, -1
%40 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, %39
store i32 %42, i32* %40
%43 = load i32, i32* @OPOST
%44 = load i32, i32* @ONLCR
%45 = or i32 %43, %44
%46 = load i32, i32* @OCRNL
%47 = or i32 %45, %46
%48 = xor i32 %47, -1
%49 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 1
%50 = load i32, i32* %49
%51 = and i32 %50, %48
store i32 %51, i32* %49
%52 = load i32, i32* @ONLRET
%53 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 1
%54 = load i32, i32* %53
%55 = or i32 %54, %52
store i32 %55, i32* %53
%56 = load i32, i32* @CBAUD
%57 = load i32, i32* @CSIZE
%58 = or i32 %56, %57
%59 = load i32, i32* @CSTOPB
%60 = or i32 %58, %59
%61 = load i32, i32* @PARENB
%62 = or i32 %60, %61
%63 = xor i32 %62, -1
%64 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 2
%65 = load i32, i32* %64
%66 = and i32 %65, %63
store i32 %66, i32* %64
%67 = load i32, i32* @CLOCAL
%68 = load i32, i32* @CREAD
%69 = or i32 %67, %68
%70 = load i32, i32* @B9600
%71 = or i32 %69, %70
%72 = load i32, i32* @CS8
%73 = or i32 %71, %72
%74 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 2
%75 = load i32, i32* %74
%76 = or i32 %75, %73
store i32 %76, i32* %74
%77 = load i32, i32* @ICANON
%78 = load i32, i32* @ECHO
%79 = or i32 %77, %78
%80 = load i32, i32* @ECHOE
%81 = or i32 %79, %80
%82 = load i32, i32* @ECHOK
%83 = or i32 %81, %82
%84 = load i32, i32* @ECHOKE
%85 = or i32 %83, %84
%86 = load i32, i32* @ECHONL
%87 = or i32 %85, %86
%88 = load i32, i32* @ECHOCTL
%89 = or i32 %87, %88
%90 = load i32, i32* @ISIG
%91 = or i32 %89, %90
%92 = load i32, i32* @IEXTEN
%93 = or i32 %91, %92
%94 = xor i32 %93, -1
%95 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 3
%96 = load i32, i32* %95
%97 = and i32 %96, %94
store i32 %97, i32* %95
%98 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 4
%99 = load i32*, i32** %98
%100 = load i64, i64* @VMIN
%101 = getelementptr inbounds i32, i32* %99, i64 %100
store i32 0, i32* %101
%102 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 4
%103 = load i32*, i32** %102
%104 = load i64, i64* @VTIME
%105 = getelementptr inbounds i32, i32* %103, i64 %104
store i32 10, i32* %105
%106 = load i32, i32* %7
%107 = load i32, i32* @TCSAFLUSH
%108 = call i32 @tcsetattr(i32 %106, i32 %107, %struct.termios* %8)
store i32 %108, i32* %6
%109 = icmp slt i32 %108, 0
br i1 %109, label %110, label %111
110: ; preds = %28
store i32 -1, i32* %3
br label %113
111: ; preds = %28
%112 = load i32, i32* %7
store i32 %112, i32* %3
br label %113
113: ; preds = %111, %110, %25, %20
%114 = load i32, i32* %3
ret i32 %114
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32, i32* }
@F_OK = global i32 0
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@INLCR = global i32 0
@ICRNL = global i32 0
@OPOST = global i32 0
@ONLCR = global i32 0
@OCRNL = global i32 0
@ONLRET = global i32 0
@CBAUD = global i32 0
@CSIZE = global i32 0
@CSTOPB = global i32 0
@PARENB = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@B9600 = global i32 0
@CS8 = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHOKE = global i32 0
@ECHONL = global i32 0
@ECHOCTL = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define i32 @tty_open(i8* nocapture %0, %struct.termios* nocapture writeonly %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = load i32, i32* @F_OK
%6 = tail call i64 @access(i8* %0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %93
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %93, label %14
14: ; preds = %8
%15 = call i32 @tcgetattr(i32 %12, %struct.termios* %3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @close(i32 %12)
br label %93
19: ; preds = %14
%20 = bitcast %struct.termios* %1 to i8*
%21 = load i32, i32* @IXON
%22 = load i32, i32* @IXOFF
%23 = or i32 %22, %21
%24 = load i32, i32* @INLCR
%25 = or i32 %23, %24
%26 = load i32, i32* @ICRNL
%27 = or i32 %25, %26
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%30 = load i32, i32* %29
%31 = and i32 %30, %28
store i32 %31, i32* %29
%32 = load i32, i32* @OPOST
%33 = load i32, i32* @ONLCR
%34 = or i32 %33, %32
%35 = load i32, i32* @OCRNL
%36 = or i32 %34, %35
%37 = xor i32 %36, -1
%38 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%39 = load i32, i32* %38
%40 = and i32 %39, %37
%41 = load i32, i32* @ONLRET
%42 = or i32 %41, %40
store i32 %42, i32* %38
%43 = load i32, i32* @CBAUD
%44 = load i32, i32* @CSIZE
%45 = or i32 %44, %43
%46 = load i32, i32* @CSTOPB
%47 = or i32 %45, %46
%48 = load i32, i32* @PARENB
%49 = or i32 %47, %48
%50 = xor i32 %49, -1
%51 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%52 = load i32, i32* %51
%53 = and i32 %52, %50
%54 = load i32, i32* @CLOCAL
%55 = load i32, i32* @CREAD
%56 = load i32, i32* @B9600
%57 = load i32, i32* @CS8
%58 = or i32 %54, %53
%59 = or i32 %58, %55
%60 = or i32 %59, %56
%61 = or i32 %60, %57
store i32 %61, i32* %51
%62 = load i32, i32* @ICANON
%63 = load i32, i32* @ECHO
%64 = or i32 %63, %62
%65 = load i32, i32* @ECHOE
%66 = or i32 %64, %65
%67 = load i32, i32* @ECHOK
%68 = or i32 %66, %67
%69 = load i32, i32* @ECHOKE
%70 = or i32 %68, %69
%71 = load i32, i32* @ECHONL
%72 = or i32 %70, %71
%73 = load i32, i32* @ECHOCTL
%74 = or i32 %72, %73
%75 = load i32, i32* @ISIG
%76 = or i32 %74, %75
%77 = load i32, i32* @IEXTEN
%78 = or i32 %76, %77
%79 = xor i32 %78, -1
%80 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%81 = load i32, i32* %80
%82 = and i32 %81, %79
store i32 %82, i32* %80
%83 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 4
%84 = load i32*, i32** %83
%85 = load i64, i64* @VMIN
%86 = getelementptr inbounds i32, i32* %84, i64 %85
store i32 0, i32* %86
%87 = load i64, i64* @VTIME
%88 = getelementptr inbounds i32, i32* %84, i64 %87
store i32 10, i32* %88
%89 = load i32, i32* @TCSAFLUSH
%90 = call i32 @tcsetattr(i32 %12, i32 %89, %struct.termios* %3)
%91 = icmp sgt i32 %90, -1
%92 = select i1 %91, i32 %12, i32 -1
br label %93
93: ; preds = %19, %2, %8, %17
%94 = phi i32 [ -1, %17 ], [ -1, %8 ], [ -1, %2 ], [ %92, %19 ]
ret i32 %94
}