#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_cflag;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
};

/* Variables and functions */
int /*<<< orphan*/ B9600;
int CLOCAL;
int CREAD;
int CS8;
int IGNBRK;
int IGNPAR;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCMBIS;
int /*<<< orphan*/ TIOCMGET;
int /*<<< orphan*/ TIOCMSET;
int TIOCM_DTR;
int TIOCM_RTS;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int openuart(char *fn) {
  int fd = open(fn, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd < 0)
    return -1;
  struct termios cnew;
  if (tcgetattr(fd, &cnew) < 0) {
    close(fd);
    return -1;
  };
  cnew.c_iflag = IGNBRK | IGNPAR;
  cnew.c_oflag = 0;
  cnew.c_cflag = CS8 | CREAD | CLOCAL;
  cnew.c_lflag = 0;
  cfsetospeed(&cnew, B9600);
  cfsetispeed(&cnew, B9600);
  if (tcsetattr(fd, TCSANOW, &cnew) < 0) {
    close(fd);
    return -1;
  };
  int cntrlbits = TIOCM_DTR;
  if (ioctl(fd, TIOCMBIS, &cntrlbits) < 0) {
    fprintf(stderr, "TIOCM_DTR failed\n");
  }
  if (ioctl(fd, TIOCMGET, &cntrlbits) == 0) {
    cntrlbits |= TIOCM_RTS;
    ioctl(fd, TIOCMSET, &cntrlbits);
  } else {
    fprintf(stderr, "TIOCM_RTS failed\n");
  }
  tcflush(fd, TCIOFLUSH);
  return fd;
}


x86 assembly (clang, -O0):
<openuart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2dd8(%rip),%rax
or     (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d9 <openuart+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <openuart+0x1c1>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1203 <openuart+0x73>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <openuart+0x1c1>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    0x2d55(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
mov    0x2d80(%rip),%rcx
or     (%rcx),%eax
mov    0x2d1f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   10b0 <cfsetispeed@plt>
mov    -0x14(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
jge    129d <openuart+0x10d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <openuart+0x1c1>
mov    0x2d1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cdd(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    12dd <openuart+0x14d>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
lea    0xd28(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
mov    0x2cb9(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jne    1325 <openuart+0x195>
mov    0x2cc5(%rip),%rax
mov    (%rax),%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cc3(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdx
call   1050 <ioctl@plt>
jmp    133a <openuart+0x1aa>
mov    0x2c54(%rip),%rax
mov    (%rax),%edi
lea    0xcdd(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
mov    0x2c34(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openuart>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e48(%rip),%rax
mov    0x2de1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     12c3 <openuart+0x133>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
test   %rax,%rax
js     12bc <openuart+0x12c>
mov    0x2d91(%rip),%rax
mov    0x2d82(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2db3(%rip),%rax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d51(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0xc(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2d9c(%rip),%r15
mov    (%r15),%esi
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   10b0 <cfsetispeed@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1090 <tcsetattr@plt>
test   %rax,%rax
js     12bc <openuart+0x12c>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
jns    128b <openuart+0xfb>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
je     12ca <openuart+0x13a>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
lea    0xd5d(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12ec <openuart+0x15c>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    12fc <openuart+0x16c>
mov    0x2cf7(%rip),%rax
mov    (%rax),%eax
or     %eax,0x4(%rsp)
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@B9600 = global i32 0
@TCSANOW = global i32 0
@TIOCM_DTR = global i32 0
@TIOCMBIS = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"TIOCM_DTR failed\0A\00"
@TIOCMGET = global i32 0
@TIOCM_RTS = global i32 0
@TIOCMSET = global i32 0
@.str.1 = [18 x i8] c"TIOCM_RTS failed\0A\00"
@TCIOFLUSH = global i32 0
define i32 @openuart(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %7, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %77
17: ; preds = %1
%18 = load i32, i32* %4
%19 = call i64 @tcgetattr(i32 %18, %struct.termios* %5)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %2
br label %77
24: ; preds = %17
%25 = load i32, i32* @IGNBRK
%26 = load i32, i32* @IGNPAR
%27 = or i32 %25, %26
%28 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 3
store i64 0, i64* %29
%30 = load i32, i32* @CS8
%31 = load i32, i32* @CREAD
%32 = or i32 %30, %31
%33 = load i32, i32* @CLOCAL
%34 = or i32 %32, %33
%35 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
store i64 0, i64* %36
%37 = load i32, i32* @B9600
%38 = call i32 @cfsetospeed(%struct.termios* %5, i32 %37)
%39 = load i32, i32* @B9600
%40 = call i32 @cfsetispeed(%struct.termios* %5, i32 %39)
%41 = load i32, i32* %4
%42 = load i32, i32* @TCSANOW
%43 = call i64 @tcsetattr(i32 %41, i32 %42, %struct.termios* %5)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %24
%46 = load i32, i32* %4
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %2
br label %77
48: ; preds = %24
%49 = load i32, i32* @TIOCM_DTR
store i32 %49, i32* %6
%50 = load i32, i32* %4
%51 = load i32, i32* @TIOCMBIS
%52 = call i64 @ioctl(i32 %50, i32 %51, i32* %6)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %57
57: ; preds = %54, %48
%58 = load i32, i32* %4
%59 = load i32, i32* @TIOCMGET
%60 = call i64 @ioctl(i32 %58, i32 %59, i32* %6)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %69
62: ; preds = %57
%63 = load i32, i32* @TIOCM_RTS
%64 = load i32, i32* %6
%65 = or i32 %64, %63
store i32 %65, i32* %6
%66 = load i32, i32* %4
%67 = load i32, i32* @TIOCMSET
%68 = call i64 @ioctl(i32 %66, i32 %67, i32* %6)
br label %72
69: ; preds = %57
%70 = load i32, i32* @stderr
%71 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %72
72: ; preds = %69, %62
%73 = load i32, i32* %4
%74 = load i32, i32* @TCIOFLUSH
%75 = call i32 @tcflush(i32 %73, i32 %74)
%76 = load i32, i32* %4
store i32 %76, i32* %2
br label %77
77: ; preds = %72, %45, %21, %16
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@B9600 = global i32 0
@TCSANOW = global i32 0
@TIOCM_DTR = global i32 0
@TIOCMBIS = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"TIOCM_DTR failed\0A\00"
@TIOCMGET = global i32 0
@TIOCM_RTS = global i32 0
@TIOCMSET = global i32 0
@.str.1 = [18 x i8] c"TIOCM_RTS failed\0A\00"
@TCIOFLUSH = global i32 0
define i32 @openuart(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = alloca i32
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %66, label %11
11: ; preds = %1
%12 = bitcast %struct.termios* %2 to i8*
%13 = call i64 @tcgetattr(i32 %9, %struct.termios* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @close(i32 %9)
br label %64
17: ; preds = %11
%18 = load i32, i32* @IGNBRK
%19 = load i32, i32* @IGNPAR
%20 = or i32 %19, %18
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @CS8
%23 = load i32, i32* @CREAD
%24 = or i32 %23, %22
%25 = load i32, i32* @CLOCAL
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%29 = bitcast i64* %28 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %29
%30 = load i32, i32* @B9600
%31 = call i32 @cfsetospeed(%struct.termios* %2, i32 %30)
%32 = load i32, i32* @B9600
%33 = call i32 @cfsetispeed(%struct.termios* %2, i32 %32)
%34 = load i32, i32* @TCSANOW
%35 = call i64 @tcsetattr(i32 %9, i32 %34, %struct.termios* %2)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %17
%38 = call i32 @close(i32 %9)
br label %64
39: ; preds = %17
%40 = bitcast i32* %3 to i8*
%41 = load i32, i32* @TIOCM_DTR
store i32 %41, i32* %3
%42 = load i32, i32* @TIOCMBIS
%43 = call i64 @ioctl(i32 %9, i32 %42, i32* %3)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %48
48: ; preds = %45, %39
%49 = load i32, i32* @TIOCMGET
%50 = call i64 @ioctl(i32 %9, i32 %49, i32* %3)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %48
%53 = load i32, i32* @TIOCM_RTS
%54 = load i32, i32* %3
%55 = or i32 %54, %53
store i32 %55, i32* %3
%56 = load i32, i32* @TIOCMSET
%57 = call i64 @ioctl(i32 %9, i32 %56, i32* %3)
br label %61
58: ; preds = %48
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %61
61: ; preds = %58, %52
%62 = load i32, i32* @TCIOFLUSH
%63 = call i32 @tcflush(i32 %9, i32 %62)
br label %64
64: ; preds = %61, %37, %15
%65 = phi i32 [ -1, %15 ], [ -1, %37 ], [ %9, %61 ]
br label %66
66: ; preds = %1, %64
%67 = phi i32 [ %65, %64 ], [ -1, %1 ]
ret i32 %67
}