#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_iflag;
  int *c_cc;
  scalar_t__ c_oflag;
  scalar_t__ c_lflag;
};

/* Variables and functions */
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int IGNBRK;
int IXANY;
int IXOFF;
int IXON;
int PARENB;
int PARODD;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_serial_config(int fd, int speed, int parity, int blocking) {
  struct termios tty;
  memset(&tty, 0, sizeof(tty));
  if (tcgetattr(fd, &tty) != 0) {
    fprintf(stderr, "error %d from tcgetattr", errno);
    return -1;
  }
  cfsetospeed(&tty, speed);
  cfsetispeed(&tty, speed);
  tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
  tty.c_iflag &= ~IGNBRK;
  tty.c_lflag = 0;
  tty.c_oflag = 0;
  tty.c_cc[VMIN] = blocking ? 1 : 0;
  tty.c_cc[VTIME] = 5;
  tty.c_iflag &= ~(IXON | IXOFF | IXANY);
  tty.c_cflag |= (CLOCAL | CREAD);
  tty.c_cflag &= ~(PARENB | PARODD);
  tty.c_cflag |= parity;
  tty.c_cflag &= ~CSTOPB;
  tty.c_cflag &= ~CRTSCTS;
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    fprintf(stderr, "error %d from tcsetattr", errno);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_serial_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
je     11c4 <set_serial_config+0x64>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    0xe4d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <set_serial_config+0x1c5>
mov    -0xc(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0xc(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x38(%rbp),%eax
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2dc4(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0x30(%rbp),%rax
mov    0x2d7a(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2d31(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    0x2d87(%rip),%rcx
or     (%rcx),%eax
mov    0x2d2e(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
mov    0x2d3b(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    0x2d2b(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2ca8(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c9e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     131e <set_serial_config+0x1be>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
mov    0x2c84(%rip),%rax
mov    (%rax),%edx
lea    0xd0b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <set_serial_config+0x1c5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_serial_config>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r12d
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%r13d
lea    0x8(%rsp),%rbx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1060 <tcgetattr@plt>
test   %rax,%rax
je     11ac <set_serial_config+0x4c>
lea    0xe59(%rip),%rsi
jmp    12ad <set_serial_config+0x14d>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <cfsetispeed@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2ddd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0xc(%rsp)
xor    %eax,%eax
test   %r12d,%r12d
setne  %al
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x10(%rsp),%rcx
mov    0x2da4(%rip),%rdx
mov    (%rdx),%rdx
mov    %eax,(%rcx,%rdx,4)
mov    0x2d5f(%rip),%rax
mov    (%rax),%rax
movl   $0x5,(%rcx,%rax,4)
mov    0x2db6(%rip),%rax
mov    0x2db7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d5c(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0xc(%rsp)
mov    0x2d0d(%rip),%rax
mov    0x2d6e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     0x8(%rsp),%ecx
mov    0x2d77(%rip),%rax
mov    0x2d60(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
not    %edx
and    %ecx,%edx
or     %r14d,%edx
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
mov    0x2ce5(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %edx,%eax
mov    %eax,0x8(%rsp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    %rbx,%rdx
call   1070 <tcsetattr@plt>
test   %rax,%rax
je     12ca <set_serial_config+0x16a>
lea    0xd6b(%rip),%rsi
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
mov    0x2ccb(%rip),%rax
mov    (%rax),%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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