#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 atoi(char *);
unsigned long get_time();
int /*<<< orphan*/ printf(char *, ...);
int serialBegin(char *, int, int);
int serialRead(int, unsigned char *, int);
int main(int argc, char **argv) {
  int i, n;
  char *portName;
  int port;
  int baudrate = 9600;
  if (argc < 2) {
    printf("need arguments\n");
    return 1;
  }
  if (argc >= 2) {
    portName = argv[1];
    if (portName < 0) {
      printf("no this port\n");
      return 2;
    }
  }
  if (argc >= 3) {
    baudrate = atoi(argv[2]);
    if (baudrate <= 0) {
      printf("error! no this baudrate\n");
      return 3;
    }
  }
  printf("portName=%s\n", argv[1]);
  printf("baud=%d\n", baudrate);
  unsigned char buf[4096];
  port = serialBegin(portName, baudrate, 1);
  if (port < 0) {
    printf("Can not open comport\n");
    return (0);
  }
  while (1) {
    n = serialRead(port, buf, 4095);
    if (n > 0) {
      unsigned long now_time = get_time();
      while (get_time() - now_time < 5) {
        int t = serialRead(port, buf + n, 4095);
        if (t > 0) {
          n += t;
          now_time = get_time();
        }
      }
      buf[n] = 0;
      printf("%d:%s", n, (char *)buf);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2580,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1194 <main+0x44>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1338 <main+0x1e8>
cmpl   $0x2,-0x8(%rbp)
jl     11d4 <main+0x84>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11cf <main+0x7f>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1338 <main+0x1e8>
jmp    11d4 <main+0x84>
cmpl   $0x3,-0x8(%rbp)
jl     1217 <main+0xc7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     1212 <main+0xc2>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1338 <main+0x1e8>
jmp    1217 <main+0xc7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    $0x1,%edx
call   1060 <serialBegin@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1276 <main+0x126>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1338 <main+0x1e8>
jmp    127b <main+0x12b>
mov    -0x24(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0xfff,%edx
call   1030 <serialRead@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1333 <main+0x1e3>
mov    $0x0,%al
call   1050 <get_time@plt>
mov    %rax,-0x1038(%rbp)
mov    $0x0,%al
call   1050 <get_time@plt>
sub    -0x1038(%rbp),%rax
cmp    $0x5,%rax
jae    130f <main+0x1bf>
mov    -0x24(%rbp),%edi
lea    -0x1030(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0xfff,%edx
call   1030 <serialRead@plt>
mov    %eax,-0x103c(%rbp)
cmpl   $0x0,-0x103c(%rbp)
jle    130a <main+0x1ba>
mov    -0x103c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <get_time@plt>
mov    %rax,-0x1038(%rbp)
jmp    12aa <main+0x15a>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x1030(%rbp,%rax,1)
mov    -0x18(%rbp),%esi
lea    -0x1030(%rbp),%rdx
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127b <main+0x12b>
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
cmp    $0x1,%edi
jg     118a <main+0x2a>
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    12a5 <main+0x145>
mov    0x8(%rsi),%r14
mov    $0x2580,%ebp
cmp    $0x2,%edi
je     11ab <main+0x4b>
mov    0x10(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1294 <main+0x134>
lea    0xe4e(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe48(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1070 <serialBegin@plt>
test   %eax,%eax
js     1286 <main+0x126>
mov    %eax,%r12d
mov    %rsp,%r14
lea    0xe24(%rip),%r15
jmp    1214 <main+0xb4>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %ebp,%rsi
movb   $0x0,(%rsp,%rsi,1)
mov    %r15,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0xfff,%edx
call   1040 <serialRead@plt>
test   %eax,%eax
jle    1214 <main+0xb4>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <get_time@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <get_time@plt>
sub    %rbx,%rax
cmp    $0x4,%rax
jbe    1260 <main+0x100>
jmp    1200 <main+0xa0>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <get_time@plt>
sub    %rbx,%rax
cmp    $0x5,%rax
jae    1200 <main+0xa0>
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
mov    %r12d,%edi
mov    $0xfff,%edx
call   1040 <serialRead@plt>
test   %eax,%eax
jle    1250 <main+0xf0>
add    %eax,%ebp
xor    %eax,%eax
call   1060 <get_time@plt>
mov    %rax,%rbx
jmp    1250 <main+0xf0>
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
jmp    12a5 <main+0x145>
lea    0xd96(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%ebx
mov    %ebx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"need arguments\0A\00"
@.str.1 = [14 x i8] c"no this port\0A\00"
@.str.2 = [25 x i8] c"error! no this baudrate\0A\00"
@.str.3 = [13 x i8] c"portName=%s\0A\00"
@.str.4 = [9 x i8] c"baud=%d\0A\00"
@.str.5 = [22 x i8] c"Can not open comport\0A\00"
@.str.6 = [6 x i8] c"%d:%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [4096 x i8]
%12 = alloca i64
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 9600, i32* %10
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %94
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp sge i32 %19, 2
br i1 %20, label %21, label %30
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp ult i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %94
29: ; preds = %21
br label %30
30: ; preds = %29, %18
%31 = load i32, i32* %4
%32 = icmp sge i32 %31, 3
br i1 %32, label %33, label %43
33: ; preds = %30
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp sle i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %94
42: ; preds = %33
br label %43
43: ; preds = %42, %30
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %10
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i8*, i8** %8
%51 = load i32, i32* %10
%52 = call i32 @serialBegin(i8* %50, i32 %51, i32 1)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %43
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %94
57: ; preds = %43
br label %58
58: ; preds = %57, %93
%59 = load i32, i32* %9
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%61 = call i32 @serialRead(i32 %59, i8* %60, i32 4095)
store i32 %61, i32* %7
%62 = load i32, i32* %7
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %93
64: ; preds = %58
%65 = call i64 (...) @get_time()
store i64 %65, i64* %12
br label %66
66: ; preds = %85, %64
%67 = call i64 (...) @get_time()
%68 = load i64, i64* %12
%69 = sub i64 %67, %68
%70 = icmp ult i64 %69, 5
br i1 %70, label %71, label %86
71: ; preds = %66
%72 = load i32, i32* %9
%73 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%74 = load i32, i32* %7
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = call i32 @serialRead(i32 %72, i8* %76, i32 4095)
store i32 %77, i32* %13
%78 = load i32, i32* %13
%79 = icmp sgt i32 %78, 0
br i1 %79, label %80, label %85
80: ; preds = %71
%81 = load i32, i32* %13
%82 = load i32, i32* %7
%83 = add nsw i32 %82, %81
store i32 %83, i32* %7
%84 = call i64 (...) @get_time()
store i64 %84, i64* %12
br label %85
85: ; preds = %80, %71
br label %66
86: ; preds = %66
%87 = load i32, i32* %7
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 %88
store i8 0, i8* %89
%90 = load i32, i32* %7
%91 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %90, i8* %91)
br label %93
93: ; preds = %86, %58
br label %58
94: ; preds = %55, %40, %27, %16
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str.3 = [13 x i8] c"portName=%s\0A\00"
@.str.4 = [9 x i8] c"baud=%d\0A\00"
@.str.6 = [6 x i8] c"%d:%s\00"
@str = [21 x i8] c"Can not open comport\00"
@str.7 = [24 x i8] c"error! no this baudrate\00"
@str.8 = [15 x i8] c"need arguments\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
br label %57
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = icmp eq i32 %0, 2
br i1 %10, label %18, label %11
11: ; preds = %7
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
br label %57
18: ; preds = %11, %7
%19 = phi i32 [ %14, %11 ], [ 9600, %7 ]
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%23 = tail call i32 @serialBegin(i8* %9, i32 %19, i32 1)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %57
27: ; preds = %18, %56
%28 = call i32 @serialRead(i32 %23, i8* %22, i32 4095)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %56
30: ; preds = %27
%31 = call i64 (...) @get_time()
%32 = call i64 (...) @get_time()
%33 = sub i64 %32, %31
%34 = icmp ult i64 %33, 5
br i1 %34, label %35, label %51
35: ; preds = %30, %45
%36 = phi i64 [ %47, %45 ], [ %31, %30 ]
%37 = phi i32 [ %46, %45 ], [ %28, %30 ]
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %38
%40 = call i32 @serialRead(i32 %23, i8* %39, i32 4095)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = add nsw i32 %40, %37
%44 = call i64 (...) @get_time()
br label %45
45: ; preds = %42, %35
%46 = phi i32 [ %43, %42 ], [ %37, %35 ]
%47 = phi i64 [ %44, %42 ], [ %36, %35 ]
%48 = call i64 (...) @get_time()
%49 = sub i64 %48, %47
%50 = icmp ult i64 %49, 5
br i1 %50, label %35, label %51
51: ; preds = %45, %30
%52 = phi i32 [ %28, %30 ], [ %46, %45 ]
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %53
store i8 0, i8* %54
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %52, i8* %22)
br label %56
56: ; preds = %51, %27
br label %27
57: ; preds = %25, %16, %5
%58 = phi i32 [ 1, %5 ], [ 3, %16 ], [ 0, %25 ]
ret i32 %58
}