#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*/ printf(char *);
int /*<<< orphan*/ usleep(int);
short write(int, unsigned char *, unsigned short);
short send_data(int fComPort, unsigned char *pData, unsigned short nLen) {
  unsigned char nCount = 0;
  short nTmpLen = 0;
  short nResult = 0;
  while (nResult < nLen) {
    nResult = write(fComPort, pData + nTmpLen, nLen - nTmpLen);
    if (nResult <= 0) {
      if (nResult == -1) {
        printf("Comm Send Eror\r\n");
        break;
      }
      if (nCount++ > 10) {
        printf("Send Time Out\r\n");
        break;
      }
      usleep(10000);
    }
    nTmpLen += nResult;
  }
  return nResult;
}


x86 assembly (clang, -O0):
<send_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movb   $0x0,-0x13(%rbp)
movw   $0x0,-0x16(%rbp)
movw   $0x0,-0x18(%rbp)
movswl -0x18(%rbp),%eax
movzwl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jge    11fc <send_data+0xcc>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movswl -0x16(%rbp),%eax
cltq
add    %rax,%rsi
movzwl -0x12(%rbp),%eax
movswl -0x16(%rbp),%ecx
sub    %ecx,%eax
movzwl %ax,%edx
call   1030 <write@plt>
mov    %ax,-0x18(%rbp)
movswl -0x18(%rbp),%eax
cmp    $0x0,%eax
jg     11e9 <send_data+0xb9>
movswl -0x18(%rbp),%eax
cmp    $0xffffffff,%eax
jne    11b7 <send_data+0x87>
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
jmp    11fc <send_data+0xcc>
mov    -0x13(%rbp),%al
mov    %al,%cl
add    $0x1,%cl
mov    %cl,-0x13(%rbp)
movzbl %al,%eax
cmp    $0xa,%eax
jle    11df <send_data+0xaf>
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
jmp    11fc <send_data+0xcc>
mov    $0x2710,%edi
call   1050 <usleep@plt>
movswl -0x18(%rbp),%ecx
movswl -0x16(%rbp),%eax
add    %ecx,%eax
mov    %ax,-0x16(%rbp)
jmp    1156 <send_data+0x26>
movswl -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %edx,%edx
je     1199 <send_data+0x69>
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r12d
xor    %r13d,%r13d
jmp    115d <send_data+0x2d>
xchg   %ax,%ax
movswl %bx,%ecx
add    %ecx,%r14d
mov    %r14d,%eax
cmp    %ebp,%ecx
jge    11b6 <send_data+0x86>
movswl %ax,%r14d
movslq %r14d,%rsi
add    %r15,%rsi
mov    %ebp,%ecx
sub    %eax,%ecx
movzwl %cx,%edx
mov    %r12d,%edi
call   1040 <write@plt>
mov    %eax,%ebx
test   %ax,%ax
jg     1150 <send_data+0x20>
cmp    $0xffff,%bx
je     119d <send_data+0x6d>
cmp    $0xa,%r13b
ja     11aa <send_data+0x7a>
add    $0x1,%r13b
mov    $0x2710,%edi
call   1050 <usleep@plt>
jmp    1150 <send_data+0x20>
xor    %ebx,%ebx
jmp    11b6 <send_data+0x86>
mov    $0xffff,%bx
lea    0xe67(%rip),%rdi
jmp    11b1 <send_data+0x81>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Comm Send Eror\0D\0A\00"
@.str.1 = [16 x i8] c"Send Time Out\0D\0A\00"
define signext i16 @send_data(i32 %0, i8* %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i8
%8 = alloca i16
%9 = alloca i16
store i32 %0, i32* %4
store i8* %1, i8** %5
store i16 %2, i16* %6
store i8 0, i8* %7
store i16 0, i16* %8
store i16 0, i16* %9
br label %10
10: ; preds = %48, %3
%11 = load i16, i16* %9
%12 = sext i16 %11 to i32
%13 = load i16, i16* %6
%14 = zext i16 %13 to i32
%15 = icmp slt i32 %12, %14
br i1 %15, label %16, label %55
16: ; preds = %10
%17 = load i32, i32* %4
%18 = load i8*, i8** %5
%19 = load i16, i16* %8
%20 = sext i16 %19 to i32
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = load i16, i16* %6
%24 = zext i16 %23 to i32
%25 = load i16, i16* %8
%26 = sext i16 %25 to i32
%27 = sub nsw i32 %24, %26
%28 = trunc i32 %27 to i16
%29 = call signext i16 @write(i32 %17, i8* %22, i16 zeroext %28)
store i16 %29, i16* %9
%30 = load i16, i16* %9
%31 = sext i16 %30 to i32
%32 = icmp sle i32 %31, 0
br i1 %32, label %33, label %48
33: ; preds = %16
%34 = load i16, i16* %9
%35 = sext i16 %34 to i32
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %55
39: ; preds = %33
%40 = load i8, i8* %7
%41 = add i8 %40, 1
store i8 %41, i8* %7
%42 = zext i8 %40 to i32
%43 = icmp sgt i32 %42, 10
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %55
46: ; preds = %39
%47 = call i32 @usleep(i32 10000)
br label %48
48: ; preds = %46, %16
%49 = load i16, i16* %9
%50 = sext i16 %49 to i32
%51 = load i16, i16* %8
%52 = sext i16 %51 to i32
%53 = add nsw i32 %52, %50
%54 = trunc i32 %53 to i16
store i16 %54, i16* %8
br label %10
55: ; preds = %44, %37, %10
%56 = load i16, i16* %9
ret i16 %56
}

LLVM IR (-O2):
@str = [15 x i8] c"Send Time Out\0D\00"
@str.2 = [16 x i8] c"Comm Send Eror\0D\00"
define signext i16 @send_data(i32 %0, i8* nocapture %1, i16 zeroext %2) {
%4 = zext i16 %2 to i32
%5 = icmp eq i16 %2, 0
br i1 %5, label %33, label %6
6: ; preds = %3, %25
%7 = phi i32 [ %27, %25 ], [ 0, %3 ]
%8 = phi i8 [ %26, %25 ], [ 0, %3 ]
%9 = shl i32 %7, 16
%10 = ashr exact i32 %9, 16
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = trunc i32 %7 to i16
%14 = sub i16 %2, %13
%15 = tail call signext i16 @write(i32 %0, i8* %12, i16 zeroext %14)
%16 = sext i16 %15 to i32
%17 = icmp slt i16 %15, 1
br i1 %17, label %18, label %25
18: ; preds = %6
%19 = icmp eq i16 %15, -1
br i1 %19, label %29, label %20
20: ; preds = %18
%21 = icmp ugt i8 %8, 10
br i1 %21, label %29, label %22
22: ; preds = %20
%23 = add nuw nsw i8 %8, 1
%24 = tail call i32 @usleep(i32 10000)
br label %25
25: ; preds = %22, %6
%26 = phi i8 [ %23, %22 ], [ %8, %6 ]
%27 = add nsw i32 %10, %16
%28 = icmp slt i32 %16, %4
br i1 %28, label %6, label %33
29: ; preds = %20, %18
%30 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.2, i64 0, i64 0), %18 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0), %20 ]
%31 = phi i16 [ -1, %18 ], [ %15, %20 ]
%32 = tail call i32 @puts(i8* dereferenceable(1) %30)
br label %33
33: ; preds = %25, %29, %3
%34 = phi i16 [ 0, %3 ], [ %31, %29 ], [ %15, %25 ]
ret i16 %34
}