#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*/ B115200;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_interface_attribs(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ testBlockR(int, unsigned int const);
scalar_t__ testBlockW(int, unsigned int const);
int main(int argc, char **argv) {
  unsigned int test_index = 1U;
  const unsigned int test_loops = 2048U;
  const unsigned int block_size = 32U;
  unsigned int errors_count = 0U;
  char *portname = NULL;
  int fd = 0;
  if (argc != 3) {
    fprintf(stderr, "Expected port name '/dev/ttyXXX' r|w\n");
    return -1;
  }
  portname = argv[1];
  fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    fprintf(stderr, "Error opening %s: %s\n", portname, strerror(errno));
    return -1;
  }
  set_interface_attribs(fd, B115200);
  printf("Test Started:\n");
  while ((test_index++) <= test_loops) {
    if (argv[2][0] == 'w') {
      errors_count += testBlockW(fd, block_size);
    } else if (argv[2][0] == 'r') {
      errors_count += testBlockR(fd, block_size);
    } else {
      fprintf(stderr, "unknown test: %c\n", argv[2][0]);
      errors_count = 1;
      break;
    }
    printf("B%04d \n", test_index++);
  };
  printf("\n");
  if (errors_count != 0U) {
    printf("Test FAILED\nThere was %d errors in Serial communication\n Check "
           "your setup\n",
           errors_count);
  } else {
    printf("Test PASSED\n Written: %d B\n", (block_size * test_loops));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x800,-0x18(%rbp)
movl   $0x20,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11de <main+0x6e>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1393 <main+0x223>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1261 <main+0xf1>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1393 <main+0x223>
mov    -0x2c(%rbp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
call   1040 <set_interface_attribs@plt>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x800,%eax
ja     134b <main+0x1db>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    12c8 <main+0x158>
mov    -0x2c(%rbp),%edi
mov    $0x20,%esi
call   1070 <testBlockW@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
jmp    132d <main+0x1bd>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    12fa <main+0x18a>
mov    -0x2c(%rbp),%edi
mov    $0x20,%esi
call   1030 <testBlockR@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
jmp    1328 <main+0x1b8>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%edx
lea    0xd36(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x20(%rbp)
jmp    134b <main+0x1db>
jmp    132d <main+0x1bd>
mov    -0x14(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1280 <main+0x110>
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
je     1379 <main+0x209>
mov    -0x20(%rbp),%esi
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    138c <main+0x21c>
lea    0xd30(%rip),%rdi
mov    $0x10000,%esi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x3,%edi
jne    125c <main+0xcc>
mov    %rsi,%r14
mov    0x2e3d(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r14),%rbx
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     129b <main+0x10b>
mov    %eax,%r15d
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1060 <set_interface_attribs@plt>
lea    0xecf(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%ebp
lea    0xe4f(%rip),%r12
xor    %ebx,%ebx
jmp    1238 <main+0xa8>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    $0x20,%esi
call   1050 <testBlockR@plt>
add    %eax,%ebx
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    -0x1(%rbp),%eax
add    $0x2,%ebp
cmp    $0x7ff,%eax
jae    127d <main+0xed>
mov    0x10(%r14),%rax
movsbl (%rax),%edx
cmp    $0x72,%edx
je     1210 <main+0x80>
cmp    $0x77,%dl
jne    12cf <main+0x13f>
mov    %r15d,%edi
mov    $0x20,%esi
call   1090 <testBlockW@plt>
jmp    121d <main+0x8d>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1307 <main+0x177>
mov    $0xa,%edi
call   1030 <putchar@plt>
test   %ebx,%ebx
jne    12f5 <main+0x165>
lea    0xe0f(%rip),%rdi
xor    %ebp,%ebp
mov    $0x10000,%esi
jmp    1300 <main+0x170>
mov    0x2d26(%rip),%rax
mov    (%rax),%ebp
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd6d(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1307 <main+0x177>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd5d(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0x1,%ebx
lea    0xd5a(%rip),%rdi
xor    %ebp,%ebp
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Expected port name '/dev/ttyXXX' r|w\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.1 = [22 x i8] c"Error opening %s: %s\0A\00"
@errno = global i32 0
@B115200 = global i32 0
@.str.2 = [15 x i8] c"Test Started:\0A\00"
@.str.3 = [18 x i8] c"unknown test: %c\0A\00"
@.str.4 = [8 x i8] c"B%04d \0A\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [75 x i8] c"Test FAILED\0AThere was %d errors in Serial communication\0A Check your setup\0A\00"
@.str.7 = [28 x i8] c"Test PASSED\0A Written: %d B\0A\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 i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
store i32 2048, i32* %7
store i32 32, i32* %8
store i32 0, i32* %9
store i8* null, i8** %10
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %99
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %10
%21 = load i8*, i8** %10
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_NOCTTY
%24 = or i32 %22, %23
%25 = load i32, i32* @O_SYNC
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %21, i32 %26)
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %17
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %10
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %34)
store i32 -1, i32* %3
br label %99
36: ; preds = %17
%37 = load i32, i32* %11
%38 = load i32, i32* @B115200
%39 = call i32 @set_interface_attribs(i32 %37, i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %85, %36
%42 = load i32, i32* %6
%43 = add i32 %42, 1
store i32 %43, i32* %6
%44 = icmp ule i32 %42, 2048
br i1 %44, label %45, label %89
45: ; preds = %41
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = getelementptr inbounds i8, i8* %48, i64 0
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 119
br i1 %52, label %53, label %60
53: ; preds = %45
%54 = load i32, i32* %11
%55 = call i64 @testBlockW(i32 %54, i32 32)
%56 = load i32, i32* %9
%57 = zext i32 %56 to i64
%58 = add nsw i64 %57, %55
%59 = trunc i64 %58 to i32
store i32 %59, i32* %9
br label %85
60: ; preds = %45
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 2
%63 = load i8*, i8** %62
%64 = getelementptr inbounds i8, i8* %63, i64 0
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = icmp eq i32 %66, 114
br i1 %67, label %68, label %75
68: ; preds = %60
%69 = load i32, i32* %11
%70 = call i64 @testBlockR(i32 %69, i32 32)
%71 = load i32, i32* %9
%72 = zext i32 %71 to i64
%73 = add nsw i64 %72, %70
%74 = trunc i64 %73 to i32
store i32 %74, i32* %9
br label %84
75: ; preds = %60
%76 = load i32, i32* @stderr
%77 = load i8**, i8*** %5
%78 = getelementptr inbounds i8*, i8** %77, i64 2
%79 = load i8*, i8** %78
%80 = getelementptr inbounds i8, i8* %79, i64 0
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %82)
store i32 1, i32* %9
br label %89
84: ; preds = %68
br label %85
85: ; preds = %84, %53
%86 = load i32, i32* %6
%87 = add i32 %86, 1
store i32 %87, i32* %6
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %86)
br label %41
89: ; preds = %75, %41
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%91 = load i32, i32* %9
%92 = icmp ne i32 %91, 0
br i1 %92, label %93, label %96
93: ; preds = %89
%94 = load i32, i32* %9
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.6, i64 0, i64 0), i32 %94)
br label %98
96: ; preds = %89
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 65536)
br label %98
98: ; preds = %96, %93
store i32 0, i32* %3
br label %99
99: ; preds = %98, %30, %14
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Expected port name '/dev/ttyXXX' r|w\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.1 = [22 x i8] c"Error opening %s: %s\0A\00"
@errno = global i32 0
@B115200 = global i32 0
@.str.3 = [18 x i8] c"unknown test: %c\0A\00"
@.str.4 = [8 x i8] c"B%04d \0A\00"
@.str.6 = [75 x i8] c"Test FAILED\0AThere was %d errors in Serial communication\0A Check your setup\0A\00"
@.str.7 = [28 x i8] c"Test PASSED\0A Written: %d B\0A\00"
@str = [14 x i8] c"Test Started:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %58
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %11, %10
%13 = load i32, i32* @O_SYNC
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %9, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %7
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %20)
br label %58
22: ; preds = %7
%23 = load i32, i32* @B115200
%24 = tail call i32 @set_interface_attribs(i32 %15, i32 %23)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%26 = getelementptr inbounds i8*, i8** %1, i64 2
br label %27
27: ; preds = %22, %42
%28 = phi i32 [ 2, %22 ], [ %48, %42 ]
%29 = phi i32 [ 0, %22 ], [ %45, %42 ]
%30 = phi i32 [ 1, %22 ], [ %46, %42 ]
%31 = load i8*, i8** %26
%32 = load i8, i8* %31
switch i8 %32, label %37 [
i8 119, label %33
i8 114, label %35
]
33: ; preds = %27
%34 = tail call i64 @testBlockW(i32 %15, i32 32)
br label %42
35: ; preds = %27
%36 = tail call i64 @testBlockR(i32 %15, i32 32)
br label %42
37: ; preds = %27
%38 = sext i8 %32 to i32
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%41 = tail call i32 @putchar(i32 10)
br label %53
42: ; preds = %35, %33
%43 = phi i64 [ %34, %33 ], [ %36, %35 ]
%44 = trunc i64 %43 to i32
%45 = add i32 %29, %44
%46 = add nuw nsw i32 %30, 2
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%48 = add nuw nsw i32 %30, 3
%49 = icmp ult i32 %30, 2047
br i1 %49, label %27, label %50
50: ; preds = %42
%51 = tail call i32 @putchar(i32 10)
%52 = icmp eq i32 %45, 0
br i1 %52, label %56, label %53
53: ; preds = %37, %50
%54 = phi i32 [ 1, %37 ], [ %45, %50 ]
%55 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.6, i64 0, i64 0), i32 %54)
br label %58
56: ; preds = %50
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 65536)
br label %58
58: ; preds = %53, %56, %17, %4
%59 = phi i32 [ -1, %4 ], [ -1, %17 ], [ 0, %56 ], [ 0, %53 ]
ret i32 %59
}