#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_oflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_iflag;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ONLCR;
int OPOST;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ assert(int);
int fileno(int /*<<< orphan*/ *);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
void tty_set_raw_mode(FILE *istream, struct termios *old_termios) {
  struct termios new_termios;
  int fd = fileno(istream);
  int status;
  status = tcgetattr(fd, old_termios);
  if (-1 != status) {
    status = tcgetattr(fd, &new_termios);
    assert(-1 != status);
    new_termios.c_iflag = 0;
    new_termios.c_oflag = OPOST | ONLCR;
    new_termios.c_lflag = 0;
    new_termios.c_cc[VMIN] = 1;
    new_termios.c_cc[VTIME] = 0;
    status = tcsetattr(fd, TCSAFLUSH, &new_termios);
    assert(-1 != status);
  }
}


x86 assembly (clang, -O0):
<tty_set_raw_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    %eax,-0x38(%rbp)
mov    $0xffffffff,%eax
cmp    -0x38(%rbp),%eax
je     1219 <tty_set_raw_mode+0xd9>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    %eax,-0x38(%rbp)
mov    $0xffffffff,%eax
cmp    -0x38(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movq   $0x0,-0x18(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    0x2e13(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x34(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    %eax,-0x38(%rbp)
mov    $0xffffffff,%eax
cmp    -0x38(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_set_raw_mode>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
call   1040 <fileno@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11e5 <tty_set_raw_mode+0xa5>
mov    %rsp,%r14
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1030 <assert@plt>
mov    0x2e59(%rip),%rax
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e06(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1030 <assert@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i64 }
@OPOST = global i32 0
@ONLCR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define void @tty_set_raw_mode(i32* %0, %struct.termios* %1) {
%3 = alloca i32*
%4 = alloca %struct.termios*
%5 = alloca %struct.termios
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store %struct.termios* %1, %struct.termios** %4
%8 = load i32*, i32** %3
%9 = call i32 @fileno(i32* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = load %struct.termios*, %struct.termios** %4
%12 = call i32 @tcgetattr(i32 %10, %struct.termios* %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp ne i32 -1, %13
br i1 %14, label %15, label %43
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @tcgetattr(i32 %16, %struct.termios* %5)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp ne i32 -1, %18
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 3
store i64 0, i64* %22
%23 = load i32, i32* @OPOST
%24 = load i32, i32* @ONLCR
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%29 = load i32*, i32** %28
%30 = load i64, i64* @VMIN
%31 = getelementptr inbounds i32, i32* %29, i64 %30
store i32 1, i32* %31
%32 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%33 = load i32*, i32** %32
%34 = load i64, i64* @VTIME
%35 = getelementptr inbounds i32, i32* %33, i64 %34
store i32 0, i32* %35
%36 = load i32, i32* %6
%37 = load i32, i32* @TCSAFLUSH
%38 = call i32 @tcsetattr(i32 %36, i32 %37, %struct.termios* %5)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp ne i32 -1, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
br label %43
43: ; preds = %15, %2
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i64 }
@OPOST = global i32 0
@ONLCR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define void @tty_set_raw_mode(i32* nocapture %0, %struct.termios* %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = tail call i32 @fileno(i32* %0)
%6 = tail call i32 @tcgetattr(i32 %5, %struct.termios* %1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %30, label %8
8: ; preds = %2
%9 = call i32 @tcgetattr(i32 %5, %struct.termios* %3)
%10 = icmp ne i32 %9, -1
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* @OPOST
%14 = load i32, i32* @ONLCR
%15 = or i32 %14, %13
%16 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%18 = bitcast i64* %17 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %18
%19 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%20 = load i32*, i32** %19
%21 = load i64, i64* @VMIN
%22 = getelementptr inbounds i32, i32* %20, i64 %21
store i32 1, i32* %22
%23 = load i64, i64* @VTIME
%24 = getelementptr inbounds i32, i32* %20, i64 %23
store i32 0, i32* %24
%25 = load i32, i32* @TCSAFLUSH
%26 = call i32 @tcsetattr(i32 %5, i32 %25, %struct.termios* %3)
%27 = icmp ne i32 %26, -1
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
br label %30
30: ; preds = %8, %2
ret void
}