#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 */
struct termios {
  int c_cflag;
  int /*<<< orphan*/ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int B115200;
int CLOCAL;
int CREAD;
int CS7;
int /*<<< orphan*/ ICANON;
int IGNCR;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_RDONLY;
int PARENB;
int PARODD;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned char *);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, void *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main() {
  int uart0_filestream = -1, i;
  FILE *fp;
  unsigned char datalines[28][100];
  struct termios options;
  uart0_filestream = open("/dev/ttyAMA0", O_RDONLY | O_NOCTTY);
  if (uart0_filestream == -1) {
    printf("Error - Unable to open UART.  Ensure it is not in use by another "
           "application\n");
  }
  tcgetattr(uart0_filestream, &options);
  options.c_cflag = B115200 | CS7 | CLOCAL | CREAD | IGNCR | PARENB | PARODD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = ICANON;
  tcflush(uart0_filestream, TCIFLUSH);
  tcsetattr(uart0_filestream, TCSANOW, &options);
  while (1) {
    for (i = 0; i < 28; i++) {
      int rx_length = read(uart0_filestream, (void *)datalines[i], 99);
      datalines[i][rx_length] = '\0';
    }
    fp = fopen("energemeter-dump.txt", "a");
    for (i = 0; i < 28; i++) {
      printf("%s", datalines[i]);
      fprintf(fp, "%s", datalines[i]);
    }
    fclose(fp);
  }
  close(uart0_filestream);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
or     (%rax),%esi
lea    0xe3e(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e2 <main+0x52>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0xb28(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    0x2d7f(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6e(%rip),%rcx
or     (%rcx),%eax
mov    0x2dad(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcc(%rip),%rcx
or     (%rcx),%eax
mov    0x2db3(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xb28(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xb18(%rbp)
movq   $0x0,-0xb20(%rbp)
mov    0x2d39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xb24(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x8(%rbp),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
lea    -0xb28(%rbp),%rdx
call   1090 <tcsetattr@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x1c,-0xc(%rbp)
jge    12e9 <main+0x159>
mov    -0x8(%rbp),%edi
movslq -0xc(%rbp),%rax
lea    -0xb10(%rbp),%rsi
imul   $0x64,%rax,%rax
add    %rax,%rsi
mov    $0x63,%edx
call   1060 <read@plt>
mov    %eax,-0xb2c(%rbp)
movslq -0xc(%rbp),%rcx
lea    -0xb10(%rbp),%rax
imul   $0x64,%rcx,%rcx
add    %rcx,%rax
movslq -0xb2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    128f <main+0xff>
lea    0xd6b(%rip),%rdi
lea    0xd79(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x1c,-0xc(%rbp)
jge    1361 <main+0x1d1>
movslq -0xc(%rbp),%rax
lea    -0xb10(%rbp),%rsi
imul   $0x64,%rax,%rax
add    %rax,%rsi
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
lea    -0xb10(%rbp),%rdx
imul   $0x64,%rax,%rax
add    %rax,%rdx
lea    0xd24(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1307 <main+0x177>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1288 <main+0xf8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb18,%rsp
mov    0x2e28(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe36(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11e3 <main+0x43>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
call   1090 <tcgetattr@plt>
mov    0x2da6(%rip),%rax
mov    0x2d7f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d6c(%rip),%rax
or     (%rax),%ecx
mov    0x2dab(%rip),%rax
or     (%rax),%ecx
mov    0x2dca(%rip),%rax
or     (%rax),%ecx
mov    0x2db1(%rip),%rax
or     (%rax),%ecx
mov    0x2d98(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1050 <tcflush@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %rbx,%rdx
call   10a0 <tcsetattr@plt>
lea    0xd9d(%rip),%r15
lea    0xd98(%rip),%r13
nopl   0x0(%rax)
xor    %ebx,%ebx
imul   $0x64,%rbx,%rax
lea    (%rsp,%rax,1),%rbp
add    $0x20,%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x63,%edx
call   1080 <read@plt>
cltq
movb   $0x0,(%rax,%rbp,1)
add    $0x1,%rbx
cmp    $0x1c,%rbx
jne    1292 <main+0xf2>
lea    0xd48(%rip),%rdi
mov    %r15,%rsi
call   10c0 <fopen@plt>
mov    %rax,%rbp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%rsp,%r14,1),%rbx
add    $0x20,%rbx
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <fputs@plt>
add    $0x64,%r14
cmp    $0xaf0,%r14
jne    12e0 <main+0x140>
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    1290 <main+0xf0>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i64, i32 }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [78 x i8] c"Error - Unable to open UART. Ensure it is not in use by another application\0A\00"
@B115200 = global i32 0
@CS7 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNCR = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@IGNPAR = global i32 0
@ICANON = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [21 x i8] c"energemeter-dump.txt\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [28 x [100 x i8]]
%6 = alloca %struct.termios
%7 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %2
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32, i32* %2
%18 = call i32 @tcgetattr(i32 %17, %struct.termios* %6)
%19 = load i32, i32* @B115200
%20 = load i32, i32* @CS7
%21 = or i32 %19, %20
%22 = load i32, i32* @CLOCAL
%23 = or i32 %21, %22
%24 = load i32, i32* @CREAD
%25 = or i32 %23, %24
%26 = load i32, i32* @IGNCR
%27 = or i32 %25, %26
%28 = load i32, i32* @PARENB
%29 = or i32 %27, %28
%30 = load i32, i32* @PARODD
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @IGNPAR
%34 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 0, i64* %35
%36 = load i32, i32* @ICANON
%37 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* %2
%39 = load i32, i32* @TCIFLUSH
%40 = call i32 @tcflush(i32 %38, i32 %39)
%41 = load i32, i32* %2
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %41, i32 %42, %struct.termios* %6)
br label %44
44: ; preds = %16, %84
store i32 0, i32* %3
br label %45
45: ; preds = %61, %44
%46 = load i32, i32* %3
%47 = icmp slt i32 %46, 28
br i1 %47, label %48, label %64
48: ; preds = %45
%49 = load i32, i32* %2
%50 = load i32, i32* %3
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [28 x [100 x i8]], [28 x [100 x i8]]* %5, i64 0, i64 %51
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %52, i64 0, i64 0
%54 = call i32 @read(i32 %49, i8* %53, i32 99)
store i32 %54, i32* %7
%55 = load i32, i32* %3
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [28 x [100 x i8]], [28 x [100 x i8]]* %5, i64 0, i64 %56
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %57, i64 0, i64 %59
store i8 0, i8* %60
br label %61
61: ; preds = %48
%62 = load i32, i32* %3
%63 = add nsw i32 %62, 1
store i32 %63, i32* %3
br label %45
64: ; preds = %45
%65 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %65, i32** %4
store i32 0, i32* %3
br label %66
66: ; preds = %81, %64
%67 = load i32, i32* %3
%68 = icmp slt i32 %67, 28
br i1 %68, label %69, label %84
69: ; preds = %66
%70 = load i32, i32* %3
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [28 x [100 x i8]], [28 x [100 x i8]]* %5, i64 0, i64 %71
%73 = getelementptr inbounds [100 x i8], [100 x i8]* %72, i64 0, i64 0
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %73)
%75 = load i32*, i32** %4
%76 = load i32, i32* %3
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds [28 x [100 x i8]], [28 x [100 x i8]]* %5, i64 0, i64 %77
%79 = getelementptr inbounds [100 x i8], [100 x i8]* %78, i64 0, i64 0
%80 = call i32 @fprintf(i32* %75, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %69
%82 = load i32, i32* %3
%83 = add nsw i32 %82, 1
store i32 %83, i32* %3
br label %66
84: ; preds = %66
%85 = load i32*, i32** %4
%86 = call i32 @fclose(i32* %85)
br label %44
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i64, i32 }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@B115200 = global i32 0
@CS7 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNCR = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@IGNPAR = global i32 0
@ICANON = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [21 x i8] c"energemeter-dump.txt\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [3 x i8] c"%s\00"
@str = [77 x i8] c"Error - Unable to open UART. Ensure it is not in use by another application\00"
define i32 @main() {
%1 = alloca [28 x [100 x i8]]
%2 = alloca %struct.termios
%3 = getelementptr inbounds [28 x [100 x i8]], [28 x [100 x i8]]* %1, i64 0, i64 0, i64 0
%4 = bitcast %struct.termios* %2 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([77 x i8], [77 x i8]* @str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = call i32 @tcgetattr(i32 %8, %struct.termios* %2)
%14 = load i32, i32* @B115200
%15 = load i32, i32* @CS7
%16 = or i32 %15, %14
%17 = load i32, i32* @CLOCAL
%18 = or i32 %16, %17
%19 = load i32, i32* @CREAD
%20 = or i32 %18, %19
%21 = load i32, i32* @IGNCR
%22 = or i32 %20, %21
%23 = load i32, i32* @PARENB
%24 = or i32 %22, %23
%25 = load i32, i32* @PARODD
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @IGNPAR
%29 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
store i64 0, i64* %30
%31 = load i32, i32* @ICANON
%32 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @TCIFLUSH
%34 = call i32 @tcflush(i32 %8, i32 %33)
%35 = load i32, i32* @TCSANOW
%36 = call i32 @tcsetattr(i32 %8, i32 %35, %struct.termios* %2)
br label %37
37: ; preds = %45, %12
%38 = phi i64 [ 0, %12 ], [ %46, %45 ]
%39 = getelementptr inbounds [28 x [100 x i8]], [28 x [100 x i8]]* %1, i64 0, i64 %38, i64 0
%40 = call i32 @read(i32 %8, i8* %39, i32 99)
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [28 x [100 x i8]], [28 x [100 x i8]]* %1, i64 0, i64 %38, i64 %41
store i8 0, i8* %42
%43 = add nuw nsw i64 %38, 1
%44 = icmp eq i64 %43, 28
br i1 %44, label %47, label %45
45: ; preds = %37, %56
%46 = phi i64 [ %43, %37 ], [ 0, %56 ]
br label %37
47: ; preds = %37
%48 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %49
%50 = phi i64 [ 0, %47 ], [ %54, %49 ]
%51 = getelementptr inbounds [28 x [100 x i8]], [28 x [100 x i8]]* %1, i64 0, i64 %50, i64 0
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = call i32 @fputs(i8* %51, i32* %48)
%54 = add nuw nsw i64 %50, 1
%55 = icmp eq i64 %54, 28
br i1 %55, label %56, label %49
56: ; preds = %49
%57 = call i32 @fclose(i32* %48)
br label %45
}