#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_lflag;
  int c_iflag;
  int *c_cc;
  int /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int BRKINT;
int ECHO;
int ICANON;
int ICRNL;
int IEXTEN;
int IGNBRK;
int IGNCR;
int INLCR;
int INPCK;
int ISIG;
int ISTRIP;
int IXON;
int /*<<< orphan*/ OPOST;
int PARMRK;
int /*<<< orphan*/ TCSADRAIN;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int init_tty_raw(int fd, struct termios *tty_base_attrs) {
  struct termios tty_raw_attrs;
  memcpy(&tty_raw_attrs, tty_base_attrs, sizeof(struct termios));
  tty_raw_attrs.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
  tty_raw_attrs.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK |
                             ISTRIP | IXON | PARMRK);
  tty_raw_attrs.c_oflag &= ~OPOST;
  tty_raw_attrs.c_cc[VMIN] = 1;
  tty_raw_attrs.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSADRAIN, &tty_raw_attrs) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<init_tty_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdi
mov    $0x18,%edx
call   1030 <memcpy@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2e5f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3e(%rip),%rcx
or     (%rcx),%eax
mov    0x2e55(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    0x2e62(%rip),%rcx
or     (%rcx),%eax
mov    0x2de9(%rip),%rcx
or     (%rcx),%eax
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3f(%rip),%rcx
or     (%rcx),%eax
mov    0x2de6(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcd(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e33(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dbb(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2d6e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    122e <init_tty_raw+0x10e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <init_tty_raw+0x115>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_tty_raw>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x18,%edx
call   1030 <memcpy@plt>
mov    0x2e20(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e46(%rip),%rax
or     (%rax),%ecx
mov    0x2e5d(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2e3f(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2df5(%rip),%rax
or     (%rax),%ecx
mov    0x2e4c(%rip),%rax
or     (%rax),%ecx
mov    0x2e4b(%rip),%rax
or     (%rax),%ecx
mov    0x2df2(%rip),%rax
or     (%rax),%ecx
mov    0x2dd9(%rip),%rax
or     (%rax),%ecx
mov    0x2e18(%rip),%rax
or     (%rax),%ecx
mov    0x2e3f(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d83(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <tcsetattr@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
sete   %cl
neg    %ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i32 }
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@ECHO = global i32 0
@BRKINT = global i32 0
@ICRNL = global i32 0
@IGNBRK = global i32 0
@IGNCR = global i32 0
@INLCR = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@IXON = global i32 0
@PARMRK = global i32 0
@OPOST = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSADRAIN = global i32 0
define i32 @init_tty_raw(i32 %0, %struct.termios* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios*
%6 = alloca %struct.termios
store i32 %0, i32* %4
store %struct.termios* %1, %struct.termios** %5
%7 = load %struct.termios*, %struct.termios** %5
%8 = call i32 @memcpy(%struct.termios* %6, %struct.termios* %7, i32 24)
%9 = load i32, i32* @ICANON
%10 = load i32, i32* @ISIG
%11 = or i32 %9, %10
%12 = load i32, i32* @IEXTEN
%13 = or i32 %11, %12
%14 = load i32, i32* @ECHO
%15 = or i32 %13, %14
%16 = xor i32 %15, -1
%17 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %18, %16
store i32 %19, i32* %17
%20 = load i32, i32* @BRKINT
%21 = load i32, i32* @ICRNL
%22 = or i32 %20, %21
%23 = load i32, i32* @IGNBRK
%24 = or i32 %22, %23
%25 = load i32, i32* @IGNCR
%26 = or i32 %24, %25
%27 = load i32, i32* @INLCR
%28 = or i32 %26, %27
%29 = load i32, i32* @INPCK
%30 = or i32 %28, %29
%31 = load i32, i32* @ISTRIP
%32 = or i32 %30, %31
%33 = load i32, i32* @IXON
%34 = or i32 %32, %33
%35 = load i32, i32* @PARMRK
%36 = or i32 %34, %35
%37 = xor i32 %36, -1
%38 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%39 = load i32, i32* %38
%40 = and i32 %39, %37
store i32 %40, i32* %38
%41 = load i32, i32* @OPOST
%42 = xor i32 %41, -1
%43 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%44 = load i32, i32* %43
%45 = and i32 %44, %42
store i32 %45, i32* %43
%46 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%47 = load i32*, i32** %46
%48 = load i64, i64* @VMIN
%49 = getelementptr inbounds i32, i32* %47, i64 %48
store i32 1, i32* %49
%50 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%51 = load i32*, i32** %50
%52 = load i64, i64* @VTIME
%53 = getelementptr inbounds i32, i32* %51, i64 %52
store i32 0, i32* %53
%54 = load i32, i32* %4
%55 = load i32, i32* @TCSADRAIN
%56 = call i32 @tcsetattr(i32 %54, i32 %55, %struct.termios* %6)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %59
58: ; preds = %2
store i32 -1, i32* %3
br label %60
59: ; preds = %2
store i32 0, i32* %3
br label %60
60: ; preds = %59, %58
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i32 }
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@ECHO = global i32 0
@BRKINT = global i32 0
@ICRNL = global i32 0
@IGNBRK = global i32 0
@IGNCR = global i32 0
@INLCR = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@IXON = global i32 0
@PARMRK = global i32 0
@OPOST = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSADRAIN = global i32 0
define i32 @init_tty_raw(i32 %0, %struct.termios* %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @memcpy(%struct.termios* %3, %struct.termios* %1, i32 24)
%6 = load i32, i32* @ICANON
%7 = load i32, i32* @ISIG
%8 = or i32 %7, %6
%9 = load i32, i32* @IEXTEN
%10 = or i32 %8, %9
%11 = load i32, i32* @ECHO
%12 = or i32 %10, %11
%13 = xor i32 %12, -1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = load i32, i32* @BRKINT
%18 = load i32, i32* @ICRNL
%19 = or i32 %18, %17
%20 = load i32, i32* @IGNBRK
%21 = or i32 %19, %20
%22 = load i32, i32* @IGNCR
%23 = or i32 %21, %22
%24 = load i32, i32* @INLCR
%25 = or i32 %23, %24
%26 = load i32, i32* @INPCK
%27 = or i32 %25, %26
%28 = load i32, i32* @ISTRIP
%29 = or i32 %27, %28
%30 = load i32, i32* @IXON
%31 = or i32 %29, %30
%32 = load i32, i32* @PARMRK
%33 = or i32 %31, %32
%34 = xor i32 %33, -1
%35 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%36 = load i32, i32* %35
%37 = and i32 %36, %34
store i32 %37, i32* %35
%38 = load i32, i32* @OPOST
%39 = xor i32 %38, -1
%40 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%41 = load i32, i32* %40
%42 = and i32 %41, %39
store i32 %42, i32* %40
%43 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%44 = load i32*, i32** %43
%45 = load i64, i64* @VMIN
%46 = getelementptr inbounds i32, i32* %44, i64 %45
store i32 1, i32* %46
%47 = load i64, i64* @VTIME
%48 = getelementptr inbounds i32, i32* %44, i64 %47
store i32 0, i32* %48
%49 = load i32, i32* @TCSADRAIN
%50 = call i32 @tcsetattr(i32 %0, i32 %49, %struct.termios* %3)
%51 = icmp eq i32 %50, -1
%52 = sext i1 %51 to i32
ret i32 %52
}