#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_lflag;
  int *c_cc;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ _IONBF;
scalar_t__ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/,
                   int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int set_cbreak(struct termios *save_termios) {
  struct termios buf;
  if (setvbuf(stdin, NULL, _IONBF, 0) != 0)
    return -1;
  if (tcgetattr(0, save_termios))
    return -1;
  buf = *save_termios;
  buf.c_lflag &= ~(ECHO | ICANON);
  buf.c_cc[VMIN] = 1;
  buf.c_cc[VTIME] = 0;
  if (tcsetattr(0, TCSAFLUSH, &buf) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<set_cbreak>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1030 <setvbuf@plt>
cmp    $0x0,%rax
je     116f <set_cbreak+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <set_cbreak+0xe9>
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
je     1190 <set_cbreak+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <set_cbreak+0xe9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,-0x20(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    0x2dfd(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2dda(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1212 <set_cbreak+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <set_cbreak+0xe9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cbreak>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e9f(%rip),%rax
mov    (%rax),%edi
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1030 <setvbuf@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11ce <set_cbreak+0x9e>
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
test   %rax,%rax
jne    11ce <set_cbreak+0x9e>
movups (%r14),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e5b(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@stdin = global i32 0
@_IONBF = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define i32 @set_cbreak(%struct.termios* %0) {
%2 = alloca i32
%3 = alloca %struct.termios*
%4 = alloca %struct.termios
store %struct.termios* %0, %struct.termios** %3
%5 = load i32, i32* @stdin
%6 = load i32, i32* @_IONBF
%7 = call i64 @setvbuf(i32 %5, i32* null, i32 %6, i32 0)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %39
10: ; preds = %1
%11 = load %struct.termios*, %struct.termios** %3
%12 = call i64 @tcgetattr(i32 0, %struct.termios* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 -1, i32* %2
br label %39
15: ; preds = %10
%16 = load %struct.termios*, %struct.termios** %3
%17 = bitcast %struct.termios* %4 to i8*
%18 = bitcast %struct.termios* %16 to i8*
%19 = load i32, i32* @ECHO
%20 = load i32, i32* @ICANON
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%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 0, i32* %33
%34 = load i32, i32* @TCSAFLUSH
%35 = call i64 @tcsetattr(i32 0, i32 %34, %struct.termios* %4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %15
store i32 -1, i32* %2
br label %39
38: ; preds = %15
store i32 0, i32* %2
br label %39
39: ; preds = %38, %37, %14, %9
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@stdin = global i32 0
@_IONBF = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define i32 @set_cbreak(%struct.termios* %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @stdin
%5 = load i32, i32* @_IONBF
%6 = tail call i64 @setvbuf(i32 %4, i32* null, i32 %5, i32 0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %30
8: ; preds = %1
%9 = tail call i64 @tcgetattr(i32 0, %struct.termios* %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %8
%12 = bitcast %struct.termios* %0 to i8*
%13 = load i32, i32* @ECHO
%14 = load i32, i32* @ICANON
%15 = or i32 %14, %13
%16 = xor i32 %15, -1
%17 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %18, %16
store i32 %19, i32* %17
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%21 = load i32*, i32** %20
%22 = load i64, i64* @VMIN
%23 = getelementptr inbounds i32, i32* %21, i64 %22
store i32 1, i32* %23
%24 = load i64, i64* @VTIME
%25 = getelementptr inbounds i32, i32* %21, i64 %24
store i32 0, i32* %25
%26 = load i32, i32* @TCSAFLUSH
%27 = call i64 @tcsetattr(i32 0, i32 %26, %struct.termios* %2)
%28 = ashr i64 %27, 63
%29 = trunc i64 %28 to i32
br label %30
30: ; preds = %11, %8, %1
%31 = phi i32 [ -1, %1 ], [ -1, %8 ], [ %29, %11 ]
ret i32 %31
}