#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 termios2 {
  int c_iflag;
  int c_lflag;
  int c_cflag;
  int c_ispeed;
  int c_ospeed;
  int /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int B0;
int BRKINT;
int CBAUD;
int CBAUDEX;
int CS8;
int CSIZE;
int ECHO;
int ECHONL;
int ICANON;
int ICRNL;
int IEXTEN;
int IGNBRK;
int IGNCR;
int IGNPAR;
int INLCR;
int INPCK;
int ISIG;
int ISTRIP;
int IXON;
int /*<<< orphan*/ OPOST;
int /*<<< orphan*/ O_WRONLY;
int PARENB;
int PARMRK;
int /*<<< orphan*/ TCGETS2;
int /*<<< orphan*/ TCSETS2;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct termios2 *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int open_serial(const char *device) {
  struct termios2 settings;
  int fd = open(device, O_WRONLY);
  if (fd < 0) {
    fprintf(stderr, "open failed: %s", strerror(errno));
    return -1;
  }
  if (ioctl(fd, TCGETS2, &settings) < 0) {
    fprintf(stderr, "ioctl TCGETS2 failed: %s", strerror(errno));
    close(fd);
    return -1;
  }
  settings.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | INPCK | ISTRIP | INLCR |
                        IGNCR | ICRNL | IXON);
  settings.c_iflag |= PARMRK;
  settings.c_oflag &= ~OPOST;
  settings.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  settings.c_cflag &= ~(CSIZE | PARENB | CBAUD);
  settings.c_cflag |= CS8 | (CBAUDEX | B0);
  settings.c_ispeed = 250000;
  settings.c_ospeed = 250000;
  if (ioctl(fd, TCSETS2, &settings) < 0) {
    fprintf(stderr, "ioctl TCSETS2 failed: %s", strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11b3 <open_serial+0x63>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xe5e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <open_serial+0x22d>
mov    -0x2c(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1212 <open_serial+0xc2>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe17(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <open_serial+0x22d>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    0x2d6e(%rip),%rcx
or     (%rcx),%eax
mov    0x2cfd(%rip),%rcx
or     (%rcx),%eax
mov    0x2d24(%rip),%rcx
or     (%rcx),%eax
mov    0x2d03(%rip),%rcx
or     (%rcx),%eax
mov    0x2d92(%rip),%rcx
or     (%rcx),%eax
mov    0x2d81(%rip),%rcx
or     (%rcx),%eax
mov    0x2d88(%rip),%rcx
or     (%rcx),%eax
mov    0x2d57(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
mov    0x2c9b(%rip),%rcx
or     (%rcx),%eax
mov    0x2c7a(%rip),%rcx
or     (%rcx),%eax
mov    0x2ce9(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc0(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cb6(%rip),%rax
mov    (%rax),%eax
mov    0x2cdd(%rip),%rcx
or     (%rcx),%eax
mov    0x2c34(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2c9a(%rip),%rax
mov    (%rax),%eax
mov    0x2c59(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2c68(%rip),%rdx
or     (%rdx),%ecx
or     %ecx,%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x3d090,-0x1c(%rbp)
movl   $0x3d090,-0x18(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2c9e(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1377 <open_serial+0x227>
mov    0x2c22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2bfe(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xccb(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137d <open_serial+0x22d>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     12b5 <open_serial+0x165>
mov    %eax,%ebx
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     12df <open_serial+0x18f>
mov    0x2d9f(%rip),%rcx
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2d85(%rip),%rcx
or     (%rcx),%eax
mov    0x2dac(%rip),%rcx
or     (%rcx),%eax
mov    0x2d8b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e1a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e09(%rip),%rcx
or     (%rcx),%eax
mov    0x2e10(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    (%rsp),%eax
mov    0x2e09(%rip),%rcx
or     (%rcx),%eax
mov    %eax,(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x14(%rsp)
mov    0x2d9e(%rip),%rax
mov    0x2d2f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d0c(%rip),%rax
or     (%rax),%ecx
mov    0x2d7b(%rip),%rax
or     (%rax),%ecx
mov    0x2d52(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2d4b(%rip),%rax
mov    0x2d74(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2cc9(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    0x2d32(%rip),%rax
mov    0x2cf3(%rip),%rdx
or     (%rax),%ecx
or     (%rdx),%ecx
mov    0x2d00(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
movabs $0x3d0900003d090,%rax
mov    %rax,0xc(%rsp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jns    1313 <open_serial+0x1c3>
mov    0x2cc4(%rip),%rax
mov    (%rax),%ebp
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd76(%rip),%rsi
jmp    12fd <open_serial+0x1ad>
mov    0x2ca4(%rip),%rax
mov    (%rax),%ebx
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd2d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
jmp    130e <open_serial+0x1be>
mov    0x2c7a(%rip),%rax
mov    (%rax),%ebp
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd13(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios2 = type { i32, i32, i32, i32, i32, i32 }
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"open failed: %s\00"
@errno = global i32 0
@TCGETS2 = global i32 0
@.str.1 = [25 x i8] c"ioctl TCGETS2 failed: %s\00"
@IGNBRK = global i32 0
@BRKINT = global i32 0
@IGNPAR = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@PARMRK = 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
@CBAUD = global i32 0
@CS8 = global i32 0
@CBAUDEX = global i32 0
@B0 = global i32 0
@TCSETS2 = global i32 0
@.str.2 = [25 x i8] c"ioctl TCSETS2 failed: %s\00"
define i32 @open_serial(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.termios2
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 -1, i32* %2
br label %104
16: ; preds = %1
%17 = load i32, i32* %5
%18 = load i32, i32* @TCGETS2
%19 = call i64 @ioctl(i32 %17, i32 %18, %struct.termios2* %4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %2
br label %104
28: ; preds = %16
%29 = load i32, i32* @IGNBRK
%30 = load i32, i32* @BRKINT
%31 = or i32 %29, %30
%32 = load i32, i32* @IGNPAR
%33 = or i32 %31, %32
%34 = load i32, i32* @INPCK
%35 = or i32 %33, %34
%36 = load i32, i32* @ISTRIP
%37 = or i32 %35, %36
%38 = load i32, i32* @INLCR
%39 = or i32 %37, %38
%40 = load i32, i32* @IGNCR
%41 = or i32 %39, %40
%42 = load i32, i32* @ICRNL
%43 = or i32 %41, %42
%44 = load i32, i32* @IXON
%45 = or i32 %43, %44
%46 = xor i32 %45, -1
%47 = getelementptr inbounds %struct.termios2, %struct.termios2* %4, i32 0, i32 0
%48 = load i32, i32* %47
%49 = and i32 %48, %46
store i32 %49, i32* %47
%50 = load i32, i32* @PARMRK
%51 = getelementptr inbounds %struct.termios2, %struct.termios2* %4, i32 0, i32 0
%52 = load i32, i32* %51
%53 = or i32 %52, %50
store i32 %53, i32* %51
%54 = load i32, i32* @OPOST
%55 = xor i32 %54, -1
%56 = getelementptr inbounds %struct.termios2, %struct.termios2* %4, i32 0, i32 5
%57 = load i32, i32* %56
%58 = and i32 %57, %55
store i32 %58, i32* %56
%59 = load i32, i32* @ECHO
%60 = load i32, i32* @ECHONL
%61 = or i32 %59, %60
%62 = load i32, i32* @ICANON
%63 = or i32 %61, %62
%64 = load i32, i32* @ISIG
%65 = or i32 %63, %64
%66 = load i32, i32* @IEXTEN
%67 = or i32 %65, %66
%68 = xor i32 %67, -1
%69 = getelementptr inbounds %struct.termios2, %struct.termios2* %4, i32 0, i32 1
%70 = load i32, i32* %69
%71 = and i32 %70, %68
store i32 %71, i32* %69
%72 = load i32, i32* @CSIZE
%73 = load i32, i32* @PARENB
%74 = or i32 %72, %73
%75 = load i32, i32* @CBAUD
%76 = or i32 %74, %75
%77 = xor i32 %76, -1
%78 = getelementptr inbounds %struct.termios2, %struct.termios2* %4, i32 0, i32 2
%79 = load i32, i32* %78
%80 = and i32 %79, %77
store i32 %80, i32* %78
%81 = load i32, i32* @CS8
%82 = load i32, i32* @CBAUDEX
%83 = load i32, i32* @B0
%84 = or i32 %82, %83
%85 = or i32 %81, %84
%86 = getelementptr inbounds %struct.termios2, %struct.termios2* %4, i32 0, i32 2
%87 = load i32, i32* %86
%88 = or i32 %87, %85
store i32 %88, i32* %86
%89 = getelementptr inbounds %struct.termios2, %struct.termios2* %4, i32 0, i32 3
store i32 250000, i32* %89
%90 = getelementptr inbounds %struct.termios2, %struct.termios2* %4, i32 0, i32 4
store i32 250000, i32* %90
%91 = load i32, i32* %5
%92 = load i32, i32* @TCSETS2
%93 = call i64 @ioctl(i32 %91, i32 %92, %struct.termios2* %4)
%94 = icmp slt i64 %93, 0
br i1 %94, label %95, label %102
95: ; preds = %28
%96 = load i32, i32* @stderr
%97 = load i32, i32* @errno
%98 = call i8* @strerror(i32 %97)
%99 = call i32 @fprintf(i32 %96, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %98)
%100 = load i32, i32* %5
%101 = call i32 @close(i32 %100)
store i32 -1, i32* %2
br label %104
102: ; preds = %28
%103 = load i32, i32* %5
store i32 %103, i32* %2
br label %104
104: ; preds = %102, %95, %21, %11
%105 = load i32, i32* %2
ret i32 %105
}

LLVM IR (-O2):
%struct.termios2 = type { i32, i32, i32, i32, i32, i32 }
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"open failed: %s\00"
@errno = global i32 0
@TCGETS2 = global i32 0
@.str.1 = [25 x i8] c"ioctl TCGETS2 failed: %s\00"
@IGNBRK = global i32 0
@BRKINT = global i32 0
@IGNPAR = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@PARMRK = 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
@CBAUD = global i32 0
@CS8 = global i32 0
@CBAUDEX = global i32 0
@B0 = global i32 0
@TCSETS2 = global i32 0
@.str.2 = [25 x i8] c"ioctl TCSETS2 failed: %s\00"
define i32 @open_serial(i8* nocapture %0) {
%2 = alloca %struct.termios2
%3 = bitcast %struct.termios2* %2 to i8*
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %90
12: ; preds = %1
%13 = load i32, i32* @TCGETS2
%14 = call i64 @ioctl(i32 %5, i32 %13, %struct.termios2* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @close(i32 %5)
br label %90
22: ; preds = %12
%23 = load i32, i32* @IGNBRK
%24 = load i32, i32* @BRKINT
%25 = or i32 %24, %23
%26 = load i32, i32* @IGNPAR
%27 = or i32 %25, %26
%28 = load i32, i32* @INPCK
%29 = or i32 %27, %28
%30 = load i32, i32* @ISTRIP
%31 = or i32 %29, %30
%32 = load i32, i32* @INLCR
%33 = or i32 %31, %32
%34 = load i32, i32* @IGNCR
%35 = or i32 %33, %34
%36 = load i32, i32* @ICRNL
%37 = or i32 %35, %36
%38 = load i32, i32* @IXON
%39 = or i32 %37, %38
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.termios2, %struct.termios2* %2, i64 0, i32 0
%42 = load i32, i32* %41
%43 = and i32 %42, %40
%44 = load i32, i32* @PARMRK
%45 = or i32 %44, %43
store i32 %45, i32* %41
%46 = load i32, i32* @OPOST
%47 = xor i32 %46, -1
%48 = getelementptr inbounds %struct.termios2, %struct.termios2* %2, i64 0, i32 5
%49 = load i32, i32* %48
%50 = and i32 %49, %47
store i32 %50, i32* %48
%51 = load i32, i32* @ECHO
%52 = load i32, i32* @ECHONL
%53 = or i32 %52, %51
%54 = load i32, i32* @ICANON
%55 = or i32 %53, %54
%56 = load i32, i32* @ISIG
%57 = or i32 %55, %56
%58 = load i32, i32* @IEXTEN
%59 = or i32 %57, %58
%60 = xor i32 %59, -1
%61 = getelementptr inbounds %struct.termios2, %struct.termios2* %2, i64 0, i32 1
%62 = load i32, i32* %61
%63 = and i32 %62, %60
store i32 %63, i32* %61
%64 = load i32, i32* @CSIZE
%65 = load i32, i32* @PARENB
%66 = or i32 %65, %64
%67 = load i32, i32* @CBAUD
%68 = or i32 %66, %67
%69 = xor i32 %68, -1
%70 = getelementptr inbounds %struct.termios2, %struct.termios2* %2, i64 0, i32 2
%71 = load i32, i32* %70
%72 = and i32 %71, %69
%73 = load i32, i32* @CS8
%74 = load i32, i32* @CBAUDEX
%75 = load i32, i32* @B0
%76 = or i32 %73, %72
%77 = or i32 %76, %74
%78 = or i32 %77, %75
store i32 %78, i32* %70
%79 = getelementptr inbounds %struct.termios2, %struct.termios2* %2, i64 0, i32 3
store i32 250000, i32* %79
%80 = getelementptr inbounds %struct.termios2, %struct.termios2* %2, i64 0, i32 4
store i32 250000, i32* %80
%81 = load i32, i32* @TCSETS2
%82 = call i64 @ioctl(i32 %5, i32 %81, %struct.termios2* %2)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %90
84: ; preds = %22
%85 = load i32, i32* @stderr
%86 = load i32, i32* @errno
%87 = call i8* @strerror(i32 %86)
%88 = call i32 @fprintf(i32 %85, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %87)
%89 = call i32 @close(i32 %5)
br label %90
90: ; preds = %22, %84, %16, %7
%91 = phi i32 [ -1, %7 ], [ -1, %16 ], [ -1, %84 ], [ %5, %22 ]
ret i32 %91
}