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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int write(int, int *, int);
int write_to_serial_device(int device_file, uint8_t buffer[], int length) {
  int i = 0;
  int count = 0;
  printf("Writing: ");
  while (count < length) {
    if (write(device_file, &buffer[count], 1) == 1) {
      printf("0x%x, ", buffer[count]);
      count++;
    } else {
      printf("\n Can't Write Byte\n");
      break;
    }
  }
  if (count == length) {
    printf("\nWrite Successful\n");
    return count;
  } else {
    printf("\nCan't Write All Frame to Serial Device\n");
    return -1;
  }
}


x86 assembly (clang, -O0):
<write_to_serial_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11be <write_to_serial_device+0x9e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11a6 <write_to_serial_device+0x86>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b9 <write_to_serial_device+0x99>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11be <write_to_serial_device+0x9e>
jmp    114e <write_to_serial_device+0x2e>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11e3 <write_to_serial_device+0xc3>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f8 <write_to_serial_device+0xd8>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_serial_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r15d
lea    0xeb5(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14d,%r14d
jle    11b9 <write_to_serial_device+0x89>
mov    %r14d,%r13d
lea    0xea7(%rip),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    11ab <write_to_serial_device+0x7b>
mov    0x0(%rbp),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffffffffffff,%rbx
add    $0x4,%rbp
mov    %r13,%rax
add    %rbx,%rax
jne    1170 <write_to_serial_device+0x40>
lea    0xe90(%rip),%rdi
jmp    11d8 <write_to_serial_device+0xa8>
neg    %ebx
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
cmp    %r14d,%ebx
lea    0xe76(%rip),%rax
lea    0xe47(%rip),%rdi
cmove  %rax,%rdi
mov    $0xffffffff,%r13d
cmove  %r14d,%r13d
call   1030 <puts@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Writing: \00"
@.str.1 = [7 x i8] c"0x%x, \00"
@.str.2 = [20 x i8] c"\0A Can't Write Byte\0A\00"
@.str.3 = [19 x i8] c"\0AWrite Successful\0A\00"
@.str.4 = [41 x i8] c"\0ACan't Write All Frame to Serial Device\0A\00"
define i32 @write_to_serial_device(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %34, %3
%12 = load i32, i32* %9
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %35
15: ; preds = %11
%16 = load i32, i32* %5
%17 = load i32*, i32** %6
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = call i32 @write(i32 %16, i32* %20, i32 1)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %32
23: ; preds = %15
%24 = load i32*, i32** %6
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %34
32: ; preds = %15
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %35
34: ; preds = %23
br label %11
35: ; preds = %32, %11
%36 = load i32, i32* %9
%37 = load i32, i32* %7
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* %9
store i32 %41, i32* %4
br label %44
42: ; preds = %35
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
44: ; preds = %42, %39
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@.str = [10 x i8] c"Writing: \00"
@.str.1 = [7 x i8] c"0x%x, \00"
@str = [40 x i8] c"\0ACan't Write All Frame to Serial Device\00"
@str.5 = [18 x i8] c"\0AWrite Successful\00"
@str.6 = [19 x i8] c"\0A Can't Write Byte\00"
define i32 @write_to_serial_device(i32 %0, i32* nocapture %1, i32 %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 sgt i32 %2, 0
br i1 %5, label %6, label %21
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %13
%9 = phi i64 [ 0, %6 ], [ %16, %13 ]
%10 = getelementptr inbounds i32, i32* %1, i64 %9
%11 = tail call i32 @write(i32 %0, i32* %10, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i32, i32* %10
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = add nuw nsw i64 %9, 1
%17 = icmp eq i64 %16, %7
br i1 %17, label %26, label %8
18: ; preds = %8
%19 = trunc i64 %9 to i32
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %21
21: ; preds = %3, %18
%22 = phi i32 [ %19, %18 ], [ 0, %3 ]
%23 = icmp eq i32 %22, %2
%24 = select i1 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @str.5, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0)
%25 = select i1 %23, i32 %2, i32 -1
br label %26
26: ; preds = %13, %21
%27 = phi i8* [ %24, %21 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @str.5, i64 0, i64 0), %13 ]
%28 = phi i32 [ %25, %21 ], [ %2, %13 ]
%29 = tail call i32 @puts(i8* dereferenceable(1) %27)
ret i32 %28
}