#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 EINTR;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ perror(char *);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_disp_mode(int fd, int option) {
  int err;
  struct termios term;
  if (tcgetattr(fd, &term) == -1) {
    perror("Cannot get the attribution of the terminal");
    return 1;
  }
  if (option)
    term.c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
  else
    term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
  err = tcsetattr(fd, TCSAFLUSH, &term);
  if (err == -1 && err == EINTR) {
    perror("Cannot set the attribution of the terminal");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_disp_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    116b <set_disp_mode+0x3b>
lea    0xea6(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1224 <set_disp_mode+0xf4>
cmpl   $0x0,-0xc(%rbp)
je     11a4 <set_disp_mode+0x74>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    0x2e53(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e29(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11d1 <set_disp_mode+0xa1>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    0x2e24(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0b(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfa(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    121d <set_disp_mode+0xed>
mov    -0x10(%rbp),%eax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%eax
jne    121d <set_disp_mode+0xed>
lea    0xe1f(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1224 <set_disp_mode+0xf4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_disp_mode>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11a7 <set_disp_mode+0x77>
mov    0x2e95(%rip),%rcx
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e6b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5a(%rip),%rcx
or     (%rcx),%eax
test   %ebp,%ebp
je     1171 <set_disp_mode+0x41>
or     %eax,(%rsp)
jmp    1176 <set_disp_mode+0x46>
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
jne    11b8 <set_disp_mode+0x88>
mov    0x2e1f(%rip),%rcx
cmpl   $0xffffffff,(%rcx)
jne    11b8 <set_disp_mode+0x88>
lea    0xe86(%rip),%rdi
jmp    11ae <set_disp_mode+0x7e>
lea    0xe52(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [43 x i8] c"Cannot get the attribution of the terminal\00"
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
@EINTR = global i32 0
@.str.1 = [43 x i8] c"Cannot set the attribution of the terminal\00"
define i32 @set_disp_mode(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* %4
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %7)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %52
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %13
%17 = load i32, i32* @ECHO
%18 = load i32, i32* @ECHOE
%19 = or i32 %17, %18
%20 = load i32, i32* @ECHOK
%21 = or i32 %19, %20
%22 = load i32, i32* @ECHONL
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
br label %39
27: ; preds = %13
%28 = load i32, i32* @ECHO
%29 = load i32, i32* @ECHOE
%30 = or i32 %28, %29
%31 = load i32, i32* @ECHOK
%32 = or i32 %30, %31
%33 = load i32, i32* @ECHONL
%34 = or i32 %32, %33
%35 = xor i32 %34, -1
%36 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%37 = load i32, i32* %36
%38 = and i32 %37, %35
store i32 %38, i32* %36
br label %39
39: ; preds = %27, %16
%40 = load i32, i32* %4
%41 = load i32, i32* @TCSAFLUSH
%42 = call i32 @tcsetattr(i32 %40, i32 %41, %struct.termios* %7)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i32, i32* %6
%47 = load i32, i32* @EINTR
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %52
51: ; preds = %45, %39
store i32 0, i32* %3
br label %52
52: ; preds = %51, %49, %11
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [43 x i8] c"Cannot get the attribution of the terminal\00"
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
@EINTR = global i32 0
@.str.1 = [43 x i8] c"Cannot set the attribution of the terminal\00"
define i32 @set_disp_mode(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
br i1 %8, label %20, label %16
16: ; preds = %7
%17 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = or i32 %15, %18
store i32 %19, i32* %17
br label %25
20: ; preds = %7
%21 = xor i32 %15, -1
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = and i32 %23, %21
store i32 %24, i32* %22
br label %25
25: ; preds = %20, %16
%26 = load i32, i32* @TCSAFLUSH
%27 = call i32 @tcsetattr(i32 %0, i32 %26, %struct.termios* %3)
%28 = icmp eq i32 %27, -1
%29 = load i32, i32* @EINTR
%30 = icmp eq i32 %29, -1
%31 = select i1 %28, i1 %30, i1 false
br i1 %31, label %32, label %35
32: ; preds = %25, %2
%33 = phi i8* [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), %25 ]
%34 = call i32 @perror(i8* %33)
br label %35
35: ; preds = %32, %25
%36 = phi i32 [ 0, %25 ], [ 1, %32 ]
ret i32 %36
}