#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 */
typedef int /*<<< orphan*/ tio;
struct termios {
  int c_cflag;
  int *c_cc;
  scalar_t__ c_iflag;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int CLOCAL;
int CREAD;
int CS8;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char const *, int);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int kwboot_open_tty(const char *path, speed_t speed) {
  int rc, fd;
  struct termios tio;
  rc = -1;
  fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd < 0)
    goto out;
  memset(&tio, 0, sizeof(tio));
  tio.c_iflag = 0;
  tio.c_cflag = CREAD | CLOCAL | CS8;
  tio.c_cc[VMIN] = 1;
  tio.c_cc[VTIME] = 10;
  cfsetospeed(&tio, speed);
  cfsetispeed(&tio, speed);
  rc = tcsetattr(fd, TCSANOW, &tio);
  if (rc)
    goto out;
  rc = fd;
out:
  if (rc < 0) {
    if (fd >= 0)
      close(fd);
  }
  return rc;
}


x86 assembly (clang, -O0):
<kwboot_open_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ac <kwboot_open_tty+0x4c>
jmp    1251 <kwboot_open_tty+0xf1>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
movq   $0x0,-0x20(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    0x2dc4(%rip),%rcx
or     (%rcx),%eax
mov    0x2df3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2da6(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x14(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     124b <kwboot_open_tty+0xeb>
jmp    1251 <kwboot_open_tty+0xf1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1272 <kwboot_open_tty+0x112>
cmpl   $0x0,-0x14(%rbp)
jl     126d <kwboot_open_tty+0x10d>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1272 <kwboot_open_tty+0x112>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_open_tty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e79(%rip),%rcx
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%ebp
mov    0x2e2d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1070 <open@plt>
test   %eax,%eax
js     1234 <kwboot_open_tty+0xd4>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e27(%rip),%rax
mov    0x2de0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
movq   $0x0,0x18(%rsp)
mov    0x2e04(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dea(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2db9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1080 <cfsetispeed@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %eax,%ebp
test   %eax,%eax
cmove  %ebx,%ebp
test   %ebp,%ebp
jns    1239 <kwboot_open_tty+0xd9>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1239 <kwboot_open_tty+0xd9>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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