#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*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int ttySetRaw(int fd, struct termios *prevTermios) {
  struct termios t;
  if (tcgetattr(fd, &t) == -1)
    return -1;
  if (prevTermios != NULL)
    *prevTermios = t;
  t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
  t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ISTRIP |
                 IXON | PARMRK);
  t.c_oflag &= ~OPOST;
  t.c_cc[VMIN] = 1;
  t.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<ttySetRaw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1150 <ttySetRaw+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <ttySetRaw+0x14a>
cmpq   $0x0,-0x10(%rbp)
je     1176 <ttySetRaw+0x56>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rcx
mov    %rcx,0x10(%rax)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    0x2e22(%rip),%rcx
or     (%rcx),%eax
mov    0x2e01(%rip),%rcx
or     (%rcx),%eax
mov    0x2e18(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    0x2e2d(%rip),%rcx
or     (%rcx),%eax
mov    0x2db4(%rip),%rcx
or     (%rcx),%eax
mov    0x2e03(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0a(%rip),%rcx
or     (%rcx),%eax
mov    0x2da9(%rip),%rcx
or     (%rcx),%eax
mov    0x2d98(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcf(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfe(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d7e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2d39(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1263 <ttySetRaw+0x143>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <ttySetRaw+0x14a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttySetRaw>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     1228 <ttySetRaw+0x108>
test   %rbx,%rbx
je     1151 <ttySetRaw+0x31>
mov    0x10(%rsp),%rax
mov    %rax,0x10(%rbx)
movups (%rsp),%xmm0
movups %xmm0,(%rbx)
mov    0x2e08(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e26(%rip),%rax
or     (%rax),%ecx
mov    0x2e3d(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2e1f(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2ddd(%rip),%rax
or     (%rax),%ecx
mov    0x2e2c(%rip),%rax
or     (%rax),%ecx
mov    0x2e33(%rip),%rax
or     (%rax),%ecx
mov    0x2dd2(%rip),%rax
or     (%rax),%ecx
mov    0x2dc1(%rip),%rax
or     (%rax),%ecx
mov    0x2df8(%rip),%rax
or     (%rax),%ecx
mov    0x2e27(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2dac(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d6b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
jmp    122d <ttySetRaw+0x10d>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
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
@TCSAFLUSH = global i32 0
define i32 @ttySetRaw(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 i32, i32* %4
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %70
11: ; preds = %2
%12 = load %struct.termios*, %struct.termios** %5
%13 = icmp ne %struct.termios* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load %struct.termios*, %struct.termios** %5
%16 = bitcast %struct.termios* %15 to i8*
%17 = bitcast %struct.termios* %6 to i8*
br label %18
18: ; preds = %14, %11
%19 = load i32, i32* @ICANON
%20 = load i32, i32* @ISIG
%21 = or i32 %19, %20
%22 = load i32, i32* @IEXTEN
%23 = or i32 %21, %22
%24 = load i32, i32* @ECHO
%25 = or i32 %23, %24
%26 = xor i32 %25, -1
%27 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* @BRKINT
%31 = load i32, i32* @ICRNL
%32 = or i32 %30, %31
%33 = load i32, i32* @IGNBRK
%34 = or i32 %32, %33
%35 = load i32, i32* @IGNCR
%36 = or i32 %34, %35
%37 = load i32, i32* @INLCR
%38 = or i32 %36, %37
%39 = load i32, i32* @INPCK
%40 = or i32 %38, %39
%41 = load i32, i32* @ISTRIP
%42 = or i32 %40, %41
%43 = load i32, i32* @IXON
%44 = or i32 %42, %43
%45 = load i32, i32* @PARMRK
%46 = or i32 %44, %45
%47 = xor i32 %46, -1
%48 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%49 = load i32, i32* %48
%50 = and i32 %49, %47
store i32 %50, i32* %48
%51 = load i32, i32* @OPOST
%52 = xor i32 %51, -1
%53 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%54 = load i32, i32* %53
%55 = and i32 %54, %52
store i32 %55, i32* %53
%56 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%57 = load i32*, i32** %56
%58 = load i64, i64* @VMIN
%59 = getelementptr inbounds i32, i32* %57, i64 %58
store i32 1, i32* %59
%60 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%61 = load i32*, i32** %60
%62 = load i64, i64* @VTIME
%63 = getelementptr inbounds i32, i32* %61, i64 %62
store i32 0, i32* %63
%64 = load i32, i32* %4
%65 = load i32, i32* @TCSAFLUSH
%66 = call i32 @tcsetattr(i32 %64, i32 %65, %struct.termios* %6)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %69
68: ; preds = %18
store i32 -1, i32* %3
br label %70
69: ; preds = %18
store i32 0, i32* %3
br label %70
70: ; preds = %69, %68, %10
%71 = load i32, i32* %3
ret i32 %71
}

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