#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 /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ warn(char *);
int set_echo(int echo) {
  struct termios localios;
  if (tcgetattr(STDIN_FILENO, &localios)) {
    warn("tcgetattr");
    return -1;
  }
  if (echo) {
    localios.c_lflag |= ECHO;
  } else {
    localios.c_lflag &= ~ECHO;
  }
  if (tcsetattr(STDIN_FILENO, TCSANOW, &localios)) {
    warn("tcsetattr");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
je     116f <set_echo+0x3f>
lea    0xea2(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <set_echo+0xb3>
cmpl   $0x0,-0x8(%rbp)
je     118d <set_echo+0x5d>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    119f <set_echo+0x6f>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
je     11dc <set_echo+0xac>
lea    0xe3f(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <set_echo+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_echo>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e93(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1040 <tcgetattr@plt>
test   %rax,%rax
je     1156 <set_echo+0x26>
lea    0xeac(%rip),%rdi
jmp    118d <set_echo+0x5d>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
test   %ebx,%ebx
je     1168 <set_echo+0x38>
or     %eax,(%rsp)
jmp    116d <set_echo+0x3d>
not    %eax
and    %eax,(%rsp)
mov    (%r14),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1050 <tcsetattr@plt>
test   %rax,%rax
je     119f <set_echo+0x6f>
lea    0xe7d(%rip),%rdi
call   1030 <warn@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
jmp    1197 <set_echo+0x67>

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"tcgetattr\00"
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @set_echo(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %3
%5 = load i32, i32* @STDIN_FILENO
%6 = call i64 @tcgetattr(i32 %5, %struct.termios* %4)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %32
10: ; preds = %1
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i32, i32* @ECHO
%15 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = or i32 %16, %14
store i32 %17, i32* %15
br label %24
18: ; preds = %10
%19 = load i32, i32* @ECHO
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%22 = load i32, i32* %21
%23 = and i32 %22, %20
store i32 %23, i32* %21
br label %24
24: ; preds = %18, %13
%25 = load i32, i32* @STDIN_FILENO
%26 = load i32, i32* @TCSANOW
%27 = call i64 @tcsetattr(i32 %25, i32 %26, %struct.termios* %4)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %32
31: ; preds = %24
store i32 0, i32* %2
br label %32
32: ; preds = %31, %29, %8
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"tcgetattr\00"
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @set_echo(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @STDIN_FILENO
%5 = call i64 @tcgetattr(i32 %4, %struct.termios* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %24
7: ; preds = %1
%8 = icmp eq i32 %0, 0
%9 = load i32, i32* @ECHO
br i1 %8, label %14, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = or i32 %12, %9
store i32 %13, i32* %11
br label %19
14: ; preds = %7
%15 = xor i32 %9, -1
%16 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, %15
store i32 %18, i32* %16
br label %19
19: ; preds = %14, %10
%20 = load i32, i32* @STDIN_FILENO
%21 = load i32, i32* @TCSANOW
%22 = call i64 @tcsetattr(i32 %20, i32 %21, %struct.termios* %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19, %1
%25 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %19 ]
%26 = call i32 @warn(i8* %25)
br label %27
27: ; preds = %24, %19
%28 = phi i32 [ 0, %19 ], [ -1, %24 ]
ret i32 %28
}