#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;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ECHO;
int ISIG;
int TCSAFLUSH;
int TCSANOW;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
scalar_t__ setvbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int, struct termios *);
int config_tty(FILE *in, struct termios *prev_tattr) {
  struct termios t;
  if (setvbuf(in, NULL, _IONBF, 0) != 0)
    return -1;
  if (tcgetattr(fileno(in), prev_tattr) == 0) {
    t = *prev_tattr;
    cfmakeraw(&t);
    t.c_lflag &= ~(ECHO | ISIG);
    if (tcsetattr(fileno(in), TCSAFLUSH | 0 | TCSANOW, &t) == 0)
      return 0;
    else
      return -1;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<config_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1050 <setvbuf@plt>
cmp    $0x0,%rax
je     118e <config_tty+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <config_tty+0xdb>
mov    -0x10(%rbp),%rdi
call   1030 <fileno@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
jne    1224 <config_tty+0xd4>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    0x2e02(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fileno@plt>
mov    %eax,%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
mov    0x2de9(%rip),%rax
or     (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
jne    1218 <config_tty+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    122b <config_tty+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <config_tty+0xdb>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_tty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1050 <setvbuf@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11e6 <config_tty+0x96>
mov    %rbx,%rdi
call   1030 <fileno@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
test   %rax,%rax
jne    11e6 <config_tty+0x96>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e2c(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <fileno@plt>
mov    0x2e1c(%rip),%rcx
mov    0x2e0d(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%edi
mov    %rbp,%rdx
call   1070 <tcsetattr@plt>
neg    %rax
sbb    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@_IONBF = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSAFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @config_tty(i32* %0, %struct.termios* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.termios*
%6 = alloca %struct.termios
store i32* %0, i32** %4
store %struct.termios* %1, %struct.termios** %5
%7 = load i32*, i32** %4
%8 = load i32, i32* @_IONBF
%9 = call i64 @setvbuf(i32* %7, i32* null, i32 %8, i32 0)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %41
12: ; preds = %2
%13 = load i32*, i32** %4
%14 = call i32 @fileno(i32* %13)
%15 = load %struct.termios*, %struct.termios** %5
%16 = call i64 @tcgetattr(i32 %14, %struct.termios* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %40
18: ; preds = %12
%19 = load %struct.termios*, %struct.termios** %5
%20 = bitcast %struct.termios* %6 to i8*
%21 = bitcast %struct.termios* %19 to i8*
%22 = call i32 @cfmakeraw(%struct.termios* %6)
%23 = load i32, i32* @ECHO
%24 = load i32, i32* @ISIG
%25 = or i32 %23, %24
%26 = xor i32 %25, -1
%27 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, %26
store i32 %29, i32* %27
%30 = load i32*, i32** %4
%31 = call i32 @fileno(i32* %30)
%32 = load i32, i32* @TCSAFLUSH
%33 = or i32 %32, 0
%34 = load i32, i32* @TCSANOW
%35 = or i32 %33, %34
%36 = call i64 @tcsetattr(i32 %31, i32 %35, %struct.termios* %6)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %18
store i32 0, i32* %3
br label %41
39: ; preds = %18
store i32 -1, i32* %3
br label %41
40: ; preds = %12
store i32 -1, i32* %3
br label %41
41: ; preds = %40, %39, %38, %11
%42 = load i32, i32* %3
ret i32 %42
}

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