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

/* Variables and functions */
int /*<<< orphan*/ SpiWrite(unsigned char *, unsigned short);
unsigned char *UINT16_TO_STREAM_f(unsigned char *, int);
int /*<<< orphan*/ memcpy(unsigned char *, char *, unsigned short);
void hci_patch_send(unsigned char ucOpcode, unsigned char *pucBuff, char *patch,
                    unsigned short usDataLength) {
  unsigned char *data_ptr = (pucBuff + (5));
  unsigned short usTransLength;
  unsigned char *stream = (pucBuff + (5));
  { *(stream)++ = (0x3); };
  { *(stream)++ = (ucOpcode); };
  stream = (UINT16_TO_STREAM_f(stream, usDataLength + (2)));
  if (usDataLength <= (1000)) {
    (UINT16_TO_STREAM_f(stream, usDataLength));
    stream = (UINT16_TO_STREAM_f(stream, usDataLength));
    memcpy((pucBuff + (5)) + (6), patch, usDataLength);
    SpiWrite(pucBuff, usDataLength + (6));
  } else {
    usTransLength = (usDataLength / (1000));
    (UINT16_TO_STREAM_f(stream, usDataLength + (2) + usTransLength * (2)));
    stream = (UINT16_TO_STREAM_f(stream, (1000)));
    memcpy(pucBuff + (5) + (6), patch, (1000));
    usDataLength -= (1000);
    patch += (1000);
    SpiWrite(pucBuff, (1000) + (6));
    while (usDataLength) {
      if (usDataLength <= (1000)) {
        usTransLength = usDataLength;
        usDataLength = 0;
      } else {
        usTransLength = (1000);
        usDataLength -= usTransLength;
      }
      *(unsigned short *)data_ptr = usTransLength;
      memcpy(data_ptr + (2), patch, usTransLength);
      patch += usTransLength;
      SpiWrite((unsigned char *)data_ptr,
               usTransLength + sizeof(usTransLength));
    }
  }
}


x86 assembly (clang, -O0):
<hci_patch_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %cx,%ax
mov    %dil,%cl
mov    %cl,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ax,-0x1a(%rbp)
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x3,(%rax)
mov    -0x1(%rbp),%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
mov    -0x38(%rbp),%rdi
movzwl -0x1a(%rbp),%esi
add    $0x2,%esi
call   1030 <UINT16_TO_STREAM_f@plt>
mov    %rax,-0x38(%rbp)
movzwl -0x1a(%rbp),%eax
cmp    $0x3e8,%eax
jg     1200 <hci_patch_send+0xd0>
mov    -0x38(%rbp),%rdi
movzwl -0x1a(%rbp),%esi
call   1030 <UINT16_TO_STREAM_f@plt>
mov    -0x38(%rbp),%rdi
movzwl -0x1a(%rbp),%esi
call   1030 <UINT16_TO_STREAM_f@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x5,%rdi
add    $0x6,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1a(%rbp),%ax
movzwl %ax,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
movzwl -0x1a(%rbp),%eax
add    $0x6,%eax
movzwl %ax,%esi
call   1050 <SpiWrite@plt>
jmp    1312 <hci_patch_send+0x1e2>
movzwl -0x1a(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %ax,-0x2a(%rbp)
mov    -0x38(%rbp),%rdi
movzwl -0x1a(%rbp),%esi
add    $0x2,%esi
movzwl -0x2a(%rbp),%eax
shl    $0x1,%eax
add    %eax,%esi
call   1030 <UINT16_TO_STREAM_f@plt>
mov    -0x38(%rbp),%rdi
mov    $0x3e8,%esi
call   1030 <UINT16_TO_STREAM_f@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x5,%rdi
add    $0x6,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x3e8,%edx
call   1040 <memcpy@plt>
movzwl -0x1a(%rbp),%eax
sub    $0x3e8,%eax
mov    %ax,-0x1a(%rbp)
mov    -0x18(%rbp),%rax
add    $0x3e8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3ee,%esi
call   1050 <SpiWrite@plt>
cmpw   $0x0,-0x1a(%rbp)
je     130d <hci_patch_send+0x1dd>
movzwl -0x1a(%rbp),%eax
cmp    $0x3e8,%eax
jg     12ab <hci_patch_send+0x17b>
mov    -0x1a(%rbp),%ax
mov    %ax,-0x2a(%rbp)
movw   $0x0,-0x1a(%rbp)
jmp    12bf <hci_patch_send+0x18f>
movw   $0x3e8,-0x2a(%rbp)
movzwl -0x2a(%rbp),%ecx
movzwl -0x1a(%rbp),%eax
sub    %ecx,%eax
mov    %ax,-0x1a(%rbp)
mov    -0x2a(%rbp),%cx
mov    -0x28(%rbp),%rax
mov    %cx,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x2,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2a(%rbp),%ax
movzwl %ax,%edx
call   1040 <memcpy@plt>
movzwl -0x2a(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
movzwl -0x2a(%rbp),%eax
add    $0x2,%rax
movzwl %ax,%esi
call   1050 <SpiWrite@plt>
jmp    127e <hci_patch_send+0x14e>
jmp    1312 <hci_patch_send+0x1e2>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hci_patch_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %rdx,%r13
mov    %rsi,%r12
movb   $0x3,0x5(%rsi)
lea    0x7(%rsi),%r14
mov    %dil,0x6(%rsi)
lea    0x2(%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <UINT16_TO_STREAM_f@plt>
cmp    $0x3e8,%ebx
ja     11a7 <hci_patch_send+0x77>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %ebx,%esi
call   1030 <UINT16_TO_STREAM_f@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <UINT16_TO_STREAM_f@plt>
lea    0xb(%r12),%rdi
movzwl %bx,%edx
mov    %r13,%rsi
call   1040 <memcpy@plt>
lea    0x6(%rbx),%eax
movzwl %ax,%esi
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <SpiWrite@plt>
mov    %rbp,%rdx
lea    0x5(%r12),%r15
movzwl %bx,%ebp
mov    %ebp,%ecx
shr    $0x3,%ecx
imul   $0x20c5,%ecx,%ecx
shr    $0x14,%ecx
lea    (%rdx,%rcx,2),%esi
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <UINT16_TO_STREAM_f@plt>
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   1030 <UINT16_TO_STREAM_f@plt>
lea    0xb(%r12),%rdi
mov    %r13,%rsi
mov    $0x3e8,%edx
call   1040 <memcpy@plt>
add    $0xfffffc18,%ebp
mov    %r12,%rdi
mov    $0x3ee,%esi
call   1050 <SpiWrite@plt>
add    $0x3e8,%r13
nopw   0x0(%rax,%rax,1)
movzwl %bp,%eax
lea    -0x3e8(%rax),%ebp
cmp    $0x3e9,%eax
mov    $0x3e8,%ebx
cmovb  %eax,%ebx
mov    %bx,(%r15)
movzwl %bx,%edx
movzwl %bx,%r12d
mov    $0x0,%eax
cmovb  %eax,%ebp
mov    %r14,%rdi
mov    %r13,%rsi
call   1040 <memcpy@plt>
add    %r12,%r13
add    $0x2,%ebx
movzwl %bx,%esi
mov    %r15,%rdi
call   1050 <SpiWrite@plt>
test   %bp,%bp
jne    1210 <hci_patch_send+0xe0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @hci_patch_send(i8 zeroext %0, i8* %1, i8* %2, i16 zeroext %3) {
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i16
%9 = alloca i8*
%10 = alloca i16
%11 = alloca i8*
store i8 %0, i8* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i16 %3, i16* %8
%12 = load i8*, i8** %6
%13 = getelementptr inbounds i8, i8* %12, i64 5
store i8* %13, i8** %9
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 5
store i8* %15, i8** %11
%16 = load i8*, i8** %11
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %11
store i8 3, i8* %16
%18 = load i8, i8* %5
%19 = load i8*, i8** %11
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %11
store i8 %18, i8* %19
%21 = load i8*, i8** %11
%22 = load i16, i16* %8
%23 = zext i16 %22 to i32
%24 = add nsw i32 %23, 2
%25 = call i8* @UINT16_TO_STREAM_f(i8* %21, i32 %24)
store i8* %25, i8** %11
%26 = load i16, i16* %8
%27 = zext i16 %26 to i32
%28 = icmp sle i32 %27, 1000
br i1 %28, label %29, label %50
29: ; preds = %4
%30 = load i8*, i8** %11
%31 = load i16, i16* %8
%32 = zext i16 %31 to i32
%33 = call i8* @UINT16_TO_STREAM_f(i8* %30, i32 %32)
%34 = load i8*, i8** %11
%35 = load i16, i16* %8
%36 = zext i16 %35 to i32
%37 = call i8* @UINT16_TO_STREAM_f(i8* %34, i32 %36)
store i8* %37, i8** %11
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i64 5
%40 = getelementptr inbounds i8, i8* %39, i64 6
%41 = load i8*, i8** %7
%42 = load i16, i16* %8
%43 = call i32 @memcpy(i8* %40, i8* %41, i16 zeroext %42)
%44 = load i8*, i8** %6
%45 = load i16, i16* %8
%46 = zext i16 %45 to i32
%47 = add nsw i32 %46, 6
%48 = trunc i32 %47 to i16
%49 = call i32 @SpiWrite(i8* %44, i16 zeroext %48)
br label %116
50: ; preds = %4
%51 = load i16, i16* %8
%52 = zext i16 %51 to i32
%53 = sdiv i32 %52, 1000
%54 = trunc i32 %53 to i16
store i16 %54, i16* %10
%55 = load i8*, i8** %11
%56 = load i16, i16* %8
%57 = zext i16 %56 to i32
%58 = add nsw i32 %57, 2
%59 = load i16, i16* %10
%60 = zext i16 %59 to i32
%61 = mul nsw i32 %60, 2
%62 = add nsw i32 %58, %61
%63 = call i8* @UINT16_TO_STREAM_f(i8* %55, i32 %62)
%64 = load i8*, i8** %11
%65 = call i8* @UINT16_TO_STREAM_f(i8* %64, i32 1000)
store i8* %65, i8** %11
%66 = load i8*, i8** %6
%67 = getelementptr inbounds i8, i8* %66, i64 5
%68 = getelementptr inbounds i8, i8* %67, i64 6
%69 = load i8*, i8** %7
%70 = call i32 @memcpy(i8* %68, i8* %69, i16 zeroext 1000)
%71 = load i16, i16* %8
%72 = zext i16 %71 to i32
%73 = sub nsw i32 %72, 1000
%74 = trunc i32 %73 to i16
store i16 %74, i16* %8
%75 = load i8*, i8** %7
%76 = getelementptr inbounds i8, i8* %75, i64 1000
store i8* %76, i8** %7
%77 = load i8*, i8** %6
%78 = call i32 @SpiWrite(i8* %77, i16 zeroext 1006)
br label %79
79: ; preds = %95, %50
%80 = load i16, i16* %8
%81 = icmp ne i16 %80, 0
br i1 %81, label %82, label %115
82: ; preds = %79
%83 = load i16, i16* %8
%84 = zext i16 %83 to i32
%85 = icmp sle i32 %84, 1000
br i1 %85, label %86, label %88
86: ; preds = %82
%87 = load i16, i16* %8
store i16 %87, i16* %10
store i16 0, i16* %8
br label %95
88: ; preds = %82
store i16 1000, i16* %10
%89 = load i16, i16* %10
%90 = zext i16 %89 to i32
%91 = load i16, i16* %8
%92 = zext i16 %91 to i32
%93 = sub nsw i32 %92, %90
%94 = trunc i32 %93 to i16
store i16 %94, i16* %8
br label %95
95: ; preds = %88, %86
%96 = load i16, i16* %10
%97 = load i8*, i8** %9
%98 = bitcast i8* %97 to i16*
store i16 %96, i16* %98
%99 = load i8*, i8** %9
%100 = getelementptr inbounds i8, i8* %99, i64 2
%101 = load i8*, i8** %7
%102 = load i16, i16* %10
%103 = call i32 @memcpy(i8* %100, i8* %101, i16 zeroext %102)
%104 = load i16, i16* %10
%105 = zext i16 %104 to i32
%106 = load i8*, i8** %7
%107 = sext i32 %105 to i64
%108 = getelementptr inbounds i8, i8* %106, i64 %107
store i8* %108, i8** %7
%109 = load i8*, i8** %9
%110 = load i16, i16* %10
%111 = zext i16 %110 to i64
%112 = add i64 %111, 2
%113 = trunc i64 %112 to i16
%114 = call i32 @SpiWrite(i8* %109, i16 zeroext %113)
br label %79
115: ; preds = %79
br label %116
116: ; preds = %115, %29
ret void
}

LLVM IR (-O2):
define void @hci_patch_send(i8 zeroext %0, i8* %1, i8* %2, i16 zeroext %3) {
%5 = getelementptr inbounds i8, i8* %1, i64 5
%6 = getelementptr inbounds i8, i8* %1, i64 6
store i8 3, i8* %5
%7 = getelementptr inbounds i8, i8* %1, i64 7
store i8 %0, i8* %6
%8 = zext i16 %3 to i32
%9 = add nuw nsw i32 %8, 2
%10 = tail call i8* @UINT16_TO_STREAM_f(i8* %7, i32 %9)
%11 = icmp ult i16 %3, 1001
br i1 %11, label %12, label %19
12: ; preds = %4
%13 = tail call i8* @UINT16_TO_STREAM_f(i8* %10, i32 %8)
%14 = tail call i8* @UINT16_TO_STREAM_f(i8* %10, i32 %8)
%15 = getelementptr inbounds i8, i8* %1, i64 11
%16 = tail call i32 @memcpy(i8* %15, i8* %2, i16 zeroext %3)
%17 = add nuw nsw i16 %3, 6
%18 = tail call i32 @SpiWrite(i8* %1, i16 zeroext %17)
br label %45
19: ; preds = %4
%20 = udiv i16 %3, 1000
%21 = shl nuw nsw i16 %20, 1
%22 = zext i16 %21 to i32
%23 = add nuw nsw i32 %9, %22
%24 = tail call i8* @UINT16_TO_STREAM_f(i8* %10, i32 %23)
%25 = tail call i8* @UINT16_TO_STREAM_f(i8* %10, i32 1000)
%26 = getelementptr inbounds i8, i8* %1, i64 11
%27 = tail call i32 @memcpy(i8* %26, i8* %2, i16 zeroext 1000)
%28 = add i16 %3, -1000
%29 = tail call i32 @SpiWrite(i8* %1, i16 zeroext 1006)
%30 = getelementptr inbounds i8, i8* %2, i64 1000
%31 = bitcast i8* %5 to i16*
br label %32
32: ; preds = %19, %32
%33 = phi i8* [ %30, %19 ], [ %40, %32 ]
%34 = phi i16 [ %28, %19 ], [ %43, %32 ]
%35 = icmp ult i16 %34, 1001
%36 = add i16 %34, -1000
%37 = select i1 %35, i16 %34, i16 1000
store i16 %37, i16* %31
%38 = tail call i32 @memcpy(i8* %7, i8* %33, i16 zeroext %37)
%39 = zext i16 %37 to i64
%40 = getelementptr inbounds i8, i8* %33, i64 %39
%41 = add nuw nsw i16 %37, 2
%42 = tail call i32 @SpiWrite(i8* %5, i16 zeroext %41)
%43 = select i1 %35, i16 0, i16 %36
%44 = icmp eq i16 %43, 0
br i1 %44, label %45, label %32
45: ; preds = %32, %12
ret void
}