#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_lflag;
  int c_cflag;
  int /*<<< orphan*/ c_oflag;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int ECHO;
int ICANON;
int ICRNL;
int IGNBRK;
int IGNCR;
int INLCR;
int INPCK;
int ISIG;
int ISTRIP;
int IXOFF;
int IXON;
int /*<<< orphan*/ OPOST;
int O_NONBLOCK;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial(char *name, speed_t baudrate) {
  int serial;
  struct termios serial_struct;
  if ((serial = open(name, O_RDWR | O_NONBLOCK)) < 0) {
    perror(name);
    return -1;
  }
  tcgetattr(serial, &serial_struct);
  serial_struct.c_iflag &=
      ~(INPCK | ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
  serial_struct.c_oflag &= ~(OPOST);
  serial_struct.c_lflag &= ~(ISIG | ICANON | ECHO);
  serial_struct.c_cflag &= ~(CSTOPB | PARENB | CSIZE);
  serial_struct.c_iflag |= IGNBRK;
  serial_struct.c_cflag |= (CS8 | CREAD);
  cfsetospeed(&serial_struct, B115200);
  cfsetispeed(&serial_struct, B115200);
  if (tcsetattr(serial, TCSANOW, &serial_struct) < 0)
    perror("tcsetattr");
  return serial;
}


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    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11ab <open_serial+0x4b>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <open_serial+0x175>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    0x2d99(%rip),%rcx
or     (%rcx),%eax
mov    0x2df0(%rip),%rcx
or     (%rcx),%eax
mov    0x2e07(%rip),%rcx
or     (%rcx),%eax
mov    0x2df6(%rip),%rcx
or     (%rcx),%eax
mov    0x2de5(%rip),%rcx
or     (%rcx),%eax
mov    0x2dbc(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    0x2d27(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6e(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cfc(%rip),%rax
mov    (%rax),%eax
mov    0x2d63(%rip),%rcx
or     (%rcx),%eax
mov    0x2d2a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cf8(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d11(%rip),%rax
mov    (%rax),%eax
mov    0x2d10(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cc9(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    -0x18(%rbp),%edi
mov    0x2cf2(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    12cf <open_serial+0x16f>
lea    0xd36(%rip),%rdi
call   1080 <perror@plt>
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   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e76(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     128e <open_serial+0x12e>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    0x2dc8(%rip),%rcx
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e0e(%rip),%rcx
or     (%rcx),%eax
mov    0x2e25(%rip),%rcx
or     (%rcx),%eax
mov    0x2e14(%rip),%rcx
or     (%rcx),%eax
mov    0x2e03(%rip),%rcx
or     (%rcx),%eax
mov    0x2dda(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    (%rsp),%eax
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %ecx,0xc(%rsp)
mov    0x2d9d(%rip),%rcx
mov    0x2d4e(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d93(%rip),%rcx
or     (%rcx),%edx
not    %edx
and    %edx,0x4(%rsp)
mov    0x2d24(%rip),%rcx
mov    0x2d8d(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d52(%rip),%rcx
or     (%rcx),%edx
not    %edx
and    0x8(%rsp),%edx
mov    0x2d23(%rip),%rcx
or     (%rcx),%eax
mov    0x2d42(%rip),%rcx
or     (%rcx),%edx
mov    0x2d41(%rip),%rcx
or     (%rcx),%edx
mov    %eax,(%rsp)
mov    %edx,0x8(%rsp)
mov    0x2cf9(%rip),%rbx
mov    (%rbx),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1070 <cfsetispeed@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
test   %rax,%rax
js     129d <open_serial+0x13d>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1283 <open_serial+0x123>
lea    0xd5c(%rip),%rdi
call   1080 <perror@plt>
jmp    1283 <open_serial+0x123>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@INLCR = global i32 0
@IXOFF = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@CSTOPB = global i32 0
@PARENB = global i32 0
@CSIZE = global i32 0
@IGNBRK = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str = [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_NONBLOCK
%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 %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @perror(i8* %15)
store i32 -1, i32* %3
br label %82
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i32 @tcgetattr(i32 %18, %struct.termios* %7)
%20 = load i32, i32* @INPCK
%21 = load i32, i32* @ISTRIP
%22 = or i32 %20, %21
%23 = load i32, i32* @IGNCR
%24 = or i32 %22, %23
%25 = load i32, i32* @ICRNL
%26 = or i32 %24, %25
%27 = load i32, i32* @INLCR
%28 = or i32 %26, %27
%29 = load i32, i32* @IXOFF
%30 = or i32 %28, %29
%31 = load i32, i32* @IXON
%32 = or i32 %30, %31
%33 = xor i32 %32, -1
%34 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%35 = load i32, i32* %34
%36 = and i32 %35, %33
store i32 %36, i32* %34
%37 = load i32, i32* @OPOST
%38 = xor i32 %37, -1
%39 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
%40 = load i32, i32* %39
%41 = and i32 %40, %38
store i32 %41, i32* %39
%42 = load i32, i32* @ISIG
%43 = load i32, i32* @ICANON
%44 = or i32 %42, %43
%45 = load i32, i32* @ECHO
%46 = or i32 %44, %45
%47 = xor i32 %46, -1
%48 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%49 = load i32, i32* %48
%50 = and i32 %49, %47
store i32 %50, i32* %48
%51 = load i32, i32* @CSTOPB
%52 = load i32, i32* @PARENB
%53 = or i32 %51, %52
%54 = load i32, i32* @CSIZE
%55 = or i32 %53, %54
%56 = xor i32 %55, -1
%57 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%58 = load i32, i32* %57
%59 = and i32 %58, %56
store i32 %59, i32* %57
%60 = load i32, i32* @IGNBRK
%61 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%62 = load i32, i32* %61
%63 = or i32 %62, %60
store i32 %63, i32* %61
%64 = load i32, i32* @CS8
%65 = load i32, i32* @CREAD
%66 = or i32 %64, %65
%67 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%68 = load i32, i32* %67
%69 = or i32 %68, %66
store i32 %69, i32* %67
%70 = load i32, i32* @B115200
%71 = call i32 @cfsetospeed(%struct.termios* %7, i32 %70)
%72 = load i32, i32* @B115200
%73 = call i32 @cfsetispeed(%struct.termios* %7, i32 %72)
%74 = load i32, i32* %6
%75 = load i32, i32* @TCSANOW
%76 = call i64 @tcsetattr(i32 %74, i32 %75, %struct.termios* %7)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %17
%79 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %80
80: ; preds = %78, %17
%81 = load i32, i32* %6
store i32 %81, i32* %3
br label %82
82: ; preds = %80, %14
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@INLCR = global i32 0
@IXOFF = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@CSTOPB = global i32 0
@PARENB = global i32 0
@CSIZE = global i32 0
@IGNBRK = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str = [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_NONBLOCK
%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 @perror(i8* %0)
br label %69
12: ; preds = %2
%13 = call i32 @tcgetattr(i32 %8, %struct.termios* %3)
%14 = load i32, i32* @INPCK
%15 = load i32, i32* @ISTRIP
%16 = or i32 %15, %14
%17 = load i32, i32* @IGNCR
%18 = or i32 %16, %17
%19 = load i32, i32* @ICRNL
%20 = or i32 %18, %19
%21 = load i32, i32* @INLCR
%22 = or i32 %20, %21
%23 = load i32, i32* @IXOFF
%24 = or i32 %22, %23
%25 = load i32, i32* @IXON
%26 = or i32 %24, %25
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%29 = load i32, i32* %28
%30 = and i32 %29, %27
%31 = load i32, i32* @OPOST
%32 = xor i32 %31, -1
%33 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%34 = load i32, i32* %33
%35 = and i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* @ISIG
%37 = load i32, i32* @ICANON
%38 = or i32 %37, %36
%39 = load i32, i32* @ECHO
%40 = or i32 %38, %39
%41 = xor i32 %40, -1
%42 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%43 = load i32, i32* %42
%44 = and i32 %43, %41
store i32 %44, i32* %42
%45 = load i32, i32* @CSTOPB
%46 = load i32, i32* @PARENB
%47 = or i32 %46, %45
%48 = load i32, i32* @CSIZE
%49 = or i32 %47, %48
%50 = xor i32 %49, -1
%51 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%52 = load i32, i32* %51
%53 = and i32 %52, %50
%54 = load i32, i32* @IGNBRK
%55 = or i32 %54, %30
store i32 %55, i32* %28
%56 = load i32, i32* @CS8
%57 = load i32, i32* @CREAD
%58 = or i32 %56, %53
%59 = or i32 %58, %57
store i32 %59, i32* %51
%60 = load i32, i32* @B115200
%61 = call i32 @cfsetospeed(%struct.termios* %3, i32 %60)
%62 = load i32, i32* @B115200
%63 = call i32 @cfsetispeed(%struct.termios* %3, i32 %62)
%64 = load i32, i32* @TCSANOW
%65 = call i64 @tcsetattr(i32 %8, i32 %64, %struct.termios* %3)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %12
%68 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %69
69: ; preds = %12, %67, %10
%70 = phi i32 [ -1, %10 ], [ %8, %67 ], [ %8, %12 ]
ret i32 %70
}