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

/* Variables and functions */
int ECHO;
int ECHONL;
int /*<<< orphan*/ TCSAFLUSH;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int ast_restore_tty(int fd, int oldstate) {
  int res;
  struct termios tios;
  if (oldstate < 0)
    return 0;
  res = tcgetattr(fd, &tios);
  if (res < 0)
    return -1;
  tios.c_lflag &= ~(ECHO | ECHONL);
  tios.c_lflag |= oldstate;
  res = tcsetattr(fd, TCSAFLUSH, &tios);
  if (res < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<ast_restore_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1144 <ast_restore_tty+0x24>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <ast_restore_tty+0xa2>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <tcgetattr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1169 <ast_restore_tty+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <ast_restore_tty+0xa2>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    0x2e57(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11bb <ast_restore_tty+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <ast_restore_tty+0xa2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ast_restore_tty>:
push   %rbp
push   %rbx
push   %rax
test   %esi,%esi
js     116b <ast_restore_tty+0x4b>
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
test   %eax,%eax
js     116f <ast_restore_tty+0x4f>
mov    0x2ea2(%rip),%rax
mov    0x2e8b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
or     %ebp,%ecx
mov    %ecx,(%rsp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <tcsetattr@plt>
sar    $0x1f,%eax
jmp    1174 <ast_restore_tty+0x54>
xor    %eax,%eax
jmp    1174 <ast_restore_tty+0x54>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
define i32 @ast_restore_tty(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 0, i32* %3
br label %36
11: ; preds = %2
%12 = load i32, i32* %4
%13 = call i32 @tcgetattr(i32 %12, %struct.termios* %7)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 -1, i32* %3
br label %36
17: ; preds = %11
%18 = load i32, i32* @ECHO
%19 = load i32, i32* @ECHONL
%20 = or i32 %18, %19
%21 = xor i32 %20, -1
%22 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = and i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* %5
%26 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%27 = load i32, i32* %26
%28 = or i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* %4
%30 = load i32, i32* @TCSAFLUSH
%31 = call i32 @tcsetattr(i32 %29, i32 %30, %struct.termios* %7)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %17
store i32 -1, i32* %3
br label %36
35: ; preds = %17
store i32 0, i32* %3
br label %36
36: ; preds = %35, %34, %16, %10
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
define i32 @ast_restore_tty(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = icmp slt i32 %1, 0
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %6
%10 = load i32, i32* @ECHO
%11 = load i32, i32* @ECHONL
%12 = or i32 %11, %10
%13 = xor i32 %12, -1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
%17 = or i32 %16, %1
store i32 %17, i32* %14
%18 = load i32, i32* @TCSAFLUSH
%19 = call i32 @tcsetattr(i32 %0, i32 %18, %struct.termios* %3)
%20 = ashr i32 %19, 31
br label %21
21: ; preds = %9, %6, %2
%22 = phi i32 [ 0, %2 ], [ -1, %6 ], [ %20, %9 ]
ret i32 %22
}