#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_iflag;
  int c_lflag;
  int c_cflag;
  int /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int BRKINT;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int ECHO;
int ECHONL;
int ICANON;
int ICRNL;
int IEXTEN;
int IGNBRK;
int IGNCR;
int INLCR;
int ISIG;
int ISTRIP;
int IXON;
int /*<<< orphan*/ OPOST;
int PARENB;
int PARMRK;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ perror(char *);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int setup_term(int fd) {
  struct termios termios;
  if (tcgetattr(fd, &termios) == -1) {
    perror("tcgetattr");
    return -1;
  }
  termios.c_iflag &=
      ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  termios.c_oflag &= ~OPOST;
  termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  termios.c_cflag &= ~(CSIZE | PARENB | CSTOPB);
  termios.c_cflag |= CS8 | CREAD;
  if (tcsetattr(fd, TCSANOW, &termios) == -1) {
    perror("tcsetattr");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_term>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1168 <setup_term+0x38>
lea    0xea9(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <setup_term+0x14a>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    0x2e20(%rip),%rcx
or     (%rcx),%eax
mov    0x2e6f(%rip),%rcx
or     (%rcx),%eax
mov    0x2dde(%rip),%rcx
or     (%rcx),%eax
mov    0x2e45(%rip),%rcx
or     (%rcx),%eax
mov    0x2e34(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e12(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    0x2d85(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6c(%rip),%rcx
or     (%rcx),%eax
mov    0x2db3(%rip),%rcx
or     (%rcx),%eax
mov    0x2d82(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    0x2da7(%rip),%rcx
or     (%rcx),%eax
mov    0x2d26(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    0x2d5b(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1273 <setup_term+0x143>
lea    0xda8(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <setup_term+0x14a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_term>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     1230 <setup_term+0x100>
mov    0x2e09(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e8f(%rip),%rax
or     (%rax),%ecx
mov    0x2dfe(%rip),%rax
or     (%rax),%ecx
mov    0x2e65(%rip),%rax
or     (%rax),%ecx
mov    0x2e54(%rip),%rax
or     (%rax),%ecx
mov    0x2e5b(%rip),%rax
or     (%rax),%ecx
mov    0x2e32(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0xc(%rsp)
mov    0x2dfd(%rip),%rax
mov    0x2dae(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d93(%rip),%rax
or     (%rax),%ecx
mov    0x2dda(%rip),%rax
or     (%rax),%ecx
mov    0x2da9(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2da2(%rip),%rax
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d50(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    0x2d89(%rip),%rax
mov    0x2d8a(%rip),%rdx
or     (%rax),%ecx
or     (%rdx),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     1239 <setup_term+0x109>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdc9(%rip),%rdi
jmp    1240 <setup_term+0x110>
lea    0xdca(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    122a <setup_term+0xfa>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32 }
@.str = [10 x i8] c"tcgetattr\00"
@IGNBRK = global i32 0
@BRKINT = global i32 0
@PARMRK = global i32 0
@ISTRIP = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@CSIZE = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @setup_term(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %70
10: ; preds = %1
%11 = load i32, i32* @IGNBRK
%12 = load i32, i32* @BRKINT
%13 = or i32 %11, %12
%14 = load i32, i32* @PARMRK
%15 = or i32 %13, %14
%16 = load i32, i32* @ISTRIP
%17 = or i32 %15, %16
%18 = load i32, i32* @INLCR
%19 = or i32 %17, %18
%20 = load i32, i32* @IGNCR
%21 = or i32 %19, %20
%22 = load i32, i32* @ICRNL
%23 = or i32 %21, %22
%24 = load i32, i32* @IXON
%25 = or i32 %23, %24
%26 = xor i32 %25, -1
%27 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* @OPOST
%31 = xor i32 %30, -1
%32 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 3
%33 = load i32, i32* %32
%34 = and i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* @ECHO
%36 = load i32, i32* @ECHONL
%37 = or i32 %35, %36
%38 = load i32, i32* @ICANON
%39 = or i32 %37, %38
%40 = load i32, i32* @ISIG
%41 = or i32 %39, %40
%42 = load i32, i32* @IEXTEN
%43 = or i32 %41, %42
%44 = xor i32 %43, -1
%45 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%46 = load i32, i32* %45
%47 = and i32 %46, %44
store i32 %47, i32* %45
%48 = load i32, i32* @CSIZE
%49 = load i32, i32* @PARENB
%50 = or i32 %48, %49
%51 = load i32, i32* @CSTOPB
%52 = or i32 %50, %51
%53 = xor i32 %52, -1
%54 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
%55 = load i32, i32* %54
%56 = and i32 %55, %53
store i32 %56, i32* %54
%57 = load i32, i32* @CS8
%58 = load i32, i32* @CREAD
%59 = or i32 %57, %58
%60 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
%61 = load i32, i32* %60
%62 = or i32 %61, %59
store i32 %62, i32* %60
%63 = load i32, i32* %3
%64 = load i32, i32* @TCSANOW
%65 = call i32 @tcsetattr(i32 %63, i32 %64, %struct.termios* %4)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %69
67: ; preds = %10
%68 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %70
69: ; preds = %10
store i32 0, i32* %2
br label %70
70: ; preds = %69, %67, %8
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32 }
@.str = [10 x i8] c"tcgetattr\00"
@IGNBRK = global i32 0
@BRKINT = global i32 0
@PARMRK = global i32 0
@ISTRIP = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@CSIZE = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @setup_term(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %60, label %6
6: ; preds = %1
%7 = load i32, i32* @IGNBRK
%8 = load i32, i32* @BRKINT
%9 = or i32 %8, %7
%10 = load i32, i32* @PARMRK
%11 = or i32 %9, %10
%12 = load i32, i32* @ISTRIP
%13 = or i32 %11, %12
%14 = load i32, i32* @INLCR
%15 = or i32 %13, %14
%16 = load i32, i32* @IGNCR
%17 = or i32 %15, %16
%18 = load i32, i32* @ICRNL
%19 = or i32 %17, %18
%20 = load i32, i32* @IXON
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%26 = load i32, i32* @OPOST
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @ECHO
%32 = load i32, i32* @ECHONL
%33 = or i32 %32, %31
%34 = load i32, i32* @ICANON
%35 = or i32 %33, %34
%36 = load i32, i32* @ISIG
%37 = or i32 %35, %36
%38 = load i32, i32* @IEXTEN
%39 = or i32 %37, %38
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%42 = load i32, i32* %41
%43 = and i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* @CSIZE
%45 = load i32, i32* @PARENB
%46 = or i32 %45, %44
%47 = load i32, i32* @CSTOPB
%48 = or i32 %46, %47
%49 = xor i32 %48, -1
%50 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%51 = load i32, i32* %50
%52 = and i32 %51, %49
%53 = load i32, i32* @CS8
%54 = load i32, i32* @CREAD
%55 = or i32 %53, %52
%56 = or i32 %55, %54
store i32 %56, i32* %50
%57 = load i32, i32* @TCSANOW
%58 = call i32 @tcsetattr(i32 %0, i32 %57, %struct.termios* %2)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %6, %1
%61 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %6 ]
%62 = call i32 @perror(i8* %61)
br label %63
63: ; preds = %60, %6
%64 = phi i32 [ 0, %6 ], [ -1, %60 ]
ret i32 %64
}