#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*/ 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_answer_packet(unsigned char *data,
                                      unsigned char data_count) {
  unsigned char packet_size = 1 + 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;
  memcpy((packet + 1), data, data_count);
  calculate_crc(packet_size - 2, packet, crc);
  packet[1 + data_count] = crc[0];
  packet[1 + data_count + 1] = crc[1];
  return packet;
}


x86 assembly (clang, -O0):
<generate_answer_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
add    $0x1,%eax
add    $0x2,%eax
mov    %al,-0x12(%rbp)
mov    $0x1,%esi
movzbl -0x12(%rbp),%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1179 <generate_answer_packet+0x49>
movq   $0x0,-0x8(%rbp)
jmp    11e3 <generate_answer_packet+0xb3>
mov    -0x12(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x11(%rbp),%al
movzbl %al,%edx
call   1050 <memcpy@plt>
movzbl -0x12(%rbp),%eax
sub    $0x2,%eax
mov    -0x20(%rbp),%rsi
lea    -0x14(%rbp),%rdx
movzbl %al,%edi
call   1030 <calculate_crc@plt>
mov    -0x14(%rbp),%dl
mov    -0x20(%rbp),%rax
movzbl -0x11(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x13(%rbp),%dl
mov    -0x20(%rbp),%rax
movzbl -0x11(%rbp),%ecx
add    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_answer_packet>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x3(%r14),%ebp
movzbl %bpl,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     119c <generate_answer_packet+0x6c>
movzbl %r14b,%r12d
mov    %bpl,(%rbx)
mov    %rbx,%rdi
add    $0x1,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
add    $0x1,%r14b
movzbl %r14b,%edi
lea    0xe(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <calculate_crc@plt>
mov    0xe(%rsp),%al
mov    %al,0x1(%r12,%rbx,1)
mov    0xf(%rsp),%al
mov    %al,0x2(%r12,%rbx,1)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @generate_answer_packet(i8* %0, i8 zeroext %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8
%7 = alloca [2 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i8 %1, i8* %5
%9 = load i8, i8* %5
%10 = zext i8 %9 to i32
%11 = add nsw i32 1, %10
%12 = add nsw i32 %11, 2
%13 = trunc i32 %12 to i8
store i8 %13, i8* %6
%14 = load i8, i8* %6
%15 = call i8* @calloc(i8 zeroext %14, i32 1)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %2
store i8* null, i8** %3
br label %53
19: ; preds = %2
%20 = load i8, i8* %6
%21 = load i8*, i8** %8
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 %20, i8* %22
%23 = load i8*, i8** %8
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = load i8*, i8** %4
%26 = load i8, i8* %5
%27 = call i32 @memcpy(i8* %24, i8* %25, i8 zeroext %26)
%28 = load i8, i8* %6
%29 = zext i8 %28 to i32
%30 = sub nsw i32 %29, 2
%31 = trunc i32 %30 to i8
%32 = load i8*, i8** %8
%33 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%34 = call i32 @calculate_crc(i8 zeroext %31, i8* %32, i8* %33)
%35 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%36 = load i8, i8* %35
%37 = load i8*, i8** %8
%38 = load i8, i8* %5
%39 = zext i8 %38 to i32
%40 = add nsw i32 1, %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %37, i64 %41
store i8 %36, i8* %42
%43 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
%44 = load i8, i8* %43
%45 = load i8*, i8** %8
%46 = load i8, i8* %5
%47 = zext i8 %46 to i32
%48 = add nsw i32 1, %47
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %45, i64 %50
store i8 %44, i8* %51
%52 = load i8*, i8** %8
store i8* %52, i8** %3
br label %53
53: ; preds = %19, %18
%54 = load i8*, i8** %3
ret i8* %54
}

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