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

/* Variables and functions */
int BRKINT;
int CREAD;
int CS8;
int ECHO;
int ICANON;
int ICRNL;
int IEXTEN;
int IGNBRK;
int IGNCR;
int IGNPAR;
int INLCR;
int INPCK;
int ISIG;
int ISTRIP;
int IXOFF;
int IXON;
int /*<<< orphan*/ OPOST;
int PARMRK;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ perror(char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_termios(struct termios *old, int fd) {
  if (tcgetattr(fd, old) < 0) {
    perror("cannot get tty attributes");
    return -1;
  }
  struct termios new = *old;
  new.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | IGNPAR | INLCR | INPCK |
                   ISTRIP | IXOFF | IXON | PARMRK);
  new.c_oflag &= ~(OPOST);
  new.c_cflag |= (CREAD | CS8);
  new.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
  new.c_cc[VMIN] = 1;
  new.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSAFLUSH, &new) < 0) {
    perror("cannot set tty attributes");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_termios>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0x0,%rax
jge    116d <set_termios+0x3d>
lea    0xea4(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <set_termios+0x18b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,-0x38(%rbp)
mov    0x8(%rax),%rcx
mov    %rcx,-0x30(%rbp)
mov    0x10(%rax),%rcx
mov    %rcx,-0x28(%rbp)
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
mov    0x2daf(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0e(%rip),%rcx
or     (%rcx),%eax
mov    0x2d8d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e14(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9b(%rip),%rcx
or     (%rcx),%eax
mov    0x2d8a(%rip),%rcx
or     (%rcx),%eax
mov    0x2de9(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc8(%rip),%rcx
or     (%rcx),%eax
mov    0x2dff(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    0x2d6a(%rip),%rcx
or     (%rcx),%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    0x2d0a(%rip),%rcx
or     (%rcx),%eax
mov    0x2d61(%rip),%rcx
or     (%rcx),%eax
mov    0x2d30(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d22(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2cdd(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x14(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0x0,%rax
jge    12b4 <set_termios+0x184>
lea    0xd77(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <set_termios+0x18b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_termios>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%edi
mov    %rbx,%rsi
call   1030 <tcgetattr@plt>
test   %rax,%rax
js     1260 <set_termios+0x130>
mov    0x2e43(%rip),%rcx
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2df1(%rip),%rcx
or     (%rcx),%eax
mov    0x2e50(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcf(%rip),%rcx
or     (%rcx),%eax
mov    0x2e56(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddd(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcc(%rip),%rcx
or     (%rcx),%eax
movups (%rbx),%xmm0
movups 0x10(%rbx),%xmm1
mov    0x2e24(%rip),%rcx
or     (%rcx),%eax
movaps %xmm0,(%rsp)
mov    0x2dff(%rip),%rcx
or     (%rcx),%eax
movaps %xmm1,0x10(%rsp)
mov    0x2e31(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %eax,(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x2db4(%rip),%rax
mov    0x2da5(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,0x4(%rsp)
mov    0x2db6(%rip),%rax
mov    0x2d47(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d9c(%rip),%rax
or     (%rax),%ecx
mov    0x2d6b(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x10(%rsp),%rax
mov    0x2d5f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d1e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <tcsetattr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1269 <set_termios+0x139>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd99(%rip),%rdi
jmp    1270 <set_termios+0x140>
lea    0xdaa(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    1259 <set_termios+0x129>

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

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