#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 /*<<< orphan*/ c_cflag;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCAL;
int /*<<< orphan*/ CS8;
int /*<<< orphan*/ PARENB;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void set_raw_tty_mode(int fd) {
  struct termios ttymodes;
  if (tcgetattr(fd, &ttymodes) < 0) {
    perror("tcgetattr");
    exit(1);
  }
  cfmakeraw(&ttymodes);
  ttymodes.c_cflag |= CS8;
  ttymodes.c_cflag &= ~PARENB;
  ttymodes.c_cflag |= CLOCAL;
  if (tcsetattr(fd, TCSANOW, &ttymodes) < 0) {
    perror("tcsetattr");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<set_raw_tty_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1187 <set_raw_tty_mode+0x37>
lea    0xe88(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x8(%rbp),%rdi
call   1030 <cfmakeraw@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11f5 <set_raw_tty_mode+0xa5>
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_raw_tty_mode>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%rsi
call   1040 <tcgetattr@plt>
test   %rax,%rax
js     11b3 <set_raw_tty_mode+0x63>
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <cfmakeraw@plt>
mov    0x2e63(%rip),%rax
mov    (%rsp),%ecx
or     (%rax),%ecx
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2e32(%rip),%rcx
or     (%rcx),%eax
mov    %eax,(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
test   %rax,%rax
js     11bc <set_raw_tty_mode+0x6c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe46(%rip),%rdi
jmp    11c3 <set_raw_tty_mode+0x73>
lea    0xe47(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [10 x i8] c"tcgetattr\00"
@CS8 = global i32 0
@PARENB = global i32 0
@CLOCAL = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define void @set_raw_tty_mode(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i64 @tcgetattr(i32 %4, %struct.termios* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = call i32 @cfmakeraw(%struct.termios* %3)
%12 = load i32, i32* @CS8
%13 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = or i32 %14, %12
store i32 %15, i32* %13
%16 = load i32, i32* @PARENB
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* @CLOCAL
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = or i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* %2
%26 = load i32, i32* @TCSANOW
%27 = call i64 @tcsetattr(i32 %25, i32 %26, %struct.termios* %3)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %10
%30 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %10
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [10 x i8] c"tcgetattr\00"
@CS8 = global i32 0
@PARENB = global i32 0
@CLOCAL = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define void @set_raw_tty_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 slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = call i32 @cfmakeraw(%struct.termios* %2)
%11 = load i32, i32* @CS8
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = or i32 %13, %11
%15 = load i32, i32* @PARENB
%16 = xor i32 %15, -1
%17 = and i32 %14, %16
%18 = load i32, i32* @CLOCAL
%19 = or i32 %18, %17
store i32 %19, i32* %12
%20 = load i32, i32* @TCSANOW
%21 = call i64 @tcsetattr(i32 %0, i32 %20, %struct.termios* %2)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %9
%24 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %9
ret void
}