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

/* Variables and functions */
char EOF;
int errno;
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int write_serial(FILE *file, int port, useconds_t delay, size_t *written) {
  int result = 0;
  char str[2] = {0};
  size_t bytes_written = 0;
  if (!file || !port) {
    fprintf(stderr, "[ERROR] Invalid write parameter: File: 0x%p, Port: %d\n",
            file, port);
    result = errno;
    goto exit;
  }
  fprintf(stdout, "[INFO] Writting to serial... ");
  while ((str[0] = fgetc(file)) != EOF) {
    write(port, str, 1);
    ++bytes_written;
    usleep(delay);
  }
  write(port, "run\r\n", 5);
  fprintf(stdout, "Done.\n[INFO] %.02f KB (%lu bytes) written to serial\n",
          (bytes_written / (1024.0)), bytes_written);
  if (written) {
    *written = bytes_written;
  }
exit:
  return result;
}


x86 assembly (clang, -O0):
<write_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x1e(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <memset@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     119a <write_serial+0x4a>
cmpl   $0x0,-0xc(%rbp)
jne    11c9 <write_serial+0x79>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe7f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    12a8 <write_serial+0x158>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe8e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x1e(%rbp)
movsbl %al,%eax
mov    0x2df2(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     122b <write_serial+0xdb>
mov    -0xc(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <usleep@plt>
jmp    11e0 <write_serial+0x90>
mov    -0xc(%rbp),%edi
lea    0xe50(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
movaps 0xdc1(%rip),%xmm1
movq   -0x28(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xdc0(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xd89(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    -0x28(%rbp),%rdx
lea    0xe05(%rip),%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
cmpq   $0x0,-0x18(%rbp)
je     12a3 <write_serial+0x153>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    12a8 <write_serial+0x158>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_serial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r12d
mov    %rdi,%rbx
movw   $0x0,0xe(%rsp)
test   %rdi,%rdi
je     11a3 <write_serial+0x63>
test   %r12d,%r12d
je     11a3 <write_serial+0x63>
mov    %rcx,%rbp
mov    %edx,%r13d
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xee4(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %al,0xe(%rsp)
mov    0x2e53(%rip),%r15
cmp    (%r15),%al
mov    %rbp,0x10(%rsp)
jne    11ce <write_serial+0x8e>
xor    %ebp,%ebp
jmp    120d <write_serial+0xcd>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
mov    %rbx,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
jmp    1273 <write_serial+0x133>
xor    %ebp,%ebp
lea    0xe(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x1,%rbp
mov    %r13d,%edi
call   1060 <usleep@plt>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %al,0xe(%rsp)
cmp    (%r15),%al
jne    11e0 <write_serial+0xa0>
lea    0xe69(%rip),%rsi
mov    %r12d,%edi
mov    $0x5,%edx
call   1030 <write@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
movq   %rbp,%xmm1
punpckldq 0xdc9(%rip),%xmm1
subpd  0xdd1(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
mulsd  0xdcd(%rip),%xmm0
lea    0xe29(%rip),%rsi
mov    %rbp,%rdx
mov    $0x1,%al
call   1050 <fprintf@plt>
mov    0x10(%rsp),%rax
test   %rax,%rax
je     1271 <write_serial+0x131>
mov    %rbp,(%rax)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [55 x i8] c"[ERROR] Invalid write parameter: File: 0x%p, Port: %d\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.1 = [30 x i8] c"[INFO] Writting to serial... \00"
@EOF = global i8 0
@.str.2 = [6 x i8] c"run\0D\0A\00"
@.str.3 = [53 x i8] c"Done.\0A[INFO] %.02f KB (%lu bytes) written to serial\0A\00"
define i32 @write_serial(i32* %0, i32 %1, i32 %2, i64* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64*
%9 = alloca i32
%10 = alloca [2 x i8]
%11 = alloca i64
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64* %3, i64** %8
store i32 0, i32* %9
%12 = bitcast [2 x i8]* %10 to i8*
store i64 0, i64* %11
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %15, %4
%19 = load i32, i32* @stderr
%20 = load i32*, i32** %5
%21 = load i32, i32* %6
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32* %20, i32 %21)
%23 = load i32, i32* @errno
store i32 %23, i32* %9
br label %58
24: ; preds = %15
%25 = load i32, i32* @stdout
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %35, %24
%28 = load i32*, i32** %5
%29 = call signext i8 @fgetc(i32* %28)
%30 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
store i8 %29, i8* %30
%31 = sext i8 %29 to i32
%32 = load i8, i8* @EOF
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %31, %33
br i1 %34, label %35, label %43
35: ; preds = %27
%36 = load i32, i32* %6
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%38 = call i32 @write(i32 %36, i8* %37, i32 1)
%39 = load i64, i64* %11
%40 = add i64 %39, 1
store i64 %40, i64* %11
%41 = load i32, i32* %7
%42 = call i32 @usleep(i32 %41)
br label %27
43: ; preds = %27
%44 = load i32, i32* %6
%45 = call i32 @write(i32 %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%46 = load i32, i32* @stdout
%47 = load i64, i64* %11
%48 = uitofp i64 %47 to double
%49 = fdiv double %48, 1.024000e+03
%50 = load i64, i64* %11
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), double %49, i64 %50)
%52 = load i64*, i64** %8
%53 = icmp ne i64* %52, null
br i1 %53, label %54, label %57
54: ; preds = %43
%55 = load i64, i64* %11
%56 = load i64*, i64** %8
store i64 %55, i64* %56
br label %57
57: ; preds = %54, %43
br label %58
58: ; preds = %57, %18
%59 = load i32, i32* %9
ret i32 %59
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [55 x i8] c"[ERROR] Invalid write parameter: File: 0x%p, Port: %d\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.1 = [30 x i8] c"[INFO] Writting to serial... \00"
@EOF = global i8 0
@.str.2 = [6 x i8] c"run\0D\0A\00"
@.str.3 = [53 x i8] c"Done.\0A[INFO] %.02f KB (%lu bytes) written to serial\0A\00"
define i32 @write_serial(i32* %0, i32 %1, i32 %2, i64* writeonly %3) {
%5 = alloca i16
%6 = bitcast i16* %5 to i8*
store i16 0, i16* %5
%7 = icmp ne i32* %0, null
%8 = icmp ne i32 %1, 0
%9 = and i1 %7, %8
br i1 %9, label %14, label %10
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32* %0, i32 %1)
%13 = load i32, i32* @errno
br label %37
14: ; preds = %4
%15 = load i32, i32* @stdout
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call signext i8 @fgetc(i32* %0)
store i8 %17, i8* %6
%18 = load i8, i8* @EOF
%19 = icmp eq i8 %17, %18
br i1 %19, label %28, label %20
20: ; preds = %14, %20
%21 = phi i64 [ %23, %20 ], [ 0, %14 ]
%22 = call i32 @write(i32 %1, i8* %6, i32 1)
%23 = add i64 %21, 1
%24 = tail call i32 @usleep(i32 %2)
%25 = tail call signext i8 @fgetc(i32* %0)
store i8 %25, i8* %6
%26 = load i8, i8* @EOF
%27 = icmp eq i8 %25, %26
br i1 %27, label %28, label %20
28: ; preds = %20, %14
%29 = phi i64 [ 0, %14 ], [ %23, %20 ]
%30 = tail call i32 @write(i32 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%31 = load i32, i32* @stdout
%32 = uitofp i64 %29 to double
%33 = fmul double %32, 0x3F50000000000000
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), double %33, i64 %29)
%35 = icmp eq i64* %3, null
br i1 %35, label %37, label %36
36: ; preds = %28
store i64 %29, i64* %3
br label %37
37: ; preds = %28, %36, %10
%38 = phi i32 [ 0, %36 ], [ 0, %28 ], [ %13, %10 ]
ret i32 %38
}