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

/* Variables and functions */
int B4800;
int CLOCAL;
int CREAD;
int CRTSCTS;
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 /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_serial_options(int fd) {
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  if (tcgetattr(fd, &tty) != 0) {
    return -1;
  }
  cfsetospeed(&tty, B4800);
  cfsetispeed(&tty, B4800);
  tty.c_iflag &= ~IGNBRK;
  tty.c_lflag = 0;
  tty.c_cc[VMIN] = 1;
  tty.c_cc[VTIME] = 50;
  tty.c_iflag &= ~(IXON | IXOFF | IXANY);
  tty.c_cflag |= (CLOCAL | CREAD);
  tty.c_cflag &= ~(PARENB | PARODD);
  tty.c_cflag |= B4800;
  tty.c_cflag &= ~CSTOPB;
  tty.c_cflag &= ~CRTSCTS;
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_serial_options>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     118d <set_serial_options+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <set_serial_options+0x161>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dda(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2da5(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x32,(%rax,%rcx,4)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    0x2de3(%rip),%rcx
or     (%rcx),%eax
mov    0x2d92(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    0x2d8f(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    0x2d87(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     12aa <set_serial_options+0x15a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12b1 <set_serial_options+0x161>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_serial_options>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <tcgetattr@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    126c <set_serial_options+0x11c>
mov    0x2e32(%rip),%rbx
mov    (%rbx),%esi
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1070 <cfsetispeed@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x8(%rsp),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2db7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x32,(%rax,%rcx,4)
mov    0x2df6(%rip),%rax
mov    0x2df7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2da4(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2d66(%rip),%rax
mov    0x2da7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     0x10(%rsp),%ecx
mov    0x2db8(%rip),%rax
mov    0x2da1(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
not    %edx
and    %ecx,%edx
or     (%rbx),%edx
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    0x2d3f(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %edx,%eax
mov    %eax,0x10(%rsp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,1),%eax
add    $0xfffffffe,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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