#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ RS232_CloseComport(int);
scalar_t__ RS232_OpenComport(int, int, char *);
int RS232_PollComport(int, unsigned char *, int);
int /*<<< orphan*/ RS232_SendByte(int, unsigned char);
int /*<<< orphan*/ Sleep(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *stderr;
int main(void) {
  int bauds = 115200;
  int comport_num = 6;
  char mode[10] = "8N1";
  unsigned char buffer[4096];
  unsigned char results[16][65535];
  int i = 0;
  int j = 0;
  if (RS232_OpenComport(comport_num, bauds, mode)) {
    fprintf(stderr, "Nao consegui abrir COM port %d\n", comport_num + 1);
    exit(1);
  }
  FILE *output;
  if (!(output = fopen("resultado.txt", "w"))) {
    fprintf(stderr, "Nao consegui abrir o arquivo resultado.txt\n");
    exit(1);
  }
  for (i = 0; i < 16; i++) {
    int total = 0;
    RS232_SendByte(comport_num, (unsigned char)i);
    while (total < 65535) {
      int n = RS232_PollComport(comport_num, buffer, 4096);
      for (j = 0; j < n; j++, total++) {
        results[i][total] = buffer[j];
      }
    }
    fprintf(output, "Entrada: %d\n", i);
    fprintf(output, "Saida: ");
    for (j = 0; j < 65535; j++) {
      fprintf(output, "%d ", results[i][j]);
    }
    fprintf(output, "\n");
    Sleep(1000);
  }
  fclose(output);
  RS232_CloseComport(comport_num);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x101030,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1c200,-0x8(%rbp)
movl   $0x6,-0xc(%rbp)
mov    0xe49(%rip),%rax
mov    %rax,-0x16(%rbp)
mov    0xe46(%rip),%ax
mov    %ax,-0xe(%rbp)
movl   $0x0,-0x101014(%rbp)
movl   $0x0,-0x101018(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x16(%rbp),%rdx
call   1090 <RS232_OpenComport@plt>
cmp    $0x0,%rax
je     121b <main+0x8b>
mov    0x2de6(%rip),%rax
mov    (%rax),%rdi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe08(%rip),%rdi
lea    0xe0f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x101020(%rbp)
cmp    $0x0,%rax
jne    1261 <main+0xd1>
mov    0x2d9a(%rip),%rax
mov    (%rax),%rdi
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x101014(%rbp)
cmpl   $0x10,-0x101014(%rbp)
jge    13fa <main+0x26a>
movl   $0x0,-0x101024(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x101014(%rbp),%eax
movzbl %al,%esi
call   1040 <RS232_SendByte@plt>
cmpl   $0xffff,-0x101024(%rbp)
jge    1331 <main+0x1a1>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <RS232_PollComport@plt>
mov    %eax,-0x101028(%rbp)
movl   $0x0,-0x101018(%rbp)
mov    -0x101018(%rbp),%eax
cmp    -0x101028(%rbp),%eax
jge    132c <main+0x19c>
movslq -0x101018(%rbp),%rax
mov    -0x1020(%rbp,%rax,1),%dl
movslq -0x101014(%rbp),%rcx
lea    -0x101010(%rbp),%rax
imul   $0xffff,%rcx,%rcx
add    %rcx,%rax
movslq -0x101024(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x101018(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x101018(%rbp)
mov    -0x101024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x101024(%rbp)
jmp    12c7 <main+0x137>
jmp    1293 <main+0x103>
mov    -0x101020(%rbp),%rdi
mov    -0x101014(%rbp),%edx
lea    0xd21(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x101020(%rbp),%rdi
lea    0xd19(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x101018(%rbp)
cmpl   $0xffff,-0x101018(%rbp)
jge    13c7 <main+0x237>
mov    -0x101020(%rbp),%rdi
movslq -0x101014(%rbp),%rcx
lea    -0x101010(%rbp),%rax
imul   $0xffff,%rcx,%rcx
add    %rcx,%rax
movslq -0x101018(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xccf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x101018(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x101018(%rbp)
jmp    136b <main+0x1db>
mov    -0x101020(%rbp),%rdi
lea    0xc9c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x3e8,%edi
call   10b0 <Sleep@plt>
mov    -0x101014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x101014(%rbp)
jmp    126b <main+0xdb>
mov    -0x101020(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0xc(%rbp),%edi
call   1060 <RS232_CloseComport@plt>
mov    0x2bc3(%rip),%rax
mov    (%rax),%eax
add    $0x101030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x101018,%rsp
movw   $0x0,0x10(%rsp)
movq   $0x314e38,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    $0x6,%edi
mov    $0x1c200,%esi
call   10b0 <RS232_OpenComport@plt>
test   %rax,%rax
jne    1367 <main+0x1a7>
lea    0xe25(%rip),%rdi
lea    0xe2c(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     138e <main+0x1ce>
mov    %rax,%r13
lea    0x1020(%rsp),%r12
lea    0x20(%rsp),%rbx
lea    0xe4a(%rip),%rbp
xor    %r14d,%r14d
mov    %rax,0x18(%rsp)
nopl   0x0(%rax)
movzbl %r14b,%esi
mov    $0x6,%edi
call   1040 <RS232_SendByte@plt>
xor    %r13d,%r13d
jmp    1269 <main+0xa9>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0xfffe,%r13d
jg     12b0 <main+0xf0>
mov    $0x6,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <RS232_PollComport@plt>
test   %eax,%eax
jle    1260 <main+0xa0>
mov    %eax,%r15d
movslq %r13d,%r13
mov    %r14,%rax
shl    $0x10,%rax
sub    %r14,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x1020,%rdi
add    %r13,%rdi
mov    %r15d,%edx
mov    %rbx,%rsi
call   1090 <memcpy@plt>
add    %r15d,%r13d
jmp    1260 <main+0xa0>
nopl   (%rax)
mov    %rbx,%r15
mov    0x18(%rsp),%r13
mov    %r13,%rdi
lea    0xda4(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x7,%esi
mov    $0x1,%edx
lea    0xd96(%rip),%rdi
mov    %r13,%rcx
call   10d0 <fwrite@plt>
mov    $0xffffffffffff0001,%rbx
nopl   0x0(%rax)
movzbl 0xffff(%r12,%rbx,1),%edx
mov    %r13,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
inc    %rbx
jne    12f0 <main+0x130>
mov    $0xa,%edi
mov    %r13,%rsi
call   1070 <fputc@plt>
mov    $0x3e8,%edi
call   10e0 <Sleep@plt>
add    $0x1,%r14
add    $0xffff,%r12
cmp    $0x10,%r14
mov    %r15,%rbx
jne    1240 <main+0x80>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    $0x6,%edi
call   1060 <RS232_CloseComport@plt>
mov    0x2c85(%rip),%rax
mov    (%rax),%eax
add    $0x101018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c72(%rip),%rax
mov    (%rax),%rdi
lea    0xc92(%rip),%rsi
mov    $0x7,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2c4b(%rip),%rax
mov    (%rax),%rcx
lea    0xc9b(%rip),%rdi
mov    $0x2b,%esi
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@__const.main.mode = [10 x i8] c"8N1\00\00\00\00\00\00\00"
@stderr = global i32* null
@.str = [32 x i8] c"Nao consegui abrir COM port %d\0A\00"
@.str.1 = [14 x i8] c"resultado.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [44 x i8] c"Nao consegui abrir o arquivo resultado.txt\0A\00"
@.str.4 = [13 x i8] c"Entrada: %d\0A\00"
@.str.5 = [8 x i8] c"Saida: \00"
@.str.6 = [4 x i8] c"%d \00"
@.str.7 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [10 x i8]
%5 = alloca [4096 x i8]
%6 = alloca [16 x [65535 x i8]]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
store i32 115200, i32* %2
store i32 6, i32* %3
%12 = bitcast [10 x i8]* %4 to i8*
store i32 0, i32* %7
store i32 0, i32* %8
%13 = load i32, i32* %3
%14 = load i32, i32* %2
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%16 = call i64 @RS232_OpenComport(i32 %13, i32 %14, i8* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %0
%19 = load i32*, i32** @stderr
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %0
%25 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %9
%26 = icmp ne i32* %25, null
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i32*, i32** @stderr
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %24
store i32 0, i32* %7
br label %32
32: ; preds = %95, %31
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 16
br i1 %34, label %35, label %98
35: ; preds = %32
store i32 0, i32* %10
%36 = load i32, i32* %3
%37 = load i32, i32* %7
%38 = trunc i32 %37 to i8
%39 = call i32 @RS232_SendByte(i32 %36, i8 zeroext %38)
br label %40
40: ; preds = %67, %35
%41 = load i32, i32* %10
%42 = icmp slt i32 %41, 65535
br i1 %42, label %43, label %68
43: ; preds = %40
%44 = load i32, i32* %3
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%46 = call i32 @RS232_PollComport(i32 %44, i8* %45, i32 4096)
store i32 %46, i32* %11
store i32 0, i32* %8
br label %47
47: ; preds = %62, %43
%48 = load i32, i32* %8
%49 = load i32, i32* %11
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %67
51: ; preds = %47
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %53
%55 = load i8, i8* %54
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [16 x [65535 x i8]], [16 x [65535 x i8]]* %6, i64 0, i64 %57
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [65535 x i8], [65535 x i8]* %58, i64 0, i64 %60
store i8 %55, i8* %61
br label %62
62: ; preds = %51
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
%65 = load i32, i32* %10
%66 = add nsw i32 %65, 1
store i32 %66, i32* %10
br label %47
67: ; preds = %47
br label %40
68: ; preds = %40
%69 = load i32*, i32** %9
%70 = load i32, i32* %7
%71 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %70)
%72 = load i32*, i32** %9
%73 = call i32 (i32*, i8*, ...) @fprintf(i32* %72, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %8
br label %74
74: ; preds = %88, %68
%75 = load i32, i32* %8
%76 = icmp slt i32 %75, 65535
br i1 %76, label %77, label %91
77: ; preds = %74
%78 = load i32*, i32** %9
%79 = load i32, i32* %7
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [16 x [65535 x i8]], [16 x [65535 x i8]]* %6, i64 0, i64 %80
%82 = load i32, i32* %8
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [65535 x i8], [65535 x i8]* %81, i64 0, i64 %83
%85 = load i8, i8* %84
%86 = zext i8 %85 to i32
%87 = call i32 (i32*, i8*, ...) @fprintf(i32* %78, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %86)
br label %88
88: ; preds = %77
%89 = load i32, i32* %8
%90 = add nsw i32 %89, 1
store i32 %90, i32* %8
br label %74
91: ; preds = %74
%92 = load i32*, i32** %9
%93 = call i32 (i32*, i8*, ...) @fprintf(i32* %92, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%94 = call i32 @Sleep(i32 1000)
br label %95
95: ; preds = %91
%96 = load i32, i32* %7
%97 = add nsw i32 %96, 1
store i32 %97, i32* %7
br label %32
98: ; preds = %32
%99 = load i32*, i32** %9
%100 = call i32 @fclose(i32* %99)
%101 = load i32, i32* %3
%102 = call i32 @RS232_CloseComport(i32 %101)
%103 = load i32, i32* @EXIT_SUCCESS
ret i32 %103
}

LLVM IR (-O2):
@__const.main.mode = [10 x i8] c"8N1\00\00\00\00\00\00\00"
@stderr = global i32* null
@.str = [32 x i8] c"Nao consegui abrir COM port %d\0A\00"
@.str.1 = [14 x i8] c"resultado.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [44 x i8] c"Nao consegui abrir o arquivo resultado.txt\0A\00"
@.str.4 = [13 x i8] c"Entrada: %d\0A\00"
@.str.5 = [8 x i8] c"Saida: \00"
@.str.6 = [4 x i8] c"%d \00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [10 x i8]
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = alloca [16 x [65535 x i8]]
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [16 x [65535 x i8]], [16 x [65535 x i8]]* %4, i64 0, i64 0, i64 0
%8 = call i64 @RS232_OpenComport(i32 6, i32 115200, i8* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %0
%11 = load i32*, i32** @stderr
%12 = call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 7)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32*, i32** @stderr
%19 = call i64 @fwrite(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64 43, i64 1, i32* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14, %49
%22 = phi i64 [ %52, %49 ], [ 0, %14 ]
%23 = trunc i64 %22 to i32
%24 = trunc i64 %22 to i8
%25 = call i32 @RS232_SendByte(i32 6, i8 zeroext %24)
br label %29
26: ; preds = %33, %29
%27 = phi i32 [ %30, %29 ], [ %37, %33 ]
%28 = icmp slt i32 %27, 65535
br i1 %28, label %29, label %38
29: ; preds = %21, %26
%30 = phi i32 [ 0, %21 ], [ %27, %26 ]
%31 = call i32 @RS232_PollComport(i32 6, i8* %6, i32 4096)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %26
33: ; preds = %29
%34 = sext i32 %30 to i64
%35 = getelementptr [16 x [65535 x i8]], [16 x [65535 x i8]]* %4, i64 0, i64 %22, i64 %34
%36 = zext i32 %31 to i64
%37 = add i32 %30, %31
br label %26
38: ; preds = %26
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%40 = call i64 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i64 7, i64 1, i32* %15)
br label %41
41: ; preds = %38, %41
%42 = phi i64 [ 0, %38 ], [ %47, %41 ]
%43 = getelementptr inbounds [16 x [65535 x i8]], [16 x [65535 x i8]]* %4, i64 0, i64 %22, i64 %42
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %45)
%47 = add nuw nsw i64 %42, 1
%48 = icmp eq i64 %47, 65535
br i1 %48, label %49, label %41
49: ; preds = %41
%50 = call i32 @fputc(i32 10, i32* %15)
%51 = call i32 @Sleep(i32 1000)
%52 = add nuw nsw i64 %22, 1
%53 = icmp eq i64 %52, 16
br i1 %53, label %54, label %21
54: ; preds = %49
%55 = call i32 @fclose(i32* %15)
%56 = call i32 @RS232_CloseComport(i32 6)
%57 = load i32, i32* @EXIT_SUCCESS
ret i32 %57
}