#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 const uint8_t;
typedef char uint32_t;
typedef char const uint16_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, ...);
char rdp_checksum(int const, char const, char const, char const *);
int /*<<< orphan*/ rdp_zero(char *, int);
char *rdp_pack(char *buffer, const uint8_t flags, const uint32_t seq_ack_number,
               const uint16_t size, const char *payload) {
  uint16_t checksum = rdp_checksum(flags, seq_ack_number, size, payload);
  char *_magic_ = "CSC361";
  rdp_zero(buffer, 1023 + 1);
  memcpy(buffer + 0, _magic_, 6);
  memcpy(buffer + 6, &flags, 1);
  memcpy(buffer + 7, &seq_ack_number, 4);
  memcpy(buffer + 11, &size, 2);
  memcpy(buffer + 13, &checksum, 2);
  if (flags & ((uint8_t)0b00010000)) {
    memcpy(buffer + 15, payload, size);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<rdp_pack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %cl,-0xd(%rbp)
mov    %al,-0xe(%rbp)
mov    %r8,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0xd(%rbp),%dl
mov    -0xe(%rbp),%al
mov    -0x18(%rbp),%rcx
movsbl %dl,%esi
movsbl %al,%edx
call   1030 <rdp_checksum@plt>
mov    %al,-0x19(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x400,%esi
call   1050 <rdp_zero@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    $0x6,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
add    $0x6,%rdi
xor    %eax,%eax
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
add    $0x7,%rdi
xor    %eax,%eax
lea    -0xd(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
add    $0xb,%rdi
xor    %eax,%eax
lea    -0xe(%rbp),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
add    $0xd,%rdi
xor    %eax,%eax
lea    -0x19(%rbp),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0xc(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     121b <rdp_pack+0xeb>
mov    -0x8(%rbp),%rdi
add    $0xf,%rdi
mov    -0x18(%rbp),%rsi
movsbl -0xe(%rbp),%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdp_pack>:
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rdi,%rbx
mov    %esi,0x4(%rsp)
mov    %dl,0x3(%rsp)
mov    %cl,0x1(%rsp)
mov    %esi,%edi
mov    %edx,%esi
mov    %ecx,%edx
mov    %r8,%rcx
call   1030 <rdp_checksum@plt>
mov    %al,0x2(%rsp)
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <rdp_zero@plt>
lea    0xe94(%rip),%rsi
mov    %rbx,%rdi
mov    $0x6,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
lea    0x6(%rbx),%rdi
lea    0x4(%rsp),%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
lea    0x7(%rbx),%rdi
lea    0x3(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
lea    0xb(%rbx),%rdi
lea    0x1(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
lea    0xd(%rbx),%rdi
lea    0x2(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
testb  $0x10,0x4(%rsp)
je     11e9 <rdp_pack+0xb9>
lea    0xf(%rbx),%rdi
movsbl 0x1(%rsp),%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [7 x i8] c"CSC361\00"
define i8* @rdp_pack(i8* %0, i32 %1, i8 signext %2, i8 signext %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8*
%11 = alloca i8
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8 %2, i8* %8
store i8 %3, i8* %9
store i8* %4, i8** %10
%13 = load i32, i32* %7
%14 = load i8, i8* %8
%15 = load i8, i8* %9
%16 = load i8*, i8** %10
%17 = call signext i8 @rdp_checksum(i32 %13, i8 signext %14, i8 signext %15, i8* %16)
store i8 %17, i8* %11
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %12
%18 = load i8*, i8** %6
%19 = call i32 @rdp_zero(i8* %18, i32 1024)
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8*, i8** %12
%23 = call i32 (i8*, ...) @memcpy(i8* %21, i8* %22, i32 6)
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 6
%26 = call i32 (i8*, ...) @memcpy(i8* %25, i32* %7, i32 1)
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 7
%29 = call i32 (i8*, ...) @memcpy(i8* %28, i8* %8, i32 4)
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i64 11
%32 = call i32 (i8*, ...) @memcpy(i8* %31, i8* %9, i32 2)
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 13
%35 = call i32 (i8*, ...) @memcpy(i8* %34, i8* %11, i32 2)
%36 = load i32, i32* %7
%37 = and i32 %36, 16
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %5
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i64 15
%42 = load i8*, i8** %10
%43 = load i8, i8* %9
%44 = sext i8 %43 to i32
%45 = call i32 (i8*, ...) @memcpy(i8* %41, i8* %42, i32 %44)
br label %46
46: ; preds = %39, %5
%47 = load i8*, i8** %6
ret i8* %47
}

LLVM IR (-O2):
@.str = [7 x i8] c"CSC361\00"
define i8* @rdp_pack(i8* returned %0, i32 %1, i8 signext %2, i8 signext %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
store i32 %1, i32* %6
store i8 %2, i8* %7
store i8 %3, i8* %8
%10 = tail call signext i8 @rdp_checksum(i32 %1, i8 signext %2, i8 signext %3, i8* %4)
store i8 %10, i8* %9
%11 = tail call i32 @rdp_zero(i8* %0, i32 1024)
%12 = tail call i32 (i8*, ...) @memcpy(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%13 = getelementptr inbounds i8, i8* %0, i64 6
%14 = call i32 (i8*, ...) @memcpy(i8* %13, i32* %6, i32 1)
%15 = getelementptr inbounds i8, i8* %0, i64 7
%16 = call i32 (i8*, ...) @memcpy(i8* %15, i8* %7, i32 4)
%17 = getelementptr inbounds i8, i8* %0, i64 11
%18 = call i32 (i8*, ...) @memcpy(i8* %17, i8* %8, i32 2)
%19 = getelementptr inbounds i8, i8* %0, i64 13
%20 = call i32 (i8*, ...) @memcpy(i8* %19, i8* %9, i32 2)
%21 = load i32, i32* %6
%22 = and i32 %21, 16
%23 = icmp eq i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %5
%25 = getelementptr inbounds i8, i8* %0, i64 15
%26 = load i8, i8* %8
%27 = sext i8 %26 to i32
%28 = call i32 (i8*, ...) @memcpy(i8* %25, i8* %4, i32 %27)
br label %29
29: ; preds = %24, %5
ret i8* %0
}