#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 char uint8_t;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int errno;
int /*<<< orphan*/ free(char *);
int get_payload_size(char *);
int open(char *, int);
char *prep_uart_data();
int /*<<< orphan*/ print_uart_data(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ set_blocking(int, int /*<<< orphan*/);
int /*<<< orphan*/ set_interface_attribs(int, int /*<<< orphan*/,
                                         int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
char *strerror(int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  uint8_t *buf = NULL;
  int len = 0;
  char *portname = "/dev/ttyO1";
  int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  char rxbuf[100];
  srand(time(NULL));
  set_interface_attribs(fd, B115200, 0);
  set_blocking(fd, 0);
  write(fd, "hello!\n", 7);
  usleep((7 + 25) * 100);
  while (1) {
    if (fd < 0) {
      printf("error %d opening %s: %s", errno, portname, strerror(errno));
      return -1;
    }
    buf = prep_uart_data();
    len = get_payload_size(buf);
    print_uart_data(buf);
    write(fd, buf, len);
    usleep((len + 25) * 100);
    free(buf);
    int n = read(fd, rxbuf, sizeof buf);
    printf("number of read characters: %d\n", n);
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xde8(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
mov    0x2da7(%rip),%rax
or     (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <time@plt>
mov    %eax,%edi
call   10a0 <srand@plt>
mov    -0x24(%rbp),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <set_interface_attribs@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1080 <set_blocking@plt>
mov    -0x24(%rbp),%edi
lea    0xd91(%rip),%rsi
mov    $0x7,%edx
call   1050 <write@plt>
mov    $0xc80,%edi
call   1110 <usleep@plt>
cmpl   $0x0,-0x24(%rbp)
jge    12ec <main+0xfc>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0xa4(%rbp),%esi
mov    -0xa0(%rbp),%rdx
mov    %rax,%rcx
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0xffffffff,%eax
add    $0xb0,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   10b0 <prep_uart_data@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <get_payload_size@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   10e0 <print_uart_data@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <write@plt>
mov    -0x14(%rbp),%eax
add    $0x19,%eax
imul   $0x64,%eax,%edi
call   1110 <usleep@plt>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
mov    -0x24(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x8,%edx
call   1090 <read@plt>
mov    %eax,-0x94(%rbp)
mov    -0x94(%rbp),%esi
lea    0xcd2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   1100 <sleep@plt>
jmp    128e <main+0x9e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    0x2de5(%rip),%rax
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dcb(%rip),%rax
or     (%rax),%esi
lea    0xde2(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%r12d
xor    %edi,%edi
call   10c0 <time@plt>
mov    %eax,%edi
call   10a0 <srand@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
xor    %edx,%edx
call   1060 <set_interface_attribs@plt>
mov    %r12d,%edi
xor    %esi,%esi
call   1080 <set_blocking@plt>
lea    0xdb3(%rip),%rsi
mov    %r12d,%edi
mov    $0x7,%edx
call   1050 <write@plt>
mov    $0xc80,%edi
call   1110 <usleep@plt>
test   %r12d,%r12d
js     12e7 <main+0xf7>
mov    %rsp,%r14
lea    0xdad(%rip),%r15
xchg   %ax,%ax
xor    %eax,%eax
call   10b0 <prep_uart_data@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <get_payload_size@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   10e0 <print_uart_data@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
imul   $0x64,%ebp,%edi
add    $0x9c4,%edi
call   1110 <usleep@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1090 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1100 <sleep@plt>
jmp    1280 <main+0x90>
mov    0x2cda(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10f0 <strerror@plt>
lea    0xd15(%rip),%rdi
lea    0xcfb(%rip),%rdx
mov    %ebx,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/ttyO1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@B115200 = global i32 0
@.str.1 = [8 x i8] c"hello!\0A\00"
@.str.2 = [24 x i8] c"error %d opening %s: %s\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"number of read characters: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca i32
store i32 0, i32* %1
store i8* null, i8** %2
store i32 0, i32* %3
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %4
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_SYNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13)
store i32 %14, i32* %5
%15 = call i32 @time(i32* null)
%16 = call i32 @srand(i32 %15)
%17 = load i32, i32* %5
%18 = load i32, i32* @B115200
%19 = call i32 @set_interface_attribs(i32 %17, i32 %18, i32 0)
%20 = load i32, i32* %5
%21 = call i32 @set_blocking(i32 %20, i32 0)
%22 = load i32, i32* %5
%23 = call i32 @write(i32 %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7)
%24 = call i32 @usleep(i32 3200)
br label %25
25: ; preds = %0, %34
%26 = load i32, i32* %5
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32, i32* @errno
%30 = load i8*, i8** %4
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %29, i8* %30, i8* %32)
ret i32 -1
34: ; preds = %25
%35 = call i8* (...) @prep_uart_data()
store i8* %35, i8** %2
%36 = load i8*, i8** %2
%37 = call i32 @get_payload_size(i8* %36)
store i32 %37, i32* %3
%38 = load i8*, i8** %2
%39 = call i32 @print_uart_data(i8* %38)
%40 = load i32, i32* %5
%41 = load i8*, i8** %2
%42 = load i32, i32* %3
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 25
%46 = mul nsw i32 %45, 100
%47 = call i32 @usleep(i32 %46)
%48 = load i8*, i8** %2
%49 = call i32 @free(i8* %48)
%50 = load i32, i32* %5
%51 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 8)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %53)
%55 = call i32 @sleep(i32 1)
br label %25
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ttyO1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@B115200 = global i32 0
@.str.1 = [8 x i8] c"hello!\0A\00"
@.str.2 = [24 x i8] c"error %d opening %s: %s\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"number of read characters: %d\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_SYNC
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%9 = tail call i32 @time(i32* null)
%10 = tail call i32 @srand(i32 %9)
%11 = load i32, i32* @B115200
%12 = tail call i32 @set_interface_attribs(i32 %7, i32 %11, i32 0)
%13 = tail call i32 @set_blocking(i32 %7, i32 0)
%14 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7)
%15 = tail call i32 @usleep(i32 3200)
%16 = icmp slt i32 %7, 0
br i1 %16, label %17, label %21
17: ; preds = %0
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %19)
ret i32 -1
21: ; preds = %0, %21
%22 = tail call i8* (...) @prep_uart_data()
%23 = tail call i32 @get_payload_size(i8* %22)
%24 = tail call i32 @print_uart_data(i8* %22)
%25 = tail call i32 @write(i32 %7, i8* %22, i32 %23)
%26 = mul i32 %23, 100
%27 = add i32 %26, 2500
%28 = tail call i32 @usleep(i32 %27)
%29 = tail call i32 @free(i8* %22)
%30 = call i32 @read(i32 %7, i8* %8, i32 8)
%31 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = tail call i32 @sleep(i32 1)
br label %21
}