#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;
};

/* Variables and functions */
int /*<<< orphan*/ B9600;
int CLOCAL;
int CREAD;
int CS8;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ bzero(struct termios *, int);
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ usleep(int);
int open_tty(char *dev) {
  struct termios tio;
  int fd;
  fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd < 0)
    return -1;
  fprintf(stderr, "open done, trying to set raw\n");
  bzero(&tio, sizeof(tio));
  tio.c_cflag = CS8 | CREAD | CLOCAL;
  tio.c_cc[VMIN] = 1;
  tio.c_cc[VTIME] = 10;
  cfsetspeed(&tio, B9600);
  cfmakeraw(&tio);
  if (tcsetattr(fd, TCSANOW, &tio) < 0)
    fprintf(stderr, "cannot set raw mode\n");
  usleep(500);
  return fd;
}


x86 assembly (clang, -O0):
<open_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b9 <open_tty+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <open_tty+0x113>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
call   1040 <fprintf@plt>
lea    -0x20(%rbp),%rdi
mov    $0x10,%esi
call   1080 <bzero@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    0x2ddc(%rip),%rcx
or     (%rcx),%eax
mov    0x2d93(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2db3(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2d7e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1030 <cfsetspeed@plt>
lea    -0x20(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    -0x24(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1273 <open_tty+0x103>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1f4,%edi
call   1090 <usleep@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tty>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     125a <open_tty+0xea>
mov    %eax,%ebx
mov    0x2df6(%rip),%r15
mov    (%r15),%edi
lea    0xe4c(%rip),%rsi
call   1040 <fprintf@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x10,%esi
call   1080 <bzero@plt>
mov    0x2df0(%rip),%rax
mov    0x2df1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2da6(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2dc5(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d94(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1030 <cfsetspeed@plt>
mov    %r14,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
jns    124e <open_tty+0xde>
mov    (%r15),%edi
lea    0xdd5(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1f4,%edi
call   1090 <usleep@plt>
jmp    125f <open_tty+0xef>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"open done, trying to set raw\0A\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B9600 = global i32 0
@TCSANOW = global i32 0
@.str.1 = [21 x i8] c"cannot set raw mode\0A\00"
define i32 @open_tty(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.termios
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %47
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @bzero(%struct.termios* %4, i32 16)
%20 = load i32, i32* @CS8
%21 = load i32, i32* @CREAD
%22 = or i32 %20, %21
%23 = load i32, i32* @CLOCAL
%24 = or i32 %22, %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%27 = load i32*, i32** %26
%28 = load i64, i64* @VMIN
%29 = getelementptr inbounds i32, i32* %27, i64 %28
store i32 1, i32* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%31 = load i32*, i32** %30
%32 = load i64, i64* @VTIME
%33 = getelementptr inbounds i32, i32* %31, i64 %32
store i32 10, i32* %33
%34 = load i32, i32* @B9600
%35 = call i32 @cfsetspeed(%struct.termios* %4, i32 %34)
%36 = call i32 @cfmakeraw(%struct.termios* %4)
%37 = load i32, i32* %5
%38 = load i32, i32* @TCSANOW
%39 = call i64 @tcsetattr(i32 %37, i32 %38, %struct.termios* %4)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %16
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %44
44: ; preds = %41, %16
%45 = call i32 @usleep(i32 500)
%46 = load i32, i32* %5
store i32 %46, i32* %2
br label %47
47: ; preds = %44, %15
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"open done, trying to set raw\0A\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B9600 = global i32 0
@TCSANOW = global i32 0
@.str.1 = [21 x i8] c"cannot set raw mode\0A\00"
define i32 @open_tty(i8* nocapture %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 = load i32, i32* @O_NONBLOCK
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %38, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @bzero(%struct.termios* %2, i32 16)
%15 = load i32, i32* @CS8
%16 = load i32, i32* @CREAD
%17 = or i32 %16, %15
%18 = load i32, i32* @CLOCAL
%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 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 = load i32, i32* @B9600
%28 = call i32 @cfsetspeed(%struct.termios* %2, i32 %27)
%29 = call i32 @cfmakeraw(%struct.termios* %2)
%30 = load i32, i32* @TCSANOW
%31 = call i64 @tcsetattr(i32 %9, i32 %30, %struct.termios* %2)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %11
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %33, %11
%37 = call i32 @usleep(i32 500)
br label %38
38: ; preds = %1, %36
%39 = phi i32 [ %9, %36 ], [ -1, %1 ]
ret i32 %39
}