#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_oflag;
  int c_lflag;
  int c_cflag;
};

/* Variables and functions */
int BRKINT;
int CS8;
int CSIZE;
int ECHO;
int ECHONL;
int ICANON;
int ICRNL;
int IEXTEN;
int IGNBRK;
int IGNCR;
int INLCR;
int ISIG;
int ISTRIP;
int IXON;
int ONLCR;
int OPOST;
int O_NOCTTY;
int O_RDWR;
int PARENB;
int PARMRK;
int /*<<< orphan*/ TCSANOW;
scalar_t__ cfsetispeed(struct termios *, int);
scalar_t__ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ logperror(char *);
int open(char const *, int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial(const char *device, int speed) {
  int fd;
  struct termios termios;
  if ((fd = open(device, O_RDWR | O_NOCTTY)) < 0) {
    logperror("open serial device");
    return -1;
  }
  if (tcgetattr(fd, &termios) < 0) {
    logperror("tcgetattr");
    return -1;
  }
  if (cfsetispeed(&termios, speed) < 0) {
    logperror("cfsetispeed ");
    return -1;
  }
  if (cfsetospeed(&termios, speed) < 0) {
    logperror("cfsetospeed ");
    return -1;
  }
  termios.c_iflag &=
      ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  termios.c_oflag &= ~(ONLCR | OPOST);
  termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  termios.c_cflag &= ~(CSIZE | PARENB);
  termios.c_cflag |= CS8;
  if (tcsetattr(fd, TCSANOW, &termios) < 0) {
    logperror("tcsetattr");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    0x2de5(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11ae <open_serial+0x4e>
lea    0xe63(%rip),%rdi
call   1030 <logperror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <open_serial+0x1e1>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11dc <open_serial+0x7c>
lea    0xe48(%rip),%rdi
call   1030 <logperror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <open_serial+0x1e1>
mov    -0x14(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1080 <cfsetispeed@plt>
cmp    $0x0,%rax
jge    120a <open_serial+0xaa>
lea    0xe24(%rip),%rdi
call   1030 <logperror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <open_serial+0x1e1>
mov    -0x14(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1040 <cfsetospeed@plt>
cmp    $0x0,%rax
jge    1238 <open_serial+0xd8>
lea    0xe03(%rip),%rdi
call   1030 <logperror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <open_serial+0x1e1>
mov    0x2d11(%rip),%rax
mov    (%rax),%eax
mov    0x2d48(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9f(%rip),%rcx
or     (%rcx),%eax
mov    0x2d06(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6d(%rip),%rcx
or     (%rcx),%eax
mov    0x2d5c(%rip),%rcx
or     (%rcx),%eax
mov    0x2d63(%rip),%rcx
or     (%rcx),%eax
mov    0x2d3a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cb8(%rip),%rax
mov    (%rax),%eax
mov    0x2d27(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%eax
mov    0x2ca4(%rip),%rcx
or     (%rcx),%eax
mov    0x2c83(%rip),%rcx
or     (%rcx),%eax
mov    0x2cd2(%rip),%rcx
or     (%rcx),%eax
mov    0x2ca9(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2c9f(%rip),%rax
mov    (%rax),%eax
mov    0x2cc6(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2c8c(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2c9a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    133b <open_serial+0x1db>
lea    0xd0d(%rip),%rdi
call   1030 <logperror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <open_serial+0x1e1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    0x2e71(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     12a9 <open_serial+0x149>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
js     12b2 <open_serial+0x152>
lea    0x8(%rsp),%rdi
mov    %ebp,%esi
call   1080 <cfsetispeed@plt>
test   %rax,%rax
js     12bb <open_serial+0x15b>
lea    0x8(%rsp),%rdi
mov    %ebp,%esi
call   1040 <cfsetospeed@plt>
test   %rax,%rax
js     12c4 <open_serial+0x164>
mov    0x2d81(%rip),%rax
mov    0x2dba(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e0f(%rip),%rax
or     (%rax),%ecx
mov    0x2d76(%rip),%rax
or     (%rax),%ecx
mov    0x2ddd(%rip),%rax
or     (%rax),%ecx
mov    0x2dcc(%rip),%rax
or     (%rax),%ecx
mov    0x2dd3(%rip),%rax
or     (%rax),%ecx
mov    0x2daa(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x2d2b(%rip),%rax
mov    0x2d9c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0xc(%rsp)
mov    0x2d6b(%rip),%rax
mov    0x2d1c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2cf9(%rip),%rax
or     (%rax),%ecx
mov    0x2d48(%rip),%rax
or     (%rax),%ecx
mov    0x2d1f(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x10(%rsp)
mov    0x2d18(%rip),%rax
mov    0x2d41(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x14(%rsp),%ecx
mov    0x2d08(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x14(%rsp)
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <tcsetattr@plt>
test   %rax,%rax
jns    12d5 <open_serial+0x175>
lea    0xd90(%rip),%rdi
jmp    12cb <open_serial+0x16b>
lea    0xd50(%rip),%rdi
jmp    12cb <open_serial+0x16b>
lea    0xd5a(%rip),%rdi
jmp    12cb <open_serial+0x16b>
lea    0xd5b(%rip),%rdi
jmp    12cb <open_serial+0x16b>
lea    0xd5f(%rip),%rdi
call   1030 <logperror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [19 x i8] c"open serial device\00"
@.str.1 = [10 x i8] c"tcgetattr\00"
@.str.2 = [13 x i8] c"cfsetispeed \00"
@.str.3 = [13 x i8] c"cfsetospeed \00"
@IGNBRK = global i32 0
@BRKINT = global i32 0
@PARMRK = global i32 0
@ISTRIP = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@ONLCR = global i32 0
@OPOST = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@CSIZE = global i32 0
@PARENB = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
@.str.4 = [10 x i8] c"tcsetattr\00"
define i32 @open_serial(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11)
store i32 %12, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @logperror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %93
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i64 @tcgetattr(i32 %17, %struct.termios* %7)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @logperror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %93
22: ; preds = %16
%23 = load i32, i32* %5
%24 = call i64 @cfsetispeed(%struct.termios* %7, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @logperror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %93
28: ; preds = %22
%29 = load i32, i32* %5
%30 = call i64 @cfsetospeed(%struct.termios* %7, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @logperror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %93
34: ; preds = %28
%35 = load i32, i32* @IGNBRK
%36 = load i32, i32* @BRKINT
%37 = or i32 %35, %36
%38 = load i32, i32* @PARMRK
%39 = or i32 %37, %38
%40 = load i32, i32* @ISTRIP
%41 = or i32 %39, %40
%42 = load i32, i32* @INLCR
%43 = or i32 %41, %42
%44 = load i32, i32* @IGNCR
%45 = or i32 %43, %44
%46 = load i32, i32* @ICRNL
%47 = or i32 %45, %46
%48 = load i32, i32* @IXON
%49 = or i32 %47, %48
%50 = xor i32 %49, -1
%51 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%52 = load i32, i32* %51
%53 = and i32 %52, %50
store i32 %53, i32* %51
%54 = load i32, i32* @ONLCR
%55 = load i32, i32* @OPOST
%56 = or i32 %54, %55
%57 = xor i32 %56, -1
%58 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%59 = load i32, i32* %58
%60 = and i32 %59, %57
store i32 %60, i32* %58
%61 = load i32, i32* @ECHO
%62 = load i32, i32* @ECHONL
%63 = or i32 %61, %62
%64 = load i32, i32* @ICANON
%65 = or i32 %63, %64
%66 = load i32, i32* @ISIG
%67 = or i32 %65, %66
%68 = load i32, i32* @IEXTEN
%69 = or i32 %67, %68
%70 = xor i32 %69, -1
%71 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%72 = load i32, i32* %71
%73 = and i32 %72, %70
store i32 %73, i32* %71
%74 = load i32, i32* @CSIZE
%75 = load i32, i32* @PARENB
%76 = or i32 %74, %75
%77 = xor i32 %76, -1
%78 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
%79 = load i32, i32* %78
%80 = and i32 %79, %77
store i32 %80, i32* %78
%81 = load i32, i32* @CS8
%82 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
%83 = load i32, i32* %82
%84 = or i32 %83, %81
store i32 %84, i32* %82
%85 = load i32, i32* %6
%86 = load i32, i32* @TCSANOW
%87 = call i64 @tcsetattr(i32 %85, i32 %86, %struct.termios* %7)
%88 = icmp slt i64 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %34
%90 = call i32 @logperror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %93
91: ; preds = %34
%92 = load i32, i32* %6
store i32 %92, i32* %3
br label %93
93: ; preds = %91, %89, %32, %26, %20, %14
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [19 x i8] c"open serial device\00"
@.str.1 = [10 x i8] c"tcgetattr\00"
@.str.2 = [13 x i8] c"cfsetispeed \00"
@.str.3 = [13 x i8] c"cfsetospeed \00"
@IGNBRK = global i32 0
@BRKINT = global i32 0
@PARMRK = global i32 0
@ISTRIP = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@ONLCR = global i32 0
@OPOST = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@CSIZE = global i32 0
@PARENB = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
@.str.4 = [10 x i8] c"tcsetattr\00"
define i32 @open_serial(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @logperror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %81
12: ; preds = %2
%13 = call i64 @tcgetattr(i32 %8, %struct.termios* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @logperror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %81
17: ; preds = %12
%18 = call i64 @cfsetispeed(%struct.termios* %3, i32 %1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @logperror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %81
22: ; preds = %17
%23 = call i64 @cfsetospeed(%struct.termios* %3, i32 %1)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @logperror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %81
27: ; preds = %22
%28 = load i32, i32* @IGNBRK
%29 = load i32, i32* @BRKINT
%30 = or i32 %29, %28
%31 = load i32, i32* @PARMRK
%32 = or i32 %30, %31
%33 = load i32, i32* @ISTRIP
%34 = or i32 %32, %33
%35 = load i32, i32* @INLCR
%36 = or i32 %34, %35
%37 = load i32, i32* @IGNCR
%38 = or i32 %36, %37
%39 = load i32, i32* @ICRNL
%40 = or i32 %38, %39
%41 = load i32, i32* @IXON
%42 = or i32 %40, %41
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* @ONLCR
%48 = load i32, i32* @OPOST
%49 = or i32 %48, %47
%50 = xor i32 %49, -1
%51 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%52 = load i32, i32* %51
%53 = and i32 %52, %50
store i32 %53, i32* %51
%54 = load i32, i32* @ECHO
%55 = load i32, i32* @ECHONL
%56 = or i32 %55, %54
%57 = load i32, i32* @ICANON
%58 = or i32 %56, %57
%59 = load i32, i32* @ISIG
%60 = or i32 %58, %59
%61 = load i32, i32* @IEXTEN
%62 = or i32 %60, %61
%63 = xor i32 %62, -1
%64 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%65 = load i32, i32* %64
%66 = and i32 %65, %63
store i32 %66, i32* %64
%67 = load i32, i32* @CSIZE
%68 = load i32, i32* @PARENB
%69 = or i32 %68, %67
%70 = xor i32 %69, -1
%71 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%72 = load i32, i32* %71
%73 = and i32 %72, %70
%74 = load i32, i32* @CS8
%75 = or i32 %74, %73
store i32 %75, i32* %71
%76 = load i32, i32* @TCSANOW
%77 = call i64 @tcsetattr(i32 %8, i32 %76, %struct.termios* %3)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %27
%80 = call i32 @logperror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %81
81: ; preds = %27, %79, %25, %20, %15, %10
%82 = phi i32 [ -1, %10 ], [ -1, %15 ], [ -1, %20 ], [ -1, %25 ], [ -1, %79 ], [ %8, %27 ]
ret i32 %82
}