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

/* Variables and functions */
int BUFSIZ;
int CLOCAL;
int CRTSCTS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ set_RTS(int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int tcflush(int, int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, char **argv) {
  int fd;
  char serial_name[BUFSIZ];
  struct termios attr;
  struct termios oldterminfo;
  int result;
  int length = 1000000;
  if (argc > 1) {
    strncpy(serial_name, argv[1], BUFSIZ);
  } else {
    strcpy(serial_name, "/dev/ttyAMA0");
  }
  if (argc > 2) {
    length = atoi(argv[2]);
  }
  fd = open(serial_name, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Error opening %s: %s\n", serial_name, strerror(errno));
    return -1;
  }
  result = tcgetattr(fd, &oldterminfo);
  if (result < 0) {
    fprintf(stderr, "Error tcgetattr(): %s\n", strerror(errno));
    return -1;
  }
  attr = oldterminfo;
  attr.c_cflag |= CRTSCTS | CLOCAL;
  attr.c_oflag = 0;
  result = tcflush(fd, TCIOFLUSH);
  if (result < 0) {
    fprintf(stderr, "ERROR tcflush() :%s\n", strerror(errno));
    return -1;
  }
  result = tcsetattr(fd, TCSANOW, &attr);
  if (result < 0) {
    fprintf(stderr, "ERROR: tcsetattr() :%s\n", strerror(errno));
    return -1;
  }
  while (1) {
    set_RTS(fd, 0);
    sleep(1);
    set_RTS(fd, 1);
    sleep(1);
  }
  tcsetattr(fd, TCSANOW, &oldterminfo);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0xf4240,-0x50(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1222 <main+0x72>
mov    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
jmp    1232 <main+0x82>
mov    -0x60(%rbp),%rdi
lea    0xdd3(%rip),%rsi
call   1040 <strcpy@plt>
cmpl   $0x2,-0x8(%rbp)
jle    124c <main+0x9c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x50(%rbp)
mov    -0x60(%rbp),%rdi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12b0 <main+0x100>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xd77(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1436 <main+0x286>
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    130a <main+0x15a>
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xd33(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1436 <main+0x286>
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
mov    0x2c76(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2c74(%rip),%rax
mov    (%rax),%esi
call   1060 <tcflush@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1399 <main+0x1e9>
mov    0x2c71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2c55(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xcbb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1436 <main+0x286>
mov    -0x14(%rbp),%edi
mov    0x2c3d(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    13fc <main+0x24c>
mov    0x2c0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2bf2(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
lea    0xc6d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1436 <main+0x286>
jmp    1401 <main+0x251>
jmp    1406 <main+0x256>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1050 <set_RTS@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <set_RTS@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    1401 <main+0x251>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r14
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
cmp    $0x2,%edi
jl     11d8 <main+0x38>
mov    0x8(%rsi),%rsi
mov    %r14,%rdi
call   1030 <strncpy@plt>
jmp    11e7 <main+0x47>
lea    0xe21(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     129b <main+0xfb>
mov    %eax,%ebx
lea    -0x30(%rbp),%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
test   %eax,%eax
js     12ca <main+0x12a>
movups -0x30(%rbp),%xmm0
mov    0x2d88(%rip),%rax
mov    0x2d79(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
movaps %xmm0,-0x20(%rbp)
or     %ecx,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <tcflush@plt>
test   %eax,%eax
js     12ea <main+0x14a>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
mov    %ebx,%edi
call   1090 <tcsetattr@plt>
test   %eax,%eax
js     130a <main+0x16a>
xchg   %ax,%ax
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <set_RTS@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <set_RTS@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    1270 <main+0xd0>
mov    0x2d2e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd54(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1334 <main+0x194>
mov    0x2cff(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd3b(%rip),%rsi
jmp    1328 <main+0x188>
mov    0x2cdf(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd32(%rip),%rsi
jmp    1328 <main+0x188>
mov    0x2cbf(%rip),%rax
mov    (%rax),%ebx
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd27(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64 }
@BUFSIZ = global i32 0
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Error opening %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"Error tcgetattr(): %s\0A\00"
@CRTSCTS = global i32 0
@CLOCAL = global i32 0
@TCIOFLUSH = global i32 0
@.str.3 = [21 x i8] c"ERROR tcflush() :%s\0A\00"
@TCSANOW = global i32 0
@.str.4 = [24 x i8] c"ERROR: tcsetattr() :%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.termios
%10 = alloca %struct.termios
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %7
%17 = alloca i8, i64 %15
store i64 %15, i64* %8
store i32 1000000, i32* %12
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 1
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @BUFSIZ
%25 = call i32 @strncpy(i8* %17, i8* %23, i32 %24)
br label %28
26: ; preds = %2
%27 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = load i32, i32* %4
%30 = icmp sgt i32 %29, 2
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %12
br label %36
36: ; preds = %31, %28
%37 = load i32, i32* @O_RDWR
%38 = call i32 @open(i8* %17, i32 %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %44)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %96
46: ; preds = %36
%47 = load i32, i32* %6
%48 = call i32 @tcgetattr(i32 %47, %struct.termios* %10)
store i32 %48, i32* %11
%49 = load i32, i32* %11
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %46
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %54)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %96
56: ; preds = %46
%57 = bitcast %struct.termios* %9 to i8*
%58 = bitcast %struct.termios* %10 to i8*
%59 = load i32, i32* @CRTSCTS
%60 = load i32, i32* @CLOCAL
%61 = or i32 %59, %60
%62 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%63 = load i32, i32* %62
%64 = or i32 %63, %61
store i32 %64, i32* %62
%65 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 1
store i64 0, i64* %65
%66 = load i32, i32* %6
%67 = load i32, i32* @TCIOFLUSH
%68 = call i32 @tcflush(i32 %66, i32 %67)
store i32 %68, i32* %11
%69 = load i32, i32* %11
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %56
%72 = load i32, i32* @stderr
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %74)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %96
76: ; preds = %56
%77 = load i32, i32* %6
%78 = load i32, i32* @TCSANOW
%79 = call i32 @tcsetattr(i32 %77, i32 %78, %struct.termios* %9)
store i32 %79, i32* %11
%80 = load i32, i32* %11
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %87
82: ; preds = %76
%83 = load i32, i32* @stderr
%84 = load i32, i32* @errno
%85 = call i8* @strerror(i32 %84)
%86 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %85)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %96
87: ; preds = %76
br label %88
88: ; preds = %89, %87
br label %89
89: ; preds = %88
%90 = load i32, i32* %6
%91 = call i32 @set_RTS(i32 %90, i32 0)
%92 = call i32 @sleep(i32 1)
%93 = load i32, i32* %6
%94 = call i32 @set_RTS(i32 %93, i32 1)
%95 = call i32 @sleep(i32 1)
br label %88
96: ; preds = %82, %71, %51, %41
%97 = load i8*, i8** %7
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
%struct.termios = type { i32, i64 }
@BUFSIZ = global i32 0
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Error opening %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"Error tcgetattr(): %s\0A\00"
@CRTSCTS = global i32 0
@CLOCAL = global i32 0
@TCIOFLUSH = global i32 0
@.str.3 = [21 x i8] c"ERROR tcflush() :%s\0A\00"
@TCSANOW = global i32 0
@.str.4 = [24 x i8] c"ERROR: tcsetattr() :%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast %struct.termios* %3 to i8*
%9 = bitcast %struct.termios* %4 to i8*
%10 = icmp sgt i32 %0, 1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @strncpy(i8* %7, i8* %13, i32 %5)
br label %17
15: ; preds = %2
%16 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %7, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %24)
br label %64
26: ; preds = %17
%27 = call i32 @tcgetattr(i32 %19, %struct.termios* %4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %64
34: ; preds = %26
%35 = load i32, i32* @CRTSCTS
%36 = load i32, i32* @CLOCAL
%37 = or i32 %36, %35
%38 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%39 = load i32, i32* %38
%40 = or i32 %37, %39
store i32 %40, i32* %38
%41 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
store i64 0, i64* %41
%42 = load i32, i32* @TCIOFLUSH
%43 = call i32 @tcflush(i32 %19, i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %34
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %64
50: ; preds = %34
%51 = load i32, i32* @TCSANOW
%52 = call i32 @tcsetattr(i32 %19, i32 %51, %struct.termios* %3)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %57)
br label %64
59: ; preds = %50, %59
%60 = call i32 @set_RTS(i32 %19, i32 0)
%61 = call i32 @sleep(i32 1)
%62 = call i32 @set_RTS(i32 %19, i32 1)
%63 = call i32 @sleep(i32 1)
br label %59
64: ; preds = %54, %45, %29, %21
ret i32 -1
}