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


x86 assembly (clang, -O0):
<get_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 <get_tty_mode+0x48>
jmp    11bb <get_tty_mode+0x7b>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11b6 <get_tty_mode+0x76>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe4f(%rip),%rsi
call   1060 <log_err@plt>
jmp    11bb <get_tty_mode+0x7b>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_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     117d <get_tty_mode+0x3d>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
test   %rax,%rax
js     1185 <get_tty_mode+0x45>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <log_err@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@errno = global i32 0
@.str = [28 x i8] c"tcgetattr() failed on fd=%d\00"
define void @get_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 %27
17: ; preds = %2
%18 = load i32, i32* %4
%19 = load %struct.termios*, %struct.termios** %3
%20 = call i64 @tcgetattr(i32 %18, %struct.termios* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @errno
%24 = load i32, i32* %4
%25 = call i32 @log_err(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %22, %17
br label %27
27: ; preds = %26, %16
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@errno = global i32 0
@.str = [28 x i8] c"tcgetattr() failed on fd=%d\00"
define void @get_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 %17, label %11
11: ; preds = %2
%12 = tail call i64 @tcgetattr(i32 %1, %struct.termios* %0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i32 @log_err(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %17
17: ; preds = %11, %14, %2
ret void
}