#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 */

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ dprintf(int /*<<< orphan*/, char *, ...);
int errno;
int /*<<< orphan*/ msleep(int);
scalar_t__ wait_for_aa(int);
int write(int, unsigned char const *, int);
int write_serial(int fd, const unsigned char *buf, unsigned int size,
                 unsigned int hb) {
  dprintf(STDERR_FILENO, "Sending %d byte command\n", size);
  if (hb && wait_for_aa(fd))
    return -1;
  for (unsigned int x = 0; x < size; x++) {
    dprintf(STDERR_FILENO, "- %02hhx ", buf[x]);
    if (write(fd, buf + x, 1) != 1)
      return errno;
    msleep(16);
  }
  dprintf(STDERR_FILENO, "\n");
  return 0;
}


x86 assembly (clang, -O0):
<write_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1060 <dprintf@plt>
cmpl   $0x0,-0x18(%rbp)
je     1197 <write_serial+0x57>
mov    -0x8(%rbp),%edi
call   1050 <wait_for_aa@plt>
cmp    $0x0,%rax
je     1197 <write_serial+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <write_serial+0xf3>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1215 <write_serial+0xd5>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1060 <dprintf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11fd <write_serial+0xbd>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1233 <write_serial+0xf3>
mov    $0x10,%edi
call   1040 <msleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119e <write_serial+0x5e>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1060 <dprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_serial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r13d
mov    0x2e7a(%rip),%r12
mov    (%r12),%edi
lea    0xe97(%rip),%rsi
xor    %eax,%eax
call   1060 <dprintf@plt>
test   %r15d,%r15d
je     1188 <write_serial+0x48>
mov    %r13d,%edi
call   1050 <wait_for_aa@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
jne    11fb <write_serial+0xbb>
test   %r14d,%r14d
je     11da <write_serial+0x9a>
mov    %r14d,%ebp
lea    0xe82(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
movzbl (%rbx),%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <dprintf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11f1 <write_serial+0xb1>
mov    $0x10,%edi
call   1040 <msleep@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    11a0 <write_serial+0x60>
mov    (%r12),%edi
lea    0xe32(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <dprintf@plt>
jmp    11fb <write_serial+0xbb>
mov    0x2de8(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDERR_FILENO = global i32 0
@.str = [25 x i8] c"Sending %d byte command\0A\00"
@.str.1 = [10 x i8] c"- %02hhx \00"
@errno = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define i32 @write_serial(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* @STDERR_FILENO
%12 = load i32, i32* %8
%13 = call i32 (i32, i8*, ...) @dprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %9
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %4
%17 = load i32, i32* %6
%18 = call i64 @wait_for_aa(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %5
br label %52
21: ; preds = %16, %4
store i32 0, i32* %10
br label %22
22: ; preds = %46, %21
%23 = load i32, i32* %10
%24 = load i32, i32* %8
%25 = icmp ult i32 %23, %24
br i1 %25, label %26, label %49
26: ; preds = %22
%27 = load i32, i32* @STDERR_FILENO
%28 = load i8*, i8** %7
%29 = load i32, i32* %10
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = call i32 (i32, i8*, ...) @dprintf(i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %6
%36 = load i8*, i8** %7
%37 = load i32, i32* %10
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = call i32 @write(i32 %35, i8* %39, i32 1)
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %44
42: ; preds = %26
%43 = load i32, i32* @errno
store i32 %43, i32* %5
br label %52
44: ; preds = %26
%45 = call i32 @msleep(i32 16)
br label %46
46: ; preds = %44
%47 = load i32, i32* %10
%48 = add i32 %47, 1
store i32 %48, i32* %10
br label %22
49: ; preds = %22
%50 = load i32, i32* @STDERR_FILENO
%51 = call i32 (i32, i8*, ...) @dprintf(i32 %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %5
br label %52
52: ; preds = %49, %42, %20
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
@.str = [25 x i8] c"Sending %d byte command\0A\00"
@.str.1 = [10 x i8] c"- %02hhx \00"
@errno = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define i32 @write_serial(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = load i32, i32* @STDERR_FILENO
%6 = tail call i32 (i32, i8*, ...) @dprintf(i32 %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %2)
%7 = icmp eq i32 %3, 0
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i64 @wait_for_aa(i32 %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %33
11: ; preds = %8, %4
%12 = icmp eq i32 %2, 0
br i1 %12, label %30, label %13
13: ; preds = %11
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %13, %24
%16 = phi i64 [ 0, %13 ], [ %26, %24 ]
%17 = load i32, i32* @STDERR_FILENO
%18 = getelementptr inbounds i8, i8* %1, i64 %16
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = tail call i32 (i32, i8*, ...) @dprintf(i32 %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = tail call i32 @write(i32 %0, i8* %18, i32 1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %15
%25 = tail call i32 @msleep(i32 16)
%26 = add nuw nsw i64 %16, 1
%27 = icmp eq i64 %26, %14
br i1 %27, label %30, label %15
28: ; preds = %15
%29 = load i32, i32* @errno
br label %33
30: ; preds = %24, %11
%31 = load i32, i32* @STDERR_FILENO
%32 = tail call i32 (i32, i8*, ...) @dprintf(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %28, %8, %30
%34 = phi i32 [ %29, %28 ], [ 0, %30 ], [ -1, %8 ]
ret i32 %34
}