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

/* Variables and functions */
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int ECHO;
int ECHOE;
int ICANON;
int ISIG;
int /*<<< orphan*/ IXON;
int /*<<< orphan*/ OPOST;
int PARENB;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VTIME;
scalar_t__ cfsetispeed(struct termios *, int);
scalar_t__ cfsetospeed(struct termios *, int);
scalar_t__ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int configure_ser(int fd, int speed) {
  struct termios options;
  if (tcgetattr(fd, &options) < 0)
    return -1;
  if (cfsetispeed(&options, speed) < 0)
    return -1;
  if (cfsetospeed(&options, speed) < 0)
    return -1;
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CRTSCTS;
  options.c_iflag &= ~IXON;
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  options.c_oflag &= ~OPOST;
  options.c_cc[VTIME] = 6;
  if (tcflush(fd, TCIFLUSH) < 0)
    return -1;
  return tcsetattr(fd, TCSANOW, &options);
}


x86 assembly (clang, -O0):
<configure_ser>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1180 <configure_ser+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <configure_ser+0x176>
mov    -0xc(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1070 <cfsetispeed@plt>
cmp    $0x0,%rax
jge    11a2 <configure_ser+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <configure_ser+0x176>
mov    -0xc(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1030 <cfsetospeed@plt>
cmp    $0x0,%rax
jge    11c4 <configure_ser+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <configure_ser+0x176>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    0x2ddc(%rip),%rcx
or     (%rcx),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    0x2d85(%rip),%rcx
or     (%rcx),%eax
mov    0x2d74(%rip),%rcx
or     (%rcx),%eax
mov    0x2d63(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d0b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x6,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
cmp    $0x0,%rax
jge    12ae <configure_ser+0x15e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <configure_ser+0x176>
mov    -0x8(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<configure_ser>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1050 <tcgetattr@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     1270 <configure_ser+0x120>
lea    0x8(%rsp),%rdi
mov    %ebx,%esi
call   1070 <cfsetispeed@plt>
test   %rax,%rax
js     1270 <configure_ser+0x120>
lea    0x8(%rsp),%rdi
mov    %ebx,%esi
call   1030 <cfsetospeed@plt>
test   %rax,%rax
js     1270 <configure_ser+0x120>
mov    0x2dc2(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     0x8(%rsp),%ecx
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d95(%rip),%rdx
or     (%rdx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,0x8(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x1c(%rsp)
mov    0x2d85(%rip),%rax
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2db3(%rip),%rax
or     (%rax),%ecx
mov    0x2da2(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0xc(%rsp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2d4f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x6,(%rax,%rcx,4)
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
test   %rax,%rax
js     1270 <configure_ser+0x120>
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1060 <tcsetattr@plt>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i32, i32 }
@CLOCAL = global i32 0
@CREAD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@PARENB = global i32 0
@CRTSCTS = global i32 0
@IXON = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@OPOST = global i32 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @configure_ser(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = call i64 @tcgetattr(i32 %7, %struct.termios* %6)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %81
11: ; preds = %2
%12 = load i32, i32* %5
%13 = call i64 @cfsetispeed(%struct.termios* %6, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 -1, i32* %3
br label %81
16: ; preds = %11
%17 = load i32, i32* %5
%18 = call i64 @cfsetospeed(%struct.termios* %6, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %3
br label %81
21: ; preds = %16
%22 = load i32, i32* @CLOCAL
%23 = load i32, i32* @CREAD
%24 = or i32 %22, %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = or i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* @CSIZE
%29 = xor i32 %28, -1
%30 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = and i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @CS8
%34 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = or i32 %35, %33
store i32 %36, i32* %34
%37 = load i32, i32* @PARENB
%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* @CRTSCTS
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* @IXON
%48 = xor i32 %47, -1
%49 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
%50 = load i32, i32* %49
%51 = and i32 %50, %48
store i32 %51, i32* %49
%52 = load i32, i32* @ICANON
%53 = load i32, i32* @ECHO
%54 = or i32 %52, %53
%55 = load i32, i32* @ECHOE
%56 = or i32 %54, %55
%57 = load i32, i32* @ISIG
%58 = or i32 %56, %57
%59 = xor i32 %58, -1
%60 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%61 = load i32, i32* %60
%62 = and i32 %61, %59
store i32 %62, i32* %60
%63 = load i32, i32* @OPOST
%64 = xor i32 %63, -1
%65 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%66 = load i32, i32* %65
%67 = and i32 %66, %64
store i32 %67, i32* %65
%68 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%69 = load i32*, i32** %68
%70 = load i64, i64* @VTIME
%71 = getelementptr inbounds i32, i32* %69, i64 %70
store i32 6, i32* %71
%72 = load i32, i32* %4
%73 = load i32, i32* @TCIFLUSH
%74 = call i64 @tcflush(i32 %72, i32 %73)
%75 = icmp slt i64 %74, 0
br i1 %75, label %76, label %77
76: ; preds = %21
store i32 -1, i32* %3
br label %81
77: ; preds = %21
%78 = load i32, i32* %4
%79 = load i32, i32* @TCSANOW
%80 = call i32 @tcsetattr(i32 %78, i32 %79, %struct.termios* %6)
store i32 %80, i32* %3
br label %81
81: ; preds = %77, %76, %20, %15, %10
%82 = load i32, i32* %3
ret i32 %82
}

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