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

/* Variables and functions */
scalar_t__ ECHO;
scalar_t__ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int cannon_mode(int flag) {
  struct termios config;
  if (tcgetattr(0, &config) != 0) {
    fprintf(stderr, "Configuration getting failed.\n");
    return (1);
  }
  config.c_lflag += flag ? ECHO : -ECHO;
  config.c_lflag += flag ? ICANON : -ICANON;
  config.c_cc[VMIN] = flag;
  config.c_cc[VTIME] = !flag;
  if (tcsetattr(0, TCSANOW, &config) != 0) {
    fprintf(stderr, "Configuration setting failed.\n");
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<cannon_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
je     1171 <cannon_mode+0x41>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <cannon_mode+0x130>
cmpl   $0x0,-0x8(%rbp)
je     118e <cannon_mode+0x5e>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    119e <cannon_mode+0x6e>
xor    %eax,%eax
mov    0x2e49(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
movslq -0x10(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11c9 <cannon_mode+0x99>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11d9 <cannon_mode+0xa9>
xor    %eax,%eax
mov    0x2de6(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
movslq -0x10(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
cmpl   $0x0,-0x8(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edx
mov    -0x18(%rbp),%rax
mov    0x2dac(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
je     1259 <cannon_mode+0x129>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <cannon_mode+0x130>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cannon_mode>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1040 <tcgetattr@plt>
test   %rax,%rax
je     1154 <cannon_mode+0x24>
lea    0xeae(%rip),%rsi
jmp    11c6 <cannon_mode+0x96>
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
neg    %ecx
mov    0x2e50(%rip),%rdx
mov    (%rdx),%edx
mov    %edx,%esi
neg    %esi
xor    %edi,%edi
test   %ebp,%ebp
sete   %dil
cmovne %eax,%ecx
cmovne %edx,%esi
add    0x10(%rsp),%ecx
add    %esi,%ecx
mov    %ecx,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%rcx
mov    %ebp,(%rax,%rcx,4)
mov    0x2e21(%rip),%rcx
mov    (%rcx),%rcx
mov    %edi,(%rax,%rcx,4)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
test   %rax,%rax
je     11d9 <cannon_mode+0xa9>
lea    0xe59(%rip),%rsi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1030 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@stderr = global i32 0
@.str = [31 x i8] c"Configuration getting failed.\0A\00"
@ECHO = global i64 0
@ICANON = global i64 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [31 x i8] c"Configuration setting failed.\0A\00"
define i32 @cannon_mode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %3
%5 = call i64 @tcgetattr(i32 0, %struct.termios* %4)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %59
10: ; preds = %1
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i64, i64* @ECHO
br label %18
15: ; preds = %10
%16 = load i64, i64* @ECHO
%17 = sub nsw i64 0, %16
br label %18
18: ; preds = %15, %13
%19 = phi i64 [ %14, %13 ], [ %17, %15 ]
%20 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%21 = load i32, i32* %20
%22 = sext i32 %21 to i64
%23 = add nsw i64 %22, %19
%24 = trunc i64 %23 to i32
store i32 %24, i32* %20
%25 = load i32, i32* %3
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = load i64, i64* @ICANON
br label %32
29: ; preds = %18
%30 = load i64, i64* @ICANON
%31 = sub nsw i64 0, %30
br label %32
32: ; preds = %29, %27
%33 = phi i64 [ %28, %27 ], [ %31, %29 ]
%34 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%35 = load i32, i32* %34
%36 = sext i32 %35 to i64
%37 = add nsw i64 %36, %33
%38 = trunc i64 %37 to i32
store i32 %38, i32* %34
%39 = load i32, i32* %3
%40 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%41 = load i32*, i32** %40
%42 = load i64, i64* @VMIN
%43 = getelementptr inbounds i32, i32* %41, i64 %42
store i32 %39, i32* %43
%44 = load i32, i32* %3
%45 = icmp ne i32 %44, 0
%46 = xor i1 %45, true
%47 = zext i1 %46 to i32
%48 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%49 = load i32*, i32** %48
%50 = load i64, i64* @VTIME
%51 = getelementptr inbounds i32, i32* %49, i64 %50
store i32 %47, i32* %51
%52 = load i32, i32* @TCSANOW
%53 = call i64 @tcsetattr(i32 0, i32 %52, %struct.termios* %4)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %32
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %59
58: ; preds = %32
store i32 0, i32* %2
br label %59
59: ; preds = %58, %55, %7
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@stderr = global i32 0
@.str = [31 x i8] c"Configuration getting failed.\0A\00"
@ECHO = global i64 0
@ICANON = global i64 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [31 x i8] c"Configuration setting failed.\0A\00"
define i32 @cannon_mode(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i64 @tcgetattr(i32 0, %struct.termios* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %30
6: ; preds = %1
%7 = icmp eq i32 %0, 0
%8 = load i64, i64* @ECHO
%9 = sub nsw i64 0, %8
%10 = select i1 %7, i64 %9, i64 %8
%11 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%12 = load i32, i32* %11
%13 = trunc i64 %10 to i32
%14 = add i32 %12, %13
%15 = load i64, i64* @ICANON
%16 = sub nsw i64 0, %15
%17 = select i1 %7, i64 %16, i64 %15
%18 = trunc i64 %17 to i32
%19 = add i32 %14, %18
store i32 %19, i32* %11
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%21 = load i32*, i32** %20
%22 = load i64, i64* @VMIN
%23 = getelementptr inbounds i32, i32* %21, i64 %22
store i32 %0, i32* %23
%24 = zext i1 %7 to i32
%25 = load i64, i64* @VTIME
%26 = getelementptr inbounds i32, i32* %21, i64 %25
store i32 %24, i32* %26
%27 = load i32, i32* @TCSANOW
%28 = call i64 @tcsetattr(i32 0, i32 %27, %struct.termios* %2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %6, %1
%31 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %6 ]
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* %31)
br label %34
34: ; preds = %30, %6
%35 = phi i32 [ 0, %6 ], [ 1, %30 ]
ret i32 %35
}