#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*/ TCSAFLUSH;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ isatty(int);
int /*<<< orphan*/ log_err(int /*<<< orphan*/, char *, int);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void set_tty_mode(struct termios *tty, int fd) {
  assert(fd >= 0);
  assert(tty != NULL);
  if (!isatty(fd))
    return;
  if (tcsetattr(fd, TCSAFLUSH, tty) < 0)
    log_err(errno, "tcgetattr() failed on fd=%d", fd);
  return;
}


x86 assembly (clang, -O0):
<set_tty_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1188 <set_tty_mode+0x48>
jmp    11c4 <set_tty_mode+0x84>
mov    -0xc(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11bf <set_tty_mode+0x7f>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe46(%rip),%rsi
call   1060 <log_err@plt>
jmp    11c4 <set_tty_mode+0x84>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_tty_mode>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%edi
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
mov    %ebx,%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     1186 <set_tty_mode+0x46>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
test   %rax,%rax
js     118e <set_tty_mode+0x4e>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <log_err@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@TCSAFLUSH = global i32 0
@errno = global i32 0
@.str = [28 x i8] c"tcgetattr() failed on fd=%d\00"
define void @set_tty_mode(%struct.termios* %0, i32 %1) {
%3 = alloca %struct.termios*
%4 = alloca i32
store %struct.termios* %0, %struct.termios** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = icmp sge i32 %5, 0
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load %struct.termios*, %struct.termios** %3
%10 = icmp ne %struct.termios* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* %4
%14 = call i32 @isatty(i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %2
br label %28
17: ; preds = %2
%18 = load i32, i32* %4
%19 = load i32, i32* @TCSAFLUSH
%20 = load %struct.termios*, %struct.termios** %3
%21 = call i64 @tcsetattr(i32 %18, i32 %19, %struct.termios* %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @errno
%25 = load i32, i32* %4
%26 = call i32 @log_err(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %17
br label %28
28: ; preds = %27, %16
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TCSAFLUSH = global i32 0
@errno = global i32 0
@.str = [28 x i8] c"tcgetattr() failed on fd=%d\00"
define void @set_tty_mode(%struct.termios* %0, i32 %1) {
%3 = xor i32 %1, -1
%4 = lshr i32 %3, 31
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne %struct.termios* %0, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @isatty(i32 %1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %2
%12 = load i32, i32* @TCSAFLUSH
%13 = tail call i64 @tcsetattr(i32 %1, i32 %12, %struct.termios* %0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = tail call i32 @log_err(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %18
18: ; preds = %11, %15, %2
ret void
}