#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ TCSANOW;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, ...);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
void tty_mode(int how) {
  static struct termios old_mode;
  static int old_flag;
  if (how == 0) {
    tcgetattr(0, &old_mode);
    old_flag = fcntl(0, F_GETFL);
  } else {
    tcsetattr(0, TCSANOW, &old_mode);
    fcntl(0, F_SETFL, old_flag);
  }
}


x86 assembly (clang, -O0):
<tty_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1170 <tty_mode+0x40>
xor    %edi,%edi
lea    0x2eee(%rip),%rsi
call   1040 <tcgetattr@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,0x2ed5(%rip)
jmp    119f <tty_mode+0x6f>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2eba(%rip),%rdx
call   1050 <tcsetattr@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2eaa(%rip),%edx
xor    %edi,%edi
mov    $0x0,%al
call   1030 <fcntl@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_mode>:
push   %rax
test   %edi,%edi
je     1165 <tty_mode+0x35>
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
lea    0x2ef7(%rip),%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    0x2ee5(%rip),%edx
xor    %edi,%edi
xor    %eax,%eax
pop    %rcx
jmp    1030 <fcntl@plt>
lea    0x2ed0(%rip),%rsi
xor    %edi,%edi
call   1040 <tcgetattr@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,0x2eb5(%rip)
pop    %rax
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@tty_mode.old_mode = internal global %struct.termios zeroinitializer
@tty_mode.old_flag = internal global i32 0
@F_GETFL = global i32 0
@TCSANOW = global i32 0
@F_SETFL = global i32 0
define void @tty_mode(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* %2
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = call i32 @tcgetattr(i32 0, %struct.termios* @tty_mode.old_mode)
%7 = load i32, i32* @F_GETFL
%8 = call i32 (i32, i32, ...) @fcntl(i32 0, i32 %7)
store i32 %8, i32* @tty_mode.old_flag
br label %15
9: ; preds = %1
%10 = load i32, i32* @TCSANOW
%11 = call i32 @tcsetattr(i32 0, i32 %10, %struct.termios* @tty_mode.old_mode)
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @tty_mode.old_flag
%14 = call i32 (i32, i32, ...) @fcntl(i32 0, i32 %12, i32 %13)
br label %15
15: ; preds = %9, %5
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@tty_mode.old_mode = internal global %struct.termios zeroinitializer
@tty_mode.old_flag = internal unnamed_addr global i32 0
@F_GETFL = global i32 0
@TCSANOW = global i32 0
@F_SETFL = global i32 0
define void @tty_mode(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %7
3: ; preds = %1
%4 = tail call i32 @tcgetattr(i32 0, %struct.termios* @tty_mode.old_mode)
%5 = load i32, i32* @F_GETFL
%6 = tail call i32 (i32, i32, ...) @fcntl(i32 0, i32 %5)
store i32 %6, i32* @tty_mode.old_flag
br label %13
7: ; preds = %1
%8 = load i32, i32* @TCSANOW
%9 = tail call i32 @tcsetattr(i32 0, i32 %8, %struct.termios* @tty_mode.old_mode)
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* @tty_mode.old_flag
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 0, i32 %10, i32 %11)
br label %13
13: ; preds = %7, %3
ret void
}