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

/* Variables and functions */
int B115200;
int CREAD;
int CRTSCTS;
int CS8;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ close(int);
int lock_try(char const *);
int open(char const *, int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int opentty(const char *dev) {
  int pid;
  int fd;
  struct termios term_attr;
  fd = open(dev, O_RDWR | O_NOCTTY);
  if (fd < 0) {
    return -1;
  }
  if (tcgetattr(fd, &term_attr) != 0) {
    close(fd);
    return -1;
  }
  term_attr.c_cflag = B115200 | CS8 | CREAD | CRTSCTS;
  term_attr.c_iflag = 0;
  term_attr.c_oflag = 0;
  term_attr.c_lflag = 0;
  term_attr.c_cc[VMIN] = 1;
  term_attr.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSAFLUSH, &term_attr) != 0) {
  }
  pid = lock_try(dev);
  if (pid != 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<opentty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1190 <opentty+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <opentty+0x127>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     11ba <opentty+0x6a>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <opentty+0x127>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    0x2e06(%rip),%rcx
or     (%rcx),%eax
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    0x2dbc(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x18(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     1247 <opentty+0xf7>
jmp    1247 <opentty+0xf7>
mov    -0x10(%rbp),%rdi
call   1030 <lock_try@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1271 <opentty+0x121>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <opentty+0x127>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opentty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
mov    0x2e86(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     121f <opentty+0xcf>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
jne    1218 <opentty+0xc8>
mov    0x2e12(%rip),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e28(%rip),%rax
or     (%rax),%ecx
mov    0x2ddf(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dbb(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1060 <tcsetattr@plt>
mov    %rbx,%rdi
call   1030 <lock_try@plt>
test   %eax,%eax
je     122b <opentty+0xdb>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebp,%r14d
jmp    121f <opentty+0xcf>

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

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