#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 char uint8_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ SETLEN(char *, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ set_seq_num(char *, int /*<<< orphan*/);
char *create_packet(uint8_t command, uint16_t sequence, uint16_t payloadSize,
                    char *payload) {
  char *buffer = malloc(sizeof(char) * (payloadSize + 5));
  buffer[0] = command;
  set_seq_num(buffer, sequence);
  SETLEN(buffer, payloadSize);
  if (payload != NULL) {
    memcpy(buffer + 5, payload, payloadSize);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<create_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x5,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x1(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1060 <set_seq_num@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <SETLEN@plt>
cmpq   $0x0,-0x18(%rbp)
je     11af <create_packet+0x6f>
mov    -0x20(%rbp),%rdi
add    $0x5,%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_packet>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%r12d
mov    %edi,%ebp
lea    0x5(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %bpl,(%rax)
mov    %rax,%rdi
mov    %r12d,%esi
call   1060 <set_seq_num@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <SETLEN@plt>
test   %r14,%r14
je     118f <create_packet+0x4f>
mov    %rbx,%rdi
add    $0x5,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @create_packet(i8 signext %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8 %0, i8* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%10 = load i32, i32* %7
%11 = add nsw i32 %10, 5
%12 = sext i32 %11 to i64
%13 = mul i64 1, %12
%14 = trunc i64 %13 to i32
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %9
%16 = load i8, i8* %5
%17 = load i8*, i8** %9
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 %16, i8* %18
%19 = load i8*, i8** %9
%20 = load i32, i32* %6
%21 = call i32 @set_seq_num(i8* %19, i32 %20)
%22 = load i8*, i8** %9
%23 = load i32, i32* %7
%24 = call i32 @SETLEN(i8* %22, i32 %23)
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %33
27: ; preds = %4
%28 = load i8*, i8** %9
%29 = getelementptr inbounds i8, i8* %28, i64 5
%30 = load i8*, i8** %8
%31 = load i32, i32* %7
%32 = call i32 @memcpy(i8* %29, i8* %30, i32 %31)
br label %33
33: ; preds = %27, %4
%34 = load i8*, i8** %9
ret i8* %34
}

LLVM IR (-O2):
define i8* @create_packet(i8 signext %0, i32 %1, i32 %2, i8* %3) {
%5 = add nsw i32 %2, 5
%6 = tail call i8* @malloc(i32 %5)
store i8 %0, i8* %6
%7 = tail call i32 @set_seq_num(i8* %6, i32 %1)
%8 = tail call i32 @SETLEN(i8* %6, i32 %2)
%9 = icmp eq i8* %3, null
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = getelementptr inbounds i8, i8* %6, i64 5
%12 = tail call i32 @memcpy(i8* %11, i8* %3, i32 %2)
br label %13
13: ; preds = %10, %4
ret i8* %6
}