#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;
  scalar_t__ *c_cc;
};
typedef int /*<<< orphan*/ newtio;

/* Variables and functions */
int /*<<< orphan*/ B19200;
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int PARENB;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ bzero(struct termios *, int);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_opt(int fd) {
  struct termios newtio, oldtio;
  if (tcgetattr(fd, &oldtio) != 0) {
    perror("Setup Serial");
    return -1;
  }
  bzero(&newtio, sizeof(newtio));
  newtio.c_cflag |= CLOCAL | CREAD;
  newtio.c_cflag &= ~CSIZE;
  newtio.c_cflag |= CS8;
  newtio.c_cflag &= ~PARENB;
  cfsetispeed(&newtio, B19200);
  cfsetospeed(&newtio, B19200);
  newtio.c_cflag &= ~CSTOPB;
  newtio.c_cc[VTIME] = 0;
  newtio.c_cc[VMIN] = 0;
  tcflush(fd, TCIOFLUSH);
  if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
    perror("com set error");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_opt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     11a9 <set_opt+0x39>
lea    0xe68(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <set_opt+0x143>
lea    -0x18(%rbp),%rdi
mov    $0x10,%esi
call   1090 <bzero@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    0x2e09(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d5d(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    0x2d67(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x8(%rbp),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x8(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     12ac <set_opt+0x13c>
lea    0xd72(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <set_opt+0x143>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_opt>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
lea    0x10(%rsp),%rsi
call   1050 <tcgetattr@plt>
test   %rax,%rax
jne    1260 <set_opt+0xf0>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x10,%esi
call   1090 <bzero@plt>
mov    0x2de4(%rip),%rax
mov    0x2e25(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     (%rsp),%ecx
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2e02(%rip),%rcx
or     (%rcx),%eax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,(%rsp)
mov    0x2e11(%rip),%rbx
mov    (%rbx),%esi
mov    %r14,%rdi
call   1070 <cfsetispeed@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2da1(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1269 <set_opt+0xf9>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd99(%rip),%rdi
jmp    1270 <set_opt+0x100>
lea    0xd9d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1257 <set_opt+0xe7>

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

LLVM IR (-O2):
%struct.termios = type { i32, i64* }
@.str = [13 x i8] c"Setup Serial\00"
@CLOCAL = global i32 0
@CREAD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@PARENB = global i32 0
@B19200 = global i32 0
@CSTOPB = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [14 x i8] c"com set error\00"
define i32 @set_opt(i32 %0) {
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %2 to i8*
%5 = bitcast %struct.termios* %3 to i8*
%6 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %43
8: ; preds = %1
%9 = call i32 @bzero(%struct.termios* %2, i32 16)
%10 = load i32, i32* @CLOCAL
%11 = load i32, i32* @CREAD
%12 = or i32 %11, %10
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = or i32 %12, %14
%16 = load i32, i32* @CSIZE
%17 = xor i32 %16, -1
%18 = and i32 %15, %17
%19 = load i32, i32* @CS8
%20 = or i32 %19, %18
%21 = load i32, i32* @PARENB
%22 = xor i32 %21, -1
%23 = and i32 %20, %22
store i32 %23, i32* %13
%24 = load i32, i32* @B19200
%25 = call i32 @cfsetispeed(%struct.termios* %2, i32 %24)
%26 = load i32, i32* @B19200
%27 = call i32 @cfsetospeed(%struct.termios* %2, i32 %26)
%28 = load i32, i32* @CSTOPB
%29 = xor i32 %28, -1
%30 = load i32, i32* %13
%31 = and i32 %30, %29
store i32 %31, i32* %13
%32 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%33 = load i64*, i64** %32
%34 = load i64, i64* @VTIME
%35 = getelementptr inbounds i64, i64* %33, i64 %34
store i64 0, i64* %35
%36 = load i64, i64* @VMIN
%37 = getelementptr inbounds i64, i64* %33, i64 %36
store i64 0, i64* %37
%38 = load i32, i32* @TCIOFLUSH
%39 = call i32 @tcflush(i32 %0, i32 %38)
%40 = load i32, i32* @TCSANOW
%41 = call i64 @tcsetattr(i32 %0, i32 %40, %struct.termios* %2)
%42 = icmp eq i64 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %8, %1
%44 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %8 ]
%45 = call i32 @perror(i8* %44)
br label %46
46: ; preds = %43, %8
%47 = phi i32 [ 0, %8 ], [ -1, %43 ]
ret i32 %47
}