#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 */
typedef int /*<<< orphan*/ tty;
struct termios {
  int c_cflag;
  int c_lflag;
  int *c_cc;
  int c_iflag;
};

/* Variables and functions */
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int ECHO;
int ICANON;
int ISIG;
int IXANY;
int IXOFF;
int IXON;
int PARENB;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_interface_attribs(int fd, int speed, int parity, int waitTime) {
  int isBlockingMode;
  struct termios tty;
  isBlockingMode = 0;
  if (waitTime < 0 || waitTime > 255) {
    isBlockingMode = 1;
  }
  memset(&tty, 0, sizeof(tty));
  if (tcgetattr(fd, &tty) != 0) {
    printf("error \n");
    return -1;
  }
  cfsetospeed(&tty, speed);
  cfsetispeed(&tty, speed);
  tty.c_cflag &= ~PARENB;
  tty.c_cflag &= ~CSTOPB;
  tty.c_cflag &= ~CSIZE;
  tty.c_cflag |= ~CS8;
  tty.c_cflag &= ~CRTSCTS;
  tty.c_lflag &= ~(ICANON | ECHO | ISIG);
  tty.c_cc[VMIN] = (1 == isBlockingMode) ? 1 : 0;
  tty.c_cc[VTIME] = (1 == isBlockingMode) ? 0 : waitTime;
  tty.c_iflag &= ~(IXON | IXOFF | IXANY);
  tty.c_cflag |= (CLOCAL | CREAD);
  tty.c_cflag |= parity;
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    printf("error \n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_interface_attribs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1192 <set_interface_attribs+0x32>
cmpl   $0xff,-0x14(%rbp)
jle    1199 <set_interface_attribs+0x39>
movl   $0x1,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
je     11d7 <set_interface_attribs+0x77>
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <set_interface_attribs+0x1e2>
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    0x2d6f(%rip),%rcx
or     (%rcx),%eax
mov    0x2d5e(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%ecx
mov    $0x1,%eax
xor    %edx,%edx
cmp    %ecx,%eax
cmove  %eax,%edx
mov    -0x28(%rbp),%rax
mov    0x2d21(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    $0x1,%eax
cmp    -0x18(%rbp),%eax
jne    12a5 <set_interface_attribs+0x145>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    12ab <set_interface_attribs+0x14b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rax
mov    0x2ccf(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    0x2d19(%rip),%rcx
or     (%rcx),%eax
mov    0x2cb8(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2c76(%rip),%rax
mov    (%rax),%eax
mov    0x2cc5(%rip),%rcx
or     (%rcx),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cc2(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     133b <set_interface_attribs+0x1db>
lea    0xcd6(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <set_interface_attribs+0x1e2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_interface_attribs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r13d
mov    %edx,%r12d
mov    %esi,%ebp
mov    %edi,%r14d
mov    %rsp,%rbx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <tcgetattr@plt>
test   %rax,%rax
jne    1299 <set_interface_attribs+0x139>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <cfsetospeed@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <cfsetispeed@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2d9f(%rip),%rcx
or     (%rcx),%eax
mov    0x2dce(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    (%rsp),%eax
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
or     %eax,%ecx
mov    0x2d93(%rip),%rax
mov    0x2dd4(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    0x2dc1(%rip),%rax
or     (%rax),%edx
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
not    %edx
and    %edx,0x4(%rsp)
not    %eax
and    %ecx,%eax
xor    %ecx,%ecx
cmp    $0x100,%r13d
setae  %cl
mov    %eax,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2d79(%rip),%rdx
mov    (%rdx),%rdx
mov    %ecx,(%rax,%rdx,4)
cmovae %r15d,%r13d
mov    0x2d48(%rip),%rcx
mov    (%rcx),%rcx
mov    %r13d,(%rax,%rcx,4)
mov    0x2d92(%rip),%rax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d30(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x10(%rsp)
mov    0x2cf1(%rip),%rax
mov    0x2d42(%rip),%rcx
or     (%rax),%r12d
or     (%rcx),%r12d
or     %r12d,(%rsp)
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1070 <tcsetattr@plt>
test   %rax,%rax
je     12ab <set_interface_attribs+0x14b>
lea    0xd60(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i32 }
@.str = [8 x i8] c"error \0A\00"
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CRTSCTS = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
define i32 @set_interface_attribs(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.termios
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* %9
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %4
%15 = load i32, i32* %9
%16 = icmp sgt i32 %15, 255
br i1 %16, label %17, label %18
17: ; preds = %14, %4
store i32 1, i32* %10
br label %18
18: ; preds = %17, %14
%19 = call i32 @memset(%struct.termios* %11, i32 0, i32 24)
%20 = load i32, i32* %6
%21 = call i64 @tcgetattr(i32 %20, %struct.termios* %11)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %109
25: ; preds = %18
%26 = load i32, i32* %7
%27 = call i32 @cfsetospeed(%struct.termios* %11, i32 %26)
%28 = load i32, i32* %7
%29 = call i32 @cfsetispeed(%struct.termios* %11, i32 %28)
%30 = load i32, i32* @PARENB
%31 = xor i32 %30, -1
%32 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* @CSTOPB
%36 = xor i32 %35, -1
%37 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%38 = load i32, i32* %37
%39 = and i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* @CSIZE
%41 = xor i32 %40, -1
%42 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%43 = load i32, i32* %42
%44 = and i32 %43, %41
store i32 %44, i32* %42
%45 = load i32, i32* @CS8
%46 = xor i32 %45, -1
%47 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%48 = load i32, i32* %47
%49 = or i32 %48, %46
store i32 %49, i32* %47
%50 = load i32, i32* @CRTSCTS
%51 = xor i32 %50, -1
%52 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%53 = load i32, i32* %52
%54 = and i32 %53, %51
store i32 %54, i32* %52
%55 = load i32, i32* @ICANON
%56 = load i32, i32* @ECHO
%57 = or i32 %55, %56
%58 = load i32, i32* @ISIG
%59 = or i32 %57, %58
%60 = xor i32 %59, -1
%61 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 1
%62 = load i32, i32* %61
%63 = and i32 %62, %60
store i32 %63, i32* %61
%64 = load i32, i32* %10
%65 = icmp eq i32 1, %64
%66 = zext i1 %65 to i64
%67 = select i1 %65, i32 1, i32 0
%68 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 2
%69 = load i32*, i32** %68
%70 = load i64, i64* @VMIN
%71 = getelementptr inbounds i32, i32* %69, i64 %70
store i32 %67, i32* %71
%72 = load i32, i32* %10
%73 = icmp eq i32 1, %72
br i1 %73, label %74, label %75
74: ; preds = %25
br label %77
75: ; preds = %25
%76 = load i32, i32* %9
br label %77
77: ; preds = %75, %74
%78 = phi i32 [ 0, %74 ], [ %76, %75 ]
%79 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 2
%80 = load i32*, i32** %79
%81 = load i64, i64* @VTIME
%82 = getelementptr inbounds i32, i32* %80, i64 %81
store i32 %78, i32* %82
%83 = load i32, i32* @IXON
%84 = load i32, i32* @IXOFF
%85 = or i32 %83, %84
%86 = load i32, i32* @IXANY
%87 = or i32 %85, %86
%88 = xor i32 %87, -1
%89 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 3
%90 = load i32, i32* %89
%91 = and i32 %90, %88
store i32 %91, i32* %89
%92 = load i32, i32* @CLOCAL
%93 = load i32, i32* @CREAD
%94 = or i32 %92, %93
%95 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%96 = load i32, i32* %95
%97 = or i32 %96, %94
store i32 %97, i32* %95
%98 = load i32, i32* %8
%99 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
%100 = load i32, i32* %99
%101 = or i32 %100, %98
store i32 %101, i32* %99
%102 = load i32, i32* %6
%103 = load i32, i32* @TCSANOW
%104 = call i64 @tcsetattr(i32 %102, i32 %103, %struct.termios* %11)
%105 = icmp ne i64 %104, 0
br i1 %105, label %106, label %108
106: ; preds = %77
%107 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %109
108: ; preds = %77
store i32 0, i32* %5
br label %109
109: ; preds = %108, %106, %23
%110 = load i32, i32* %5
ret i32 %110
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i32 }
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CRTSCTS = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
@str.1 = [7 x i8] c"error \00"
define i32 @set_interface_attribs(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.termios
%6 = bitcast %struct.termios* %5 to i8*
%7 = icmp ugt i32 %3, 255
%8 = call i32 @memset(%struct.termios* %5, i32 0, i32 24)
%9 = call i64 @tcgetattr(i32 %0, %struct.termios* %5)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %64
11: ; preds = %4
%12 = zext i1 %7 to i32
%13 = call i32 @cfsetospeed(%struct.termios* %5, i32 %1)
%14 = call i32 @cfsetispeed(%struct.termios* %5, i32 %1)
%15 = load i32, i32* @PARENB
%16 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @CSTOPB
%19 = or i32 %15, %18
%20 = load i32, i32* @CSIZE
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = and i32 %17, %22
%24 = load i32, i32* @CS8
%25 = xor i32 %24, -1
%26 = or i32 %23, %25
%27 = load i32, i32* @CRTSCTS
%28 = xor i32 %27, -1
%29 = and i32 %26, %28
store i32 %29, i32* %16
%30 = load i32, i32* @ICANON
%31 = load i32, i32* @ECHO
%32 = or i32 %31, %30
%33 = load i32, i32* @ISIG
%34 = or i32 %32, %33
%35 = xor i32 %34, -1
%36 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 1
%37 = load i32, i32* %36
%38 = and i32 %37, %35
store i32 %38, i32* %36
%39 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 2
%40 = load i32*, i32** %39
%41 = load i64, i64* @VMIN
%42 = getelementptr inbounds i32, i32* %40, i64 %41
store i32 %12, i32* %42
%43 = select i1 %7, i32 0, i32 %3
%44 = load i64, i64* @VTIME
%45 = getelementptr inbounds i32, i32* %40, i64 %44
store i32 %43, i32* %45
%46 = load i32, i32* @IXON
%47 = load i32, i32* @IXOFF
%48 = or i32 %47, %46
%49 = load i32, i32* @IXANY
%50 = or i32 %48, %49
%51 = xor i32 %50, -1
%52 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 3
%53 = load i32, i32* %52
%54 = and i32 %53, %51
store i32 %54, i32* %52
%55 = load i32, i32* @CLOCAL
%56 = load i32, i32* @CREAD
%57 = load i32, i32* %16
%58 = or i32 %55, %2
%59 = or i32 %58, %56
%60 = or i32 %59, %57
store i32 %60, i32* %16
%61 = load i32, i32* @TCSANOW
%62 = call i64 @tcsetattr(i32 %0, i32 %61, %struct.termios* %5)
%63 = icmp eq i64 %62, 0
br i1 %63, label %67, label %64
64: ; preds = %11, %4
%65 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @str.1, i64 0, i64 0), %11 ]
%66 = call i32 @puts(i8* dereferenceable(1) %65)
br label %67
67: ; preds = %64, %11
%68 = phi i32 [ 0, %11 ], [ -1, %64 ]
ret i32 %68
}