#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_cc;
};

/* Variables and functions */
int ECHO;
int ICANON;
int ICRNL;
int ISIG;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int ttySetCbreak(int fd, struct termios *prevTermios) {
  struct termios t;
  if (tcgetattr(fd, &t) == -1)
    return -1;
  if (prevTermios != NULL)
    *prevTermios = t;
  t.c_lflag &= ~(ICANON | ECHO);
  t.c_lflag |= ISIG;
  t.c_lflag &= ~ICRNL;
  t.c_cc[VMIN] = 1;
  t.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<ttySetCbreak>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1150 <ttySetCbreak+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <ttySetCbreak+0xe5>
cmpq   $0x0,-0x10(%rbp)
je     116e <ttySetCbreak+0x4e>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
mov    %rcx,0x8(%rax)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    0x2e5a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11fe <ttySetCbreak+0xde>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <ttySetCbreak+0xe5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttySetCbreak>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11c7 <ttySetCbreak+0xa7>
test   %rbx,%rbx
je     114b <ttySetCbreak+0x2b>
movups 0x8(%rsp),%xmm0
movups %xmm0,(%rbx)
mov    0x2e5e(%rip),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    0x2e66(%rip),%rax
or     (%rax),%ecx
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    %eax,0x8(%rsp)
mov    0x10(%rsp),%rax
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
jmp    11cc <ttySetCbreak+0xac>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@ICRNL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define i32 @ttySetCbreak(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 %49
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* @ECHO
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%26 = load i32, i32* @ISIG
%27 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = or i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* @ICRNL
%31 = xor i32 %30, -1
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %33, %31
store i32 %34, i32* %32
%35 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%36 = load i32*, i32** %35
%37 = load i64, i64* @VMIN
%38 = getelementptr inbounds i32, i32* %36, i64 %37
store i32 1, i32* %38
%39 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%40 = load i32*, i32** %39
%41 = load i64, i64* @VTIME
%42 = getelementptr inbounds i32, i32* %40, i64 %41
store i32 0, i32* %42
%43 = load i32, i32* %4
%44 = load i32, i32* @TCSAFLUSH
%45 = call i32 @tcsetattr(i32 %43, i32 %44, %struct.termios* %6)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %18
store i32 -1, i32* %3
br label %49
48: ; preds = %18
store i32 0, i32* %3
br label %49
49: ; preds = %48, %47, %10
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@ICRNL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
define i32 @ttySetCbreak(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 %34, 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* @ECHO
%14 = or i32 %13, %12
%15 = xor i32 %14, -1
%16 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, %15
%19 = load i32, i32* @ISIG
%20 = or i32 %19, %18
%21 = load i32, i32* @ICRNL
%22 = xor i32 %21, -1
%23 = and i32 %20, %22
store i32 %23, i32* %16
%24 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%25 = load i32*, i32** %24
%26 = load i64, i64* @VMIN
%27 = getelementptr inbounds i32, i32* %25, i64 %26
store i32 1, i32* %27
%28 = load i64, i64* @VTIME
%29 = getelementptr inbounds i32, i32* %25, i64 %28
store i32 0, i32* %29
%30 = load i32, i32* @TCSAFLUSH
%31 = call i32 @tcsetattr(i32 %0, i32 %30, %struct.termios* %3)
%32 = icmp eq i32 %31, -1
%33 = sext i1 %32 to i32
br label %34
34: ; preds = %11, %2
%35 = phi i32 [ -1, %2 ], [ %33, %11 ]
ret i32 %35
}