#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 /*<<< 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);
  else
    t.c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
  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    11e6 <setecho+0xc6>
cmpl   $0x0,-0xc(%rbp)
jne    118b <setecho+0x6b>
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
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11b5 <setecho+0x95>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    0x2e3d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e24(%rip),%rcx
or     (%rcx),%eax
mov    0x2e13(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11df <setecho+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <setecho+0xc6>
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     1161 <setecho+0x41>
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
test   %ebp,%ebp
je     1168 <setecho+0x48>
or     %eax,(%rsp)
jmp    116d <setecho+0x4d>
mov    $0xffffffff,%eax
jmp    118c <setecho+0x6c>
not    %eax
and    %eax,(%rsp)
mov    0x2e74(%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
@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 %44
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %26
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 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
br label %37
26: ; preds = %11
%27 = load i32, i32* @ECHO
%28 = load i32, i32* @ECHOE
%29 = or i32 %27, %28
%30 = load i32, i32* @ECHOK
%31 = or i32 %29, %30
%32 = load i32, i32* @ECHONL
%33 = or i32 %31, %32
%34 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = or i32 %35, %33
store i32 %36, i32* %34
br label %37
37: ; preds = %26, %14
%38 = load i32, i32* %4
%39 = load i32, i32* @TCSANOW
%40 = call i32 @tcsetattr(i32 %38, i32 %39, %struct.termios* %6)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 -1, i32* %3
br label %44
43: ; preds = %37
store i32 0, i32* %3
br label %44
44: ; preds = %43, %42, %10
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = 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 %30, 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
br i1 %8, label %16, label %21
16: ; preds = %7
%17 = xor i32 %15, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
br label %25
21: ; preds = %7
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = or i32 %15, %23
store i32 %24, i32* %22
br label %25
25: ; preds = %21, %16
%26 = load i32, i32* @TCSANOW
%27 = call i32 @tcsetattr(i32 %0, i32 %26, %struct.termios* %3)
%28 = icmp eq i32 %27, -1
%29 = sext i1 %28 to i32
br label %30
30: ; preds = %25, %2
%31 = phi i32 [ -1, %2 ], [ %29, %25 ]
ret i32 %31
}