#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_cflag;
  int c_iflag;
  scalar_t__ *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*/ stdout;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int setup_serial_interface(int fd, int speed, int parity) {
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  if (tcgetattr(fd, &tty) != 0) {
    fprintf(stdout, "Error %d while reading TTY attributes.", errno);
    return -1;
  }
  cfsetospeed(&tty, speed);
  cfsetispeed(&tty, speed);
  tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
  tty.c_cflag |= (CLOCAL | CREAD);
  tty.c_cflag &= ~(PARENB | PARODD);
  tty.c_cflag |= parity;
  tty.c_cflag &= ~CSTOPB;
  tty.c_cflag &= ~CRTSCTS;
  tty.c_iflag &= ~IGNBRK;
  tty.c_iflag &= ~(IXON | IXOFF | IXANY);
  tty.c_lflag = 0;
  tty.c_oflag = 0;
  tty.c_cc[VMIN] = 0;
  tty.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    fprintf(stdout, "Error %d while writing TTY attributes.", errno);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_serial_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
je     11c1 <setup_serial_interface+0x61>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
lea    0xe50(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <setup_serial_interface+0x1b8>
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    -0x30(%rbp),%eax
mov    0x2dc5(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2dc7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    0x2dba(%rip),%rcx
or     (%rcx),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    0x2daa(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    0x2d70(%rip),%rcx
or     (%rcx),%eax
mov    0x2d17(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d09(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    0x2cbb(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x8(%rbp),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     1311 <setup_serial_interface+0x1b1>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
mov    0x2c99(%rip),%rax
mov    (%rax),%edx
lea    0xd27(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <setup_serial_interface+0x1b8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_serial_interface>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
mov    %edi,%r12d
lea    0x8(%rsp),%rbx
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1060 <tcgetattr@plt>
test   %rax,%rax
je     11a9 <setup_serial_interface+0x49>
lea    0xe5c(%rip),%rsi
jmp    129a <setup_serial_interface+0x13a>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1080 <cfsetispeed@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2de0(%rip),%rcx
or     (%rcx),%eax
mov    0x2d77(%rip),%rcx
mov    0x2dd8(%rip),%rdx
or     (%rcx),%eax
or     (%rdx),%eax
mov    0x2de5(%rip),%rcx
mov    0x2dce(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
or     %r15d,%edx
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    0x2d53(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %edx,%eax
mov    %eax,0x8(%rsp)
mov    0x2d5a(%rip),%rax
mov    0x2db3(%rip),%rcx
mov    0x2db4(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d59(%rip),%rcx
or     (%rcx),%edx
or     (%rax),%edx
not    %edx
and    %edx,0xc(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2d53(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2d09(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
test   %rax,%rax
je     12b7 <setup_serial_interface+0x157>
lea    0xd8d(%rip),%rsi
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%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, i64*, i64, i64 }
@stdout = global i32 0
@.str = [39 x i8] c"Error %d while reading TTY attributes.\00"
@errno = global i32 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
@IGNBRK = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [39 x i8] c"Error %d while writing TTY attributes.\00"
define i32 @setup_serial_interface(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = call i32 @memset(%struct.termios* %8, i32 0, i32 32)
%10 = load i32, i32* %5
%11 = call i64 @tcgetattr(i32 %10, %struct.termios* %8)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i32, i32* @stdout
%15 = load i32, i32* @errno
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 -1, i32* %4
br label %90
17: ; preds = %3
%18 = load i32, i32* %6
%19 = call i32 @cfsetospeed(%struct.termios* %8, i32 %18)
%20 = load i32, i32* %6
%21 = call i32 @cfsetispeed(%struct.termios* %8, i32 %20)
%22 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @CSIZE
%25 = xor i32 %24, -1
%26 = and i32 %23, %25
%27 = load i32, i32* @CS8
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @CLOCAL
%31 = load i32, i32* @CREAD
%32 = or i32 %30, %31
%33 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = or i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* @PARENB
%37 = load i32, i32* @PARODD
%38 = or i32 %36, %37
%39 = xor i32 %38, -1
%40 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, %39
store i32 %42, i32* %40
%43 = load i32, i32* %7
%44 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%45 = load i32, i32* %44
%46 = or i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* @CSTOPB
%48 = xor i32 %47, -1
%49 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%50 = load i32, i32* %49
%51 = and i32 %50, %48
store i32 %51, i32* %49
%52 = load i32, i32* @CRTSCTS
%53 = xor i32 %52, -1
%54 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%55 = load i32, i32* %54
%56 = and i32 %55, %53
store i32 %56, i32* %54
%57 = load i32, i32* @IGNBRK
%58 = xor i32 %57, -1
%59 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 1
%60 = load i32, i32* %59
%61 = and i32 %60, %58
store i32 %61, i32* %59
%62 = load i32, i32* @IXON
%63 = load i32, i32* @IXOFF
%64 = or i32 %62, %63
%65 = load i32, i32* @IXANY
%66 = or i32 %64, %65
%67 = xor i32 %66, -1
%68 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 1
%69 = load i32, i32* %68
%70 = and i32 %69, %67
store i32 %70, i32* %68
%71 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 4
store i64 0, i64* %71
%72 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 3
store i64 0, i64* %72
%73 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 2
%74 = load i64*, i64** %73
%75 = load i64, i64* @VMIN
%76 = getelementptr inbounds i64, i64* %74, i64 %75
store i64 0, i64* %76
%77 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 2
%78 = load i64*, i64** %77
%79 = load i64, i64* @VTIME
%80 = getelementptr inbounds i64, i64* %78, i64 %79
store i64 0, i64* %80
%81 = load i32, i32* %5
%82 = load i32, i32* @TCSANOW
%83 = call i64 @tcsetattr(i32 %81, i32 %82, %struct.termios* %8)
%84 = icmp ne i64 %83, 0
br i1 %84, label %85, label %89
85: ; preds = %17
%86 = load i32, i32* @stdout
%87 = load i32, i32* @errno
%88 = call i32 @fprintf(i32 %86, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %87)
store i32 -1, i32* %4
br label %90
89: ; preds = %17
store i32 0, i32* %4
br label %90
90: ; preds = %89, %85, %13
%91 = load i32, i32* %4
ret i32 %91
}

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