#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int main(void) {
  FILE *serialDev;
  int i;
  char sendBuffer[80] = "";
  char recvBuffer[80] = "";
  printf("Text to send: ");
  scanf("%s", sendBuffer);
  serialDev = fopen("/dev/serial0", "r+");
  for (i = 0; i < 10; i++) {
    fprintf(serialDev, "%s%d\r\n", sendBuffer, i);
  }
  fprintf(serialDev, "Please type something ^_^ (type stop to close)\n");
  while (1) {
    fgets(recvBuffer, 80, serialDev);
    if (!strcmp(recvBuffer, "stop\n")) {
      printf("stopping\n");
      break;
    }
    printf("Received: %s", recvBuffer);
  }
  fclose(serialDev);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x70(%rbp),%rsi
lea    0xea2(%rip),%rdi
call   1090 <scanf@plt>
lea    0xe35(%rip),%rdi
lea    0xe3b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    1222 <main+0xa2>
mov    -0x10(%rbp),%rdi
lea    -0x70(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f1 <main+0x71>
mov    -0x10(%rbp),%rdi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0xc0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x50,%esi
call   1060 <fgets@plt>
lea    -0xc0(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1278 <main+0xf8>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1292 <main+0x112>
lea    -0xc0(%rbp),%rsi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1234 <main+0xb4>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xa0,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe30(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe88(%rip),%rdi
lea    0x50(%rsp),%r14
mov    %r14,%rsi
call   1080 <scanf@plt>
lea    0xe1d(%rip),%rdi
lea    0xe23(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r12
lea    0xe17(%rip),%r15
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x1,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x2,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x3,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x4,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x5,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x6,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x7,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x8,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x9,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xd48(%rip),%rdi
mov    $0x2f,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
mov    %rsp,%rdi
mov    $0x50,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
mov    $0x706f7473,%ebp
mov    (%rsp),%eax
xor    %ebp,%eax
movzwl 0x4(%rsp),%ecx
xor    $0xa,%ecx
or     %eax,%ecx
je     134e <main+0x1ce>
lea    0xd3f(%rip),%r14
mov    %rsp,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x50,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
mov    (%rsp),%eax
xor    %ebp,%eax
movzwl 0x4(%rsp),%ecx
xor    $0xa,%ecx
or     %eax,%ecx
jne    1320 <main+0x1a0>
lea    0xd14(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Text to send: \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [13 x i8] c"/dev/serial0\00"
@.str.3 = [3 x i8] c"r+\00"
@.str.4 = [7 x i8] c"%s%d\0D\0A\00"
@.str.5 = [48 x i8] c"Please type something ^_^ (type stop to close)\0A\00"
@.str.6 = [6 x i8] c"stop\0A\00"
@.str.7 = [10 x i8] c"stopping\0A\00"
@.str.8 = [13 x i8] c"Received: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = alloca [80 x i8]
store i32 0, i32* %1
%6 = bitcast [80 x i8]* %4 to i8*
%7 = bitcast [80 x i8]* %5 to i8*
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %11, i32** %2
store i32 0, i32* %3
br label %12
12: ; preds = %20, %0
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 10
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i32*, i32** %2
%17 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%18 = load i32, i32* %3
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %17, i32 %18)
br label %20
20: ; preds = %15
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %12
23: ; preds = %12
%24 = load i32*, i32** %2
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %26
26: ; preds = %23, %35
%27 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%28 = load i32*, i32** %2
%29 = call i32 @fgets(i8* %27, i32 80, i32* %28)
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%31 = call i32 @strcmp(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %26
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
br label %38
35: ; preds = %26
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i8* %36)
br label %26
38: ; preds = %33
%39 = load i32*, i32** %2
%40 = call i32 @fclose(i32* %39)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"Text to send: \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [13 x i8] c"/dev/serial0\00"
@.str.3 = [3 x i8] c"r+\00"
@.str.4 = [7 x i8] c"%s%d\0D\0A\00"
@.str.5 = [48 x i8] c"Please type something ^_^ (type stop to close)\0A\00"
@.str.6 = [6 x i8] c"stop\0A\00"
@.str.8 = [13 x i8] c"Received: %s\00"
@str = [9 x i8] c"stopping\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%7 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%8 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 0)
%9 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 1)
%10 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 2)
%11 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 3)
%12 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 4)
%13 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 5)
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 6)
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 7)
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 8)
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 9)
%18 = call i64 @fwrite(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0), i64 47, i64 1, i32* %7)
%19 = call i32 @fgets(i8* %4, i32 80, i32* %7)
%20 = call i32 @bcmp(i8* dereferenceable(6) %4, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i64 6)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %25, %0
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%24 = call i32 @fclose(i32* %7)
ret i32 0
25: ; preds = %0, %25
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i8* %4)
%27 = call i32 @fgets(i8* %4, i32 80, i32* %7)
%28 = call i32 @bcmp(i8* dereferenceable(6) %4, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i64 6)
%29 = icmp eq i32 %28, 0
br i1 %29, label %22, label %25
}