#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 *);
int findSerial(char *);
unsigned long get_time();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ serialBegin(int, int, int);
int serialRead(int, unsigned char *, int);
int main(int argc, char **argv) {
  int i, n, portNum;
  int baudrate = 9600;
  if (argc < 2) {
    printf("need arguments\n");
    return 1;
  }
  if (argc >= 2) {
    portNum = findSerial(argv[1]);
    if (portNum < 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("port=%s\n", argv[1]);
  printf("portNum=%d\n", portNum);
  printf("baud=%d\n", baudrate);
  unsigned char buf[4096];
  if (serialBegin(portNum, baudrate, 1)) {
    printf("Can not open comport\n");
    return (0);
  }
  while (1) {
    n = serialRead(portNum, buf, 4095);
    if (n > 0) {
      unsigned long now_time = get_time();
      while (get_time() - now_time < 5) {
        int t = serialRead(portNum, 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    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2580,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a4 <main+0x44>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1358 <main+0x1f8>
cmpl   $0x2,-0x8(%rbp)
jl     11e7 <main+0x87>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <findSerial@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e2 <main+0x82>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1358 <main+0x1f8>
jmp    11e7 <main+0x87>
cmpl   $0x3,-0x8(%rbp)
jl     122a <main+0xca>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     1225 <main+0xc5>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1358 <main+0x1f8>
jmp    122a <main+0xca>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    $0x1,%edx
call   1070 <serialBegin@plt>
cmp    $0x0,%rax
je     1296 <main+0x136>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1358 <main+0x1f8>
jmp    129b <main+0x13b>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0xfff,%edx
call   1030 <serialRead@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1353 <main+0x1f3>
mov    $0x0,%al
call   1060 <get_time@plt>
mov    %rax,-0x1028(%rbp)
mov    $0x0,%al
call   1060 <get_time@plt>
sub    -0x1028(%rbp),%rax
cmp    $0x5,%rax
jae    132f <main+0x1cf>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0xfff,%edx
call   1030 <serialRead@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0x0,-0x102c(%rbp)
jle    132a <main+0x1ca>
mov    -0x102c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <get_time@plt>
mov    %rax,-0x1028(%rbp)
jmp    12ca <main+0x16a>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x18(%rbp),%esi
lea    -0x1020(%rbp),%rdx
lea    0xd1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    129b <main+0x13b>
mov    -0x4(%rbp),%eax
add    $0x1030,%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     119a <main+0x2a>
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    12e7 <main+0x177>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   1050 <findSerial@plt>
test   %eax,%eax
js     122a <main+0xba>
mov    %eax,%r12d
mov    $0x2580,%r14d
cmp    $0x3,%ebp
jb     11ce <main+0x5e>
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    12d6 <main+0x166>
mov    0x8(%rbx),%rsi
lea    0xe27(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe20(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe1b(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,%edi
mov    %r14d,%esi
mov    $0x1,%edx
call   1080 <serialBegin@plt>
test   %rax,%rax
je     1240 <main+0xd0>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
jmp    12e7 <main+0x177>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
jmp    12e7 <main+0x177>
mov    %rsp,%r14
lea    0xdd4(%rip),%r15
jmp    1264 <main+0xf4>
nopl   0x0(%rax)
movslq %ebp,%rsi
movb   $0x0,(%rsp,%rsi,1)
mov    %r15,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0xfff,%edx
call   1040 <serialRead@plt>
test   %eax,%eax
jle    1264 <main+0xf4>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <get_time@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <get_time@plt>
sub    %rbx,%rax
cmp    $0x4,%rax
jbe    12b0 <main+0x140>
jmp    1250 <main+0xe0>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <get_time@plt>
sub    %rbx,%rax
cmp    $0x5,%rax
jae    1250 <main+0xe0>
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
mov    %r12d,%edi
mov    $0xfff,%edx
call   1040 <serialRead@plt>
test   %eax,%eax
jle    12a0 <main+0x130>
add    %eax,%ebp
xor    %eax,%eax
call   1070 <get_time@plt>
mov    %rax,%rbx
jmp    12a0 <main+0x130>
lea    0xd5c(%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 = [9 x i8] c"port=%s\0A\00"
@.str.4 = [12 x i8] c"portNum=%d\0A\00"
@.str.5 = [9 x i8] c"baud=%d\0A\00"
@.str.6 = [22 x i8] c"Can not open comport\0A\00"
@.str.7 = [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 i32
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 9600, i32* %9
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = 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 %95
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp sge i32 %18, 2
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @findSerial(i8* %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%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 %95
29: ; preds = %20
br label %30
30: ; preds = %29, %17
%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* %9
%38 = load i32, i32* %9
%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 %95
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 ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %8
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %9
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %8
%53 = load i32, i32* %9
%54 = call i64 @serialBegin(i32 %52, i32 %53, i32 1)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %43
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %95
58: ; preds = %43
br label %59
59: ; preds = %58, %94
%60 = load i32, i32* %8
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%62 = call i32 @serialRead(i32 %60, i8* %61, i32 4095)
store i32 %62, i32* %7
%63 = load i32, i32* %7
%64 = icmp sgt i32 %63, 0
br i1 %64, label %65, label %94
65: ; preds = %59
%66 = call i64 (...) @get_time()
store i64 %66, i64* %11
br label %67
67: ; preds = %86, %65
%68 = call i64 (...) @get_time()
%69 = load i64, i64* %11
%70 = sub i64 %68, %69
%71 = icmp ult i64 %70, 5
br i1 %71, label %72, label %87
72: ; preds = %67
%73 = load i32, i32* %8
%74 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%75 = load i32, i32* %7
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = call i32 @serialRead(i32 %73, i8* %77, i32 4095)
store i32 %78, i32* %12
%79 = load i32, i32* %12
%80 = icmp sgt i32 %79, 0
br i1 %80, label %81, label %86
81: ; preds = %72
%82 = load i32, i32* %12
%83 = load i32, i32* %7
%84 = add nsw i32 %83, %82
store i32 %84, i32* %7
%85 = call i64 (...) @get_time()
store i64 %85, i64* %11
br label %86
86: ; preds = %81, %72
br label %67
87: ; preds = %67
%88 = load i32, i32* %7
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 %89
store i8 0, i8* %90
%91 = load i32, i32* %7
%92 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 %91, i8* %92)
br label %94
94: ; preds = %87, %59
br label %59
95: ; preds = %56, %40, %27, %15
%96 = load i32, i32* %3
ret i32 %96
}

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