#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char const *, size_t);
int serial_write(int fd, const char *data, size_t size) {
  int written_size;
  size_t i;
  printf("Writing: ");
  for (i = 0; i < size; i++) {
    printf("0x%02x ", (unsigned char)data[i]);
  }
  printf("\n");
  written_size = write(fd, data, size);
  if (written_size != size) {
    fprintf(stderr, "Could not send serial data serial port: %s\n",
            strerror(errno));
  }
  return written_size;
}


x86 assembly (clang, -O0):
<serial_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11a2 <serial_write+0x62>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1169 <serial_write+0x29>
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11fd <serial_write+0xbd>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0xe98(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14,%r14
je     1197 <serial_write+0x47>
lea    0xe8f(%rip),%r12
xor    %ebp,%ebp
nopl   (%rax)
movzbl (%rbx,%rbp,1),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1180 <serial_write+0x30>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
mov    %eax,%ebx
cltq
cmp    %r14,%rax
je     11e0 <serial_write+0x90>
mov    0x2e21(%rip),%rax
mov    (%rax),%ebp
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe3c(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Writing: \00"
@.str.1 = [8 x i8] c"0x%02x \00"
@.str.2 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.3 = [44 x i8] c"Could not send serial data serial port: %s\0A\00"
@errno = global i32 0
define i32 @serial_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %8
br label %10
10: ; preds = %21, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %24
14: ; preds = %10
%15 = load i8*, i8** %5
%16 = load i64, i64* %8
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %14
%22 = load i64, i64* %8
%23 = add i64 %22, 1
store i64 %23, i64* %8
br label %10
24: ; preds = %10
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %4
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
%29 = call i32 @write(i32 %26, i8* %27, i64 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = load i64, i64* %6
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %24
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %34, %24
%40 = load i32, i32* %7
ret i32 %40
}

LLVM IR (-O2):
@.str = [10 x i8] c"Writing: \00"
@.str.1 = [8 x i8] c"0x%02x \00"
@stderr = global i32 0
@.str.3 = [44 x i8] c"Could not send serial data serial port: %s\0A\00"
@errno = global i32 0
define i32 @serial_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %2, 0
br i1 %5, label %14, label %6
6: ; preds = %3, %6
%7 = phi i64 [ %12, %6 ], [ 0, %3 ]
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = add nuw i64 %7, 1
%13 = icmp eq i64 %12, %2
br i1 %13, label %14, label %6
14: ; preds = %6, %3
%15 = tail call i32 @putchar(i32 10)
%16 = tail call i32 @write(i32 %0, i8* %1, i64 %2)
%17 = sext i32 %16 to i64
%18 = icmp eq i64 %17, %2
br i1 %18, label %24, label %19
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %19, %14
ret i32 %16
}