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

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, ...);
int rdp_packed_size(int);
uint16_t rdp_checksum(const uint8_t flags, const uint32_t seq_ack_number,
                      const uint16_t size, const char *payload) {
  char buffer[1023];
  char *_magic_ = "CSC361";
  int payload_size = 0;
  memcpy(buffer + 0, _magic_, 6);
  memcpy(buffer + 6, &flags, 1);
  memcpy(buffer + 7, &seq_ack_number, 4);
  memcpy(buffer + 11, &size, 2);
  if (flags & ((uint8_t)0b00010000)) {
    memcpy(buffer + 15 - 2, payload, size);
  };
  uint16_t sum = 0;
  uint16_t word = 0;
  int i;
  int len = flags & ((uint8_t)0b00010000) ? size : 0;
  for (i = 0; i < rdp_packed_size(len - 2) / 2; i++) {
    memcpy(&word, buffer + (i * 2), 2);
    sum += word;
  }
  return ~sum;
}


x86 assembly (clang, -O0):
<rdp_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %dl,%al
mov    %sil,%dl
mov    %edi,-0x4(%rbp)
mov    %dl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
mov    %rcx,-0x10(%rbp)
lea    0xebc(%rip),%rax
mov    %rax,-0x418(%rbp)
movl   $0x0,-0x41c(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rsi
xor    %eax,%eax
mov    $0x6,%edx
call   1030 <memcpy@plt>
lea    -0x410(%rbp),%rdi
add    $0x6,%rdi
xor    %eax,%eax
lea    -0x4(%rbp),%rsi
mov    $0x1,%edx
call   1030 <memcpy@plt>
lea    -0x410(%rbp),%rdi
add    $0x7,%rdi
xor    %eax,%eax
lea    -0x5(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
lea    -0x410(%rbp),%rdi
add    $0xb,%rdi
xor    %eax,%eax
lea    -0x6(%rbp),%rsi
mov    $0x2,%edx
call   1030 <memcpy@plt>
mov    -0x4(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     11ed <rdp_checksum+0xcd>
lea    -0x410(%rbp),%rdi
add    $0xf,%rdi
add    $0xfffffffffffffffe,%rdi
mov    -0x10(%rbp),%rsi
movsbl -0x6(%rbp),%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
movb   $0x0,-0x41d(%rbp)
movb   $0x0,-0x41e(%rbp)
mov    -0x4(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     1219 <rdp_checksum+0xf9>
movsbl -0x6(%rbp),%eax
mov    %eax,-0x42c(%rbp)
jmp    1226 <rdp_checksum+0x106>
xor    %eax,%eax
mov    %eax,-0x42c(%rbp)
jmp    1226 <rdp_checksum+0x106>
mov    -0x42c(%rbp),%eax
mov    %eax,-0x428(%rbp)
movl   $0x0,-0x424(%rbp)
mov    -0x424(%rbp),%eax
mov    %eax,-0x430(%rbp)
mov    -0x428(%rbp),%edi
sub    $0x2,%edi
call   1040 <rdp_packed_size@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x430(%rbp),%eax
cmp    %ecx,%eax
jge    12c0 <rdp_checksum+0x1a0>
lea    -0x410(%rbp),%rsi
mov    -0x424(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rsi
xor    %eax,%eax
lea    -0x41e(%rbp),%rdi
mov    $0x2,%edx
call   1030 <memcpy@plt>
movsbl -0x41e(%rbp),%ecx
movsbl -0x41d(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x41d(%rbp)
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    123c <rdp_checksum+0x11c>
movsbl -0x41d(%rbp),%eax
xor    $0xffffffff,%eax
movsbl %al,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdp_checksum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rcx,%r14
mov    %edi,0xc(%rsp)
mov    %sil,0xb(%rsp)
mov    %dl,0x9(%rsp)
lea    0xeb8(%rip),%rsi
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x6,%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
lea    0x16(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
lea    0x17(%rsp),%rdi
lea    0xb(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
lea    0x1b(%rsp),%rdi
lea    0x9(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
mov    $0x1,%al
testb  $0x10,0xc(%rsp)
je     11c3 <rdp_checksum+0xa3>
lea    0x1d(%rsp),%rdi
movsbl 0x9(%rsp),%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <memcpy@plt>
testb  $0x10,0xc(%rsp)
sete   %al
movb   $0x0,0xa(%rsp)
movsbl 0x9(%rsp),%ecx
add    $0xfffffffe,%ecx
test   %al,%al
mov    $0xfffffffe,%r15d
cmove  %ecx,%r15d
mov    %r15d,%edi
call   1040 <rdp_packed_size@plt>
mov    $0xff,%bpl
cmp    $0x2,%eax
jl     1242 <rdp_checksum+0x122>
lea    0xa(%rsp),%r14
xor    %ebx,%ebx
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
movzbl 0xa(%rsp),%ebp
add    %r13d,%ebp
add    $0x1,%rbx
movsbl %bpl,%r13d
mov    %r15d,%edi
call   1040 <rdp_packed_size@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rax
add    $0x2,%r12
cmp    %rax,%rbx
jl     1200 <rdp_checksum+0xe0>
not    %bpl
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"CSC361\00"
define signext i8 @rdp_checksum(i32 %0, i8 signext %1, i8 signext %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca [1023 x i8]
%9 = alloca i8
store i32 %0, i32* %5
store i8 %1, i8* %6
store i8 %2, i8* %7
%10 = getelementptr inbounds [1023 x i8], [1023 x i8]* %8, i64 0, i64 0
%11 = call i32 (i8*, ...) @memcpy(i8* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%12 = getelementptr inbounds [1023 x i8], [1023 x i8]* %8, i64 0, i64 6
%13 = call i32 (i8*, ...) @memcpy(i8* %12, i32* %5, i32 1)
%14 = getelementptr inbounds [1023 x i8], [1023 x i8]* %8, i64 0, i64 7
%15 = call i32 (i8*, ...) @memcpy(i8* %14, i8* %6, i32 4)
%16 = getelementptr inbounds [1023 x i8], [1023 x i8]* %8, i64 0, i64 11
%17 = call i32 (i8*, ...) @memcpy(i8* %16, i8* %7, i32 2)
%18 = load i32, i32* %5
%19 = and i32 %18, 16
%20 = icmp eq i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %4
%22 = getelementptr inbounds [1023 x i8], [1023 x i8]* %8, i64 0, i64 13
%23 = load i8, i8* %7
%24 = sext i8 %23 to i32
%25 = call i32 (i8*, ...) @memcpy(i8* %22, i8* %3, i32 %24)
%26 = load i32, i32* %5
%27 = and i32 %26, 16
%28 = icmp eq i32 %27, 0
br label %29
29: ; preds = %21, %4
%30 = phi i1 [ %28, %21 ], [ true, %4 ]
store i8 0, i8* %9
%31 = load i8, i8* %7
%32 = sext i8 %31 to i32
%33 = add nsw i32 %32, -2
%34 = select i1 %30, i32 -2, i32 %33
%35 = call i32 @rdp_packed_size(i32 %34)
%36 = icmp sgt i32 %35, 1
br i1 %36, label %37, label %56
37: ; preds = %29, %37
%38 = phi i64 [ %46, %37 ], [ 0, %29 ]
%39 = phi i32 [ %48, %37 ], [ 0, %29 ]
%40 = shl nuw nsw i64 %38, 1
%41 = getelementptr inbounds [1023 x i8], [1023 x i8]* %8, i64 0, i64 %40
%42 = call i32 (i8*, ...) @memcpy(i8* %9, i8* %41, i32 2)
%43 = load i8, i8* %9
%44 = zext i8 %43 to i32
%45 = add nsw i32 %39, %44
%46 = add nuw nsw i64 %38, 1
%47 = shl i32 %45, 24
%48 = ashr exact i32 %47, 24
%49 = call i32 @rdp_packed_size(i32 %34)
%50 = sdiv i32 %49, 2
%51 = sext i32 %50 to i64
%52 = icmp slt i64 %46, %51
br i1 %52, label %37, label %53
53: ; preds = %37
%54 = trunc i32 %45 to i8
%55 = xor i8 %54, -1
br label %56
56: ; preds = %53, %29
%57 = phi i8 [ -1, %29 ], [ %55, %53 ]
ret i8 %57
}