#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;
  char *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int /*<<< orphan*/ B9600;
int CLOCAL;
int CREAD;
int CS7;
int HUPCL;
int /*<<< orphan*/ IGNBRK;
int NCCS;
int O_NDELAY;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
size_t errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int open(char const *, int);
int /*<<< orphan*/ stderr;
char **sys_errlist;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int setupSerial(const char *dev) {
  int i, fd;
  struct termios tios;
  fd = open(dev, O_RDWR | O_NDELAY);
  if (fd < 0) {
    fprintf(stderr, "%s: %s\n", dev, sys_errlist[errno]);
    exit(1);
  }
  if (tcgetattr(fd, &tios) < 0) {
    fprintf(stderr, "Could not get terminal attributes: %s",
            sys_errlist[errno]);
    exit(1);
  }
  tios.c_cflag = CS7 | CREAD | HUPCL | CLOCAL | PARENB;
  tios.c_iflag = IGNBRK;
  tios.c_oflag = 0;
  tios.c_lflag = 0;
  for (i = 0; i < NCCS; i++) {
    tios.c_cc[i] = '\0';
  }
  tios.c_cc[VMIN] = 1;
  tios.c_cc[VTIME] = 0;
  cfsetospeed(&tios, B9600);
  cfsetispeed(&tios, B9600);
  if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
    fprintf(stderr, "Could not set attributes: ,%s", sys_errlist[errno]);
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<setupSerial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e1 <setupSerial+0x71>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1230 <setupSerial+0xc0>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%rax
mov    0x2d87(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
mov    0x2d78(%rip),%rcx
or     (%rcx),%eax
mov    0x2d2f(%rip),%rcx
or     (%rcx),%eax
mov    0x2d0e(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%eax
jge    12af <setupSerial+0x13f>
mov    -0x30(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1283 <setupSerial+0x113>
mov    -0x30(%rbp),%rax
mov    0x2cf6(%rip),%rcx
mov    (%rcx),%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    0x2cb4(%rip),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x10(%rbp),%edi
mov    0x2ccf(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    134f <setupSerial+0x1df>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
mov    0x2cd2(%rip),%rax
mov    (%rax),%rax
mov    0x2c68(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xcf0(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setupSerial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2e54(%rip),%rax
mov    0x2e05(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1292 <setupSerial+0x122>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
js     12ce <setupSerial+0x15e>
mov    0x2daf(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dad(%rip),%rax
or     (%rax),%ecx
mov    0x2d8c(%rip),%rax
or     (%rax),%ecx
mov    0x2deb(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2dea(%rip),%rax
cmpl   $0x0,(%rax)
jle    1225 <setupSerial+0xb5>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%rdx
movb   $0x0,(%rdx,%rcx,1)
add    $0x1,%rcx
movslq (%rax),%rdx
cmp    %rdx,%rcx
jl     1210 <setupSerial+0xa0>
mov    0x8(%rsp),%rax
mov    0x2d7f(%rip),%rcx
mov    (%rcx),%rcx
movb   $0x1,(%rax,%rcx,1)
mov    0x8(%rsp),%rax
mov    0x2d3c(%rip),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d6e(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <cfsetospeed@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1080 <cfsetispeed@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
js     12f8 <setupSerial+0x188>
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%rax
mov    0x2cec(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xd46(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%rax
mov    0x2cb0(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd12(%rip),%rsi
jmp    1320 <setupSerial+0x1b0>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
mov    0x2cf0(%rip),%rax
mov    (%rax),%rax
mov    0x2c86(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd0e(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i8*, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@sys_errlist = global i8** null
@errno = global i64 0
@.str.1 = [38 x i8] c"Could not get terminal attributes: %s\00"
@CS7 = global i32 0
@CREAD = global i32 0
@HUPCL = global i32 0
@CLOCAL = global i32 0
@PARENB = global i32 0
@IGNBRK = global i32 0
@NCCS = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B9600 = global i32 0
@TCSAFLUSH = global i32 0
@.str.2 = [30 x i8] c"Could not set attributes: ,%s\00"
define i32 @setupSerial(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NDELAY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = load i8**, i8*** @sys_errlist
%17 = load i64, i64* @errno
%18 = getelementptr inbounds i8*, i8** %16, i64 %17
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %1
%23 = load i32, i32* %4
%24 = call i64 @tcgetattr(i32 %23, %struct.termios* %5)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i8**, i8*** @sys_errlist
%29 = load i64, i64* @errno
%30 = getelementptr inbounds i8*, i8** %28, i64 %29
%31 = load i8*, i8** %30
%32 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %22
%35 = load i32, i32* @CS7
%36 = load i32, i32* @CREAD
%37 = or i32 %35, %36
%38 = load i32, i32* @HUPCL
%39 = or i32 %37, %38
%40 = load i32, i32* @CLOCAL
%41 = or i32 %39, %40
%42 = load i32, i32* @PARENB
%43 = or i32 %41, %42
%44 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* @IGNBRK
%46 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 4
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 3
store i64 0, i64* %47
%48 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
store i64 0, i64* %48
store i32 0, i32* %3
br label %49
49: ; preds = %59, %34
%50 = load i32, i32* %3
%51 = load i32, i32* @NCCS
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %62
53: ; preds = %49
%54 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%55 = load i8*, i8** %54
%56 = load i32, i32* %3
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
br label %59
59: ; preds = %53
%60 = load i32, i32* %3
%61 = add nsw i32 %60, 1
store i32 %61, i32* %3
br label %49
62: ; preds = %49
%63 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%64 = load i8*, i8** %63
%65 = load i64, i64* @VMIN
%66 = getelementptr inbounds i8, i8* %64, i64 %65
store i8 1, i8* %66
%67 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%68 = load i8*, i8** %67
%69 = load i64, i64* @VTIME
%70 = getelementptr inbounds i8, i8* %68, i64 %69
store i8 0, i8* %70
%71 = load i32, i32* @B9600
%72 = call i32 @cfsetospeed(%struct.termios* %5, i32 %71)
%73 = load i32, i32* @B9600
%74 = call i32 @cfsetispeed(%struct.termios* %5, i32 %73)
%75 = load i32, i32* %4
%76 = load i32, i32* @TCSAFLUSH
%77 = call i64 @tcsetattr(i32 %75, i32 %76, %struct.termios* %5)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %87
79: ; preds = %62
%80 = load i32, i32* @stderr
%81 = load i8**, i8*** @sys_errlist
%82 = load i64, i64* @errno
%83 = getelementptr inbounds i8*, i8** %81, i64 %82
%84 = load i8*, i8** %83
%85 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %84)
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %62
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
%struct.termios = type { i32, i8*, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@sys_errlist = global i8** null
@errno = global i64 0
@.str.1 = [38 x i8] c"Could not get terminal attributes: %s\00"
@CS7 = global i32 0
@CREAD = global i32 0
@HUPCL = global i32 0
@CLOCAL = global i32 0
@PARENB = global i32 0
@IGNBRK = global i32 0
@NCCS = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B9600 = global i32 0
@TCSAFLUSH = global i32 0
@.str.2 = [30 x i8] c"Could not set attributes: ,%s\00"
define i32 @setupSerial(i8* %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NDELAY
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** @sys_errlist
%12 = load i64, i64* @errno
%13 = getelementptr inbounds i8*, i8** %11, i64 %12
%14 = load i8*, i8** %13
%15 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = call i64 @tcgetattr(i32 %7, %struct.termios* %2)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** @sys_errlist
%23 = load i64, i64* @errno
%24 = getelementptr inbounds i8*, i8** %22, i64 %23
%25 = load i8*, i8** %24
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = load i32, i32* @CS7
%30 = load i32, i32* @CREAD
%31 = or i32 %30, %29
%32 = load i32, i32* @HUPCL
%33 = or i32 %31, %32
%34 = load i32, i32* @CLOCAL
%35 = or i32 %33, %34
%36 = load i32, i32* @PARENB
%37 = or i32 %35, %36
%38 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* @IGNBRK
%40 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 4
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%42 = bitcast i64* %41 to i8*
%43 = load i32, i32* @NCCS
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %28
%46 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
br label %47
47: ; preds = %45, %47
%48 = phi i64 [ 0, %45 ], [ %51, %47 ]
%49 = load i8*, i8** %46
%50 = getelementptr inbounds i8, i8* %49, i64 %48
store i8 0, i8* %50
%51 = add nuw nsw i64 %48, 1
%52 = load i32, i32* @NCCS
%53 = sext i32 %52 to i64
%54 = icmp slt i64 %51, %53
br i1 %54, label %47, label %55
55: ; preds = %47, %28
%56 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%57 = load i8*, i8** %56
%58 = load i64, i64* @VMIN
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8 1, i8* %59
%60 = load i8*, i8** %56
%61 = load i64, i64* @VTIME
%62 = getelementptr inbounds i8, i8* %60, i64 %61
store i8 0, i8* %62
%63 = load i32, i32* @B9600
%64 = call i32 @cfsetospeed(%struct.termios* %2, i32 %63)
%65 = load i32, i32* @B9600
%66 = call i32 @cfsetispeed(%struct.termios* %2, i32 %65)
%67 = load i32, i32* @TCSAFLUSH
%68 = call i64 @tcsetattr(i32 %7, i32 %67, %struct.termios* %2)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %78
70: ; preds = %55
%71 = load i32, i32* @stderr
%72 = load i8**, i8*** @sys_errlist
%73 = load i64, i64* @errno
%74 = getelementptr inbounds i8*, i8** %72, i64 %73
%75 = load i8*, i8** %74
%76 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %75)
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %55
ret i32 %7
}