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

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ 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_iflag &= ~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    $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 <ttySetCbreak+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <ttySetCbreak+0xed>
cmpq   $0x0,-0x10(%rbp)
je     1176 <ttySetCbreak+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    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    0x2e52(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2de6(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1206 <ttySetCbreak+0xe6>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <ttySetCbreak+0xed>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttySetCbreak>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11cb <ttySetCbreak+0xab>
test   %rbx,%rbx
je     1151 <ttySetCbreak+0x31>
mov    0x10(%rsp),%rax
mov    %rax,0x10(%rbx)
movups (%rsp),%xmm0
movups %xmm0,(%rbx)
mov    0x2e58(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
mov    0x2e61(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e18(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e2f(%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    11d0 <ttySetCbreak+0xb0>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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