#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_cc;
  int c_cflag;
};
typedef int int32_t;

/* Variables and functions */
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int PARENB;
int PARODD;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int32_t serial_set_interface(int32_t fd, int32_t speedFlags, int32_t parity,
                             int32_t rtscts, int32_t nbstop) {
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  if (tcgetattr(fd, &tty) != 0) {
    perror("error from tcgetattr");
    return -1;
  }
  cfsetospeed(&tty, speedFlags);
  cfsetispeed(&tty, speedFlags);
  cfmakeraw(&tty);
  tty.c_cc[VMIN] = 0;
  tty.c_cc[VTIME] = 5;
  tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
  tty.c_cflag |= (CLOCAL | CREAD);
  tty.c_cflag &= ~(PARENB | PARODD);
  tty.c_cflag |= parity;
  if (nbstop <= 1) {
    tty.c_cflag &= ~CSTOPB;
  } else {
    tty.c_cflag |= CSTOPB;
  }
  if (rtscts == 0) {
    tty.c_cflag &= ~CRTSCTS;
  } else {
    tty.c_cflag |= CRTSCTS;
  }
  tcflush(fd, TCIFLUSH);
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    perror("error from tcsetattr");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<serial_set_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
je     11d6 <serial_set_interface+0x56>
lea    0xe3b(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <serial_set_interface+0x1a9>
mov    -0xc(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0xc(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1090 <cfsetispeed@plt>
lea    -0x28(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    -0x28(%rbp),%rax
mov    0x2dbe(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2d89(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2d8f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    0x2d82(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    0x2d72(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x18(%rbp)
jg     129b <serial_set_interface+0x11b>
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    12aa <serial_set_interface+0x12a>
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    12cb <serial_set_interface+0x14b>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    12da <serial_set_interface+0x15a>
mov    0x2cbe(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cc4(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x8(%rbp),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
je     1322 <serial_set_interface+0x1a2>
lea    0xd04(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <serial_set_interface+0x1a9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_set_interface>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r12d
mov    %ecx,%r15d
mov    %edx,%r13d
mov    %esi,%r14d
mov    %edi,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <tcgetattr@plt>
test   %rax,%rax
jne    12bf <serial_set_interface+0x13f>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <cfsetospeed@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1090 <cfsetispeed@plt>
mov    %rbx,%rdi
call   1060 <cfmakeraw@plt>
mov    0x8(%rsp),%rax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d9d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x10(%rsp),%eax
mov    0x2da5(%rip),%rcx
or     (%rcx),%eax
mov    0x2d54(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9b(%rip),%rcx
or     (%rcx),%eax
mov    0x2daa(%rip),%rcx
mov    0x2d93(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
or     %r13d,%edx
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
or     %edx,%ecx
not    %eax
and    %edx,%eax
cmp    $0x2,%r12d
cmovge %ecx,%eax
mov    0x2d21(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,%edx
or     %eax,%edx
not    %ecx
and    %eax,%ecx
test   %r15d,%r15d
cmovne %edx,%ecx
mov    %ecx,0x10(%rsp)
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1080 <tcsetattr@plt>
test   %rax,%rax
mov    $0x0,%eax
jne    12c8 <serial_set_interface+0x148>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3a(%rip),%rdi
jmp    12cf <serial_set_interface+0x14f>
lea    0xd46(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    12b0 <serial_set_interface+0x130>

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

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