#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 */

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ putchar(char);
int serial_open(char const *, int);
char serial_read(int, int);
int /*<<< orphan*/ serial_write(int, char);
int /*<<< orphan*/ sscanf(char const *, char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int main(int argc, char const *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Missing parameter <device>\n");
    return 1;
  }
  int baud = 9600;
  if (argc == 3)
    sscanf(argv[2], "%d", &baud);
  int fd = serial_open(argv[1], baud);
  usleep(1500000);
  while (1) {
    char buf[1];
    if (fread(buf, 1, 1, stdin) > 0) {
      serial_write(fd, buf[0]);
      fflush(stdout);
    }
    char c;
    c = serial_read(fd, 10);
    if (c > 0) {
      putchar(c);
      fflush(stdout);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d0 <main+0x40>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret
movl   $0x2580,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11f9 <main+0x69>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe2c(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x14(%rbp),%esi
call   1060 <serial_open@plt>
mov    %eax,-0x18(%rbp)
mov    $0x16e360,%edi
call   10b0 <usleep@plt>
lea    -0x19(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x0,%rax
jle    1253 <main+0xc3>
mov    -0x18(%rbp),%edi
movsbl -0x19(%rbp),%esi
call   1090 <serial_write@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x18(%rbp),%edi
mov    $0xa,%esi
call   10a0 <serial_read@plt>
mov    %al,-0x1a(%rbp)
movsbl -0x1a(%rbp),%eax
cmp    $0x0,%eax
jle    1287 <main+0xf7>
movsbl -0x1a(%rbp),%edi
call   1030 <putchar@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    1216 <main+0x86>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11c3 <main+0x33>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rsi,%rbx
movl   $0x2580,0x4(%rsp)
mov    $0x2580,%esi
cmp    $0x3,%edi
jne    11f1 <main+0x61>
mov    0x10(%rbx),%rdi
lea    0xe39(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1080 <sscanf@plt>
mov    0x4(%rsp),%esi
mov    0x8(%rbx),%rdi
call   1060 <serial_open@plt>
mov    %eax,%ebx
mov    $0x16e360,%edi
call   10b0 <usleep@plt>
mov    0x2ddb(%rip),%r15
lea    0x3(%rsp),%r14
mov    0x2db7(%rip),%r12
nopl   0x0(%rax)
mov    (%r15),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
test   %rax,%rax
jle    124f <main+0xbf>
movsbl 0x3(%rsp),%esi
mov    %ebx,%edi
call   1090 <serial_write@plt>
mov    (%r12),%edi
call   1070 <fflush@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   10a0 <serial_read@plt>
test   %al,%al
jle    1220 <main+0x90>
movsbl %al,%edi
call   1030 <putchar@plt>
mov    (%r12),%edi
call   1070 <fflush@plt>
jmp    1220 <main+0x90>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Missing parameter <device>\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@stdin = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1 x i8]
%9 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
ret i32 1
15: ; preds = %2
store i32 9600, i32* %6
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 3
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
br label %23
23: ; preds = %18, %15
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* %6
%28 = call i32 @serial_open(i8* %26, i32 %27)
store i32 %28, i32* %7
%29 = call i32 @usleep(i32 1500000)
br label %30
30: ; preds = %23, %53
%31 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%32 = load i32, i32* @stdin
%33 = call i64 @fread(i8* %31, i32 1, i32 1, i32 %32)
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %30
%36 = load i32, i32* %7
%37 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%38 = load i8, i8* %37
%39 = call i32 @serial_write(i32 %36, i8 signext %38)
%40 = load i32, i32* @stdout
%41 = call i32 @fflush(i32 %40)
br label %42
42: ; preds = %35, %30
%43 = load i32, i32* %7
%44 = call signext i8 @serial_read(i32 %43, i32 10)
store i8 %44, i8* %9
%45 = load i8, i8* %9
%46 = sext i8 %45 to i32
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = load i8, i8* %9
%50 = call i32 @putchar(i8 signext %49)
%51 = load i32, i32* @stdout
%52 = call i32 @fflush(i32 %51)
br label %53
53: ; preds = %48, %42
br label %30
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Missing parameter <device>\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@stdin = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [1 x i8]
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
ret i32 1
9: ; preds = %2
%10 = bitcast i32* %3 to i8*
store i32 9600, i32* %3
%11 = icmp eq i32 %0, 3
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%16 = load i32, i32* %3
br label %17
17: ; preds = %12, %9
%18 = phi i32 [ %16, %12 ], [ 9600, %9 ]
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @serial_open(i8* %20, i32 %18)
%22 = call i32 @usleep(i32 1500000)
%23 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
br label %24
24: ; preds = %40, %17
%25 = load i32, i32* @stdin
%26 = call i64 @fread(i8* %23, i32 1, i32 1, i32 %25)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i8, i8* %23
%30 = call i32 @serial_write(i32 %21, i8 signext %29)
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
br label %33
33: ; preds = %28, %24
%34 = call signext i8 @serial_read(i32 %21, i32 10)
%35 = icmp sgt i8 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = call i32 @putchar(i8 signext %34)
%38 = load i32, i32* @stdout
%39 = call i32 @fflush(i32 %38)
br label %40
40: ; preds = %36, %33
br label %24
}