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

/* Variables and functions */
int /*<<< orphan*/ calculate_crc(unsigned char, unsigned char *,
                                 unsigned char *);
unsigned char *calloc(unsigned char, int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, unsigned char);
unsigned char *generate_command_packet(unsigned char opcode,
                                       unsigned char param1, uint16_t param2,
                                       unsigned char *data,
                                       unsigned char data_count) {
  unsigned char packet_size = 1 + 1 + 1 + 2 + data_count + 2;
  unsigned char crc[2];
  unsigned char *packet = calloc(packet_size, sizeof(unsigned char));
  if (packet == NULL) {
    return NULL;
  }
  packet[0] = packet_size;
  packet[1] = opcode;
  packet[2] = param1;
  packet[3] = (param2 & 0x00FF);
  packet[4] = ((param2 & 0xFF00) >> 8);
  memcpy((packet + 5), data, data_count);
  calculate_crc(packet_size - 2, packet, crc);
  packet[5 + data_count] = crc[0];
  packet[5 + data_count + 1] = crc[1];
  return packet;
}


x86 assembly (clang, -O0):
<generate_command_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %r8b,%al
mov    %dil,-0x9(%rbp)
mov    %sil,-0xa(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
add    $0x5,%eax
add    $0x2,%eax
mov    %al,-0x1a(%rbp)
mov    $0x1,%esi
movzbl -0x1a(%rbp),%edi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1184 <generate_command_packet+0x54>
movq   $0x0,-0x8(%rbp)
jmp    1227 <generate_command_packet+0xf7>
mov    -0x1a(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x9(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0xa(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x10(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x10(%rbp),%eax
and    $0xff00,%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,0x4(%rax)
mov    -0x28(%rbp),%rdi
add    $0x5,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x19(%rbp),%al
movzbl %al,%edx
call   1050 <memcpy@plt>
movzbl -0x1a(%rbp),%eax
sub    $0x2,%eax
mov    -0x28(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
movzbl %al,%edi
call   1030 <calculate_crc@plt>
mov    -0x1c(%rbp),%dl
mov    -0x28(%rbp),%rax
movzbl -0x19(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1b(%rbp),%dl
mov    -0x28(%rbp),%rax
movzbl -0x19(%rbp),%ecx
add    $0x5,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_command_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebx
mov    %rcx,%r13
mov    %edx,0x4(%rsp)
mov    %esi,%r15d
mov    %edi,%r12d
lea    0x7(%rbx),%r14d
movzbl %r14b,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11b6 <generate_command_packet+0x86>
mov    %r13,%rsi
movzbl %bl,%r13d
mov    %r14b,0x0(%rbp)
mov    %r12b,0x1(%rbp)
mov    %r15b,0x2(%rbp)
mov    0x4(%rsp),%eax
mov    %al,0x3(%rbp)
mov    %ah,0x4(%rbp)
mov    %rbp,%rdi
add    $0x5,%rdi
mov    %r13d,%edx
call   1050 <memcpy@plt>
add    $0x5,%bl
movzbl %bl,%edi
lea    0x2(%rsp),%rdx
mov    %rbp,%rsi
call   1030 <calculate_crc@plt>
mov    0x2(%rsp),%al
mov    %al,0x5(%r13,%rbp,1)
mov    0x3(%rsp),%al
mov    %al,0x6(%r13,%rbp,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @generate_command_packet(i8 zeroext %0, i8 zeroext %1, i32 %2, i8* %3, i8 zeroext %4) {
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8
%12 = alloca i8
%13 = alloca [2 x i8]
%14 = alloca i8*
store i8 %0, i8* %7
store i8 %1, i8* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i8 %4, i8* %11
%15 = load i8, i8* %11
%16 = zext i8 %15 to i32
%17 = add nsw i32 5, %16
%18 = add nsw i32 %17, 2
%19 = trunc i32 %18 to i8
store i8 %19, i8* %12
%20 = load i8, i8* %12
%21 = call i8* @calloc(i8 zeroext %20, i32 1)
store i8* %21, i8** %14
%22 = load i8*, i8** %14
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %5
store i8* null, i8** %6
br label %76
25: ; preds = %5
%26 = load i8, i8* %12
%27 = load i8*, i8** %14
%28 = getelementptr inbounds i8, i8* %27, i64 0
store i8 %26, i8* %28
%29 = load i8, i8* %7
%30 = load i8*, i8** %14
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8 %29, i8* %31
%32 = load i8, i8* %8
%33 = load i8*, i8** %14
%34 = getelementptr inbounds i8, i8* %33, i64 2
store i8 %32, i8* %34
%35 = load i32, i32* %9
%36 = and i32 %35, 255
%37 = trunc i32 %36 to i8
%38 = load i8*, i8** %14
%39 = getelementptr inbounds i8, i8* %38, i64 3
store i8 %37, i8* %39
%40 = load i32, i32* %9
%41 = and i32 %40, 65280
%42 = ashr i32 %41, 8
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %14
%45 = getelementptr inbounds i8, i8* %44, i64 4
store i8 %43, i8* %45
%46 = load i8*, i8** %14
%47 = getelementptr inbounds i8, i8* %46, i64 5
%48 = load i8*, i8** %10
%49 = load i8, i8* %11
%50 = call i32 @memcpy(i8* %47, i8* %48, i8 zeroext %49)
%51 = load i8, i8* %12
%52 = zext i8 %51 to i32
%53 = sub nsw i32 %52, 2
%54 = trunc i32 %53 to i8
%55 = load i8*, i8** %14
%56 = getelementptr inbounds [2 x i8], [2 x i8]* %13, i64 0, i64 0
%57 = call i32 @calculate_crc(i8 zeroext %54, i8* %55, i8* %56)
%58 = getelementptr inbounds [2 x i8], [2 x i8]* %13, i64 0, i64 0
%59 = load i8, i8* %58
%60 = load i8*, i8** %14
%61 = load i8, i8* %11
%62 = zext i8 %61 to i32
%63 = add nsw i32 5, %62
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %60, i64 %64
store i8 %59, i8* %65
%66 = getelementptr inbounds [2 x i8], [2 x i8]* %13, i64 0, i64 1
%67 = load i8, i8* %66
%68 = load i8*, i8** %14
%69 = load i8, i8* %11
%70 = zext i8 %69 to i32
%71 = add nsw i32 5, %70
%72 = add nsw i32 %71, 1
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %68, i64 %73
store i8 %67, i8* %74
%75 = load i8*, i8** %14
store i8* %75, i8** %6
br label %76
76: ; preds = %25, %24
%77 = load i8*, i8** %6
ret i8* %77
}

LLVM IR (-O2):
define i8* @generate_command_packet(i8 zeroext %0, i8 zeroext %1, i32 %2, i8* %3, i8 zeroext %4) {
%6 = alloca [2 x i8]
%7 = zext i8 %4 to i32
%8 = add i8 %4, 7
%9 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%10 = tail call i8* @calloc(i8 zeroext %8, i32 1)
%11 = icmp eq i8* %10, null
br i1 %11, label %33, label %12
12: ; preds = %5
%13 = add nuw nsw i32 %7, 5
store i8 %8, i8* %10
%14 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %0, i8* %14
%15 = getelementptr inbounds i8, i8* %10, i64 2
store i8 %1, i8* %15
%16 = trunc i32 %2 to i8
%17 = getelementptr inbounds i8, i8* %10, i64 3
store i8 %16, i8* %17
%18 = lshr i32 %2, 8
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds i8, i8* %10, i64 4
store i8 %19, i8* %20
%21 = getelementptr inbounds i8, i8* %10, i64 5
%22 = tail call i32 @memcpy(i8* %21, i8* %3, i8 zeroext %4)
%23 = add i8 %4, 5
%24 = call i32 @calculate_crc(i8 zeroext %23, i8* %10, i8* %9)
%25 = load i8, i8* %9
%26 = zext i32 %13 to i64
%27 = getelementptr inbounds i8, i8* %10, i64 %26
store i8 %25, i8* %27
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 1
%29 = load i8, i8* %28
%30 = add nuw nsw i32 %7, 6
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %10, i64 %31
store i8 %29, i8* %32
br label %33
33: ; preds = %5, %12
ret i8* %10
}