#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 ECHOE;
int ECHOK;
int ECHONL;
int ICANON;
int /*<<< orphan*/ TCSANOW;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int setecho(int fd, int onoff) {
  struct termios t;
  if (tcgetattr(fd, &t) == -1)
    return -1;
  if (onoff == 0)
    t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON);
  else
    t.c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL | ICANON);
  if (tcsetattr(fd, TCSANOW, &t) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<setecho>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    114f <setecho+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <setecho+0xd8>
cmpl   $0x0,-0xc(%rbp)
jne    1194 <setecho+0x74>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    0x2e6f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e56(%rip),%rcx
or     (%rcx),%eax
mov    0x2e45(%rip),%rcx
or     (%rcx),%eax
mov    0x2e34(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11c7 <setecho+0xa7>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    0x2e34(%rip),%rcx
or     (%rcx),%eax
mov    0x2e1b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0a(%rip),%rcx
or     (%rcx),%eax
mov    0x2df9(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11f1 <setecho+0xd1>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <setecho+0xd8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setecho>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     116a <setecho+0x4a>
mov    0x2ea5(%rip),%rcx
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e7b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e6a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e59(%rip),%rcx
or     (%rcx),%eax
test   %ebp,%ebp
je     1171 <setecho+0x51>
or     %eax,(%rsp)
jmp    1176 <setecho+0x56>
mov    $0xffffffff,%eax
jmp    1195 <setecho+0x75>
not    %eax
and    %eax,(%rsp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
define i32 @setecho(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %48
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %11
%15 = load i32, i32* @ECHO
%16 = load i32, i32* @ECHOE
%17 = or i32 %15, %16
%18 = load i32, i32* @ECHOK
%19 = or i32 %17, %18
%20 = load i32, i32* @ECHONL
%21 = or i32 %19, %20
%22 = load i32, i32* @ICANON
%23 = or i32 %21, %22
%24 = xor i32 %23, -1
%25 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = and i32 %26, %24
store i32 %27, i32* %25
br label %41
28: ; preds = %11
%29 = load i32, i32* @ECHO
%30 = load i32, i32* @ECHOE
%31 = or i32 %29, %30
%32 = load i32, i32* @ECHOK
%33 = or i32 %31, %32
%34 = load i32, i32* @ECHONL
%35 = or i32 %33, %34
%36 = load i32, i32* @ICANON
%37 = or i32 %35, %36
%38 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%39 = load i32, i32* %38
%40 = or i32 %39, %37
store i32 %40, i32* %38
br label %41
41: ; preds = %28, %14
%42 = load i32, i32* %4
%43 = load i32, i32* @TCSANOW
%44 = call i32 @tcsetattr(i32 %42, i32 %43, %struct.termios* %6)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 -1, i32* %3
br label %48
47: ; preds = %41
store i32 0, i32* %3
br label %48
48: ; preds = %47, %46, %10
%49 = load i32, i32* %3
ret i32 %49
}

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