#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 */
unsigned short CyBtldr_ComputeChecksum(unsigned char *, unsigned int const);
int CyBtldr_CreateSetActiveAppCmd(unsigned char appId, unsigned char *cmdBuf,
                                  unsigned long *cmdSize,
                                  unsigned long *resSize) {
  const unsigned long COMMAND_DATA_SIZE = 1;
  const unsigned int COMMAND_SIZE = 0x07 + COMMAND_DATA_SIZE;
  unsigned short checksum;
  *resSize = 0x07;
  *cmdSize = COMMAND_SIZE;
  cmdBuf[0] = 0x01;
  cmdBuf[1] = 0x36;
  cmdBuf[2] = (unsigned char)COMMAND_DATA_SIZE;
  cmdBuf[3] = (unsigned char)(COMMAND_DATA_SIZE >> 8);
  cmdBuf[4] = appId;
  checksum = CyBtldr_ComputeChecksum(cmdBuf, COMMAND_SIZE - 3);
  cmdBuf[5] = (unsigned char)checksum;
  cmdBuf[6] = (unsigned char)(checksum >> 8);
  cmdBuf[7] = 0x17;
  return 0x00;
}


x86 assembly (clang, -O0):
<CyBtldr_CreateSetActiveAppCmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x7,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x8,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x36,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0x1,0x2(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x3(%rax)
mov    -0x1(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x4(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x5,%esi
call   1030 <CyBtldr_ComputeChecksum@plt>
mov    %ax,-0x2e(%rbp)
mov    -0x2e(%rbp),%ax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x5(%rax)
movzwl -0x2e(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x6(%rax)
mov    -0x10(%rbp),%rax
movb   $0x17,0x7(%rax)
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CyBtldr_CreateSetActiveAppCmd>:
push   %rbx
mov    %rsi,%rbx
movq   $0x7,(%rcx)
movq   $0x8,(%rdx)
movl   $0x13601,(%rsi)
mov    %dil,0x4(%rsi)
mov    %rsi,%rdi
mov    $0x5,%esi
call   1030 <CyBtldr_ComputeChecksum@plt>
mov    %al,0x5(%rbx)
mov    %ah,0x6(%rbx)
movb   $0x17,0x7(%rbx)
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @CyBtldr_CreateSetActiveAppCmd(i8 zeroext %0, i8* %1, i64* %2, i64* %3) {
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i16
store i8 %0, i8* %5
store i8* %1, i8** %6
store i64* %2, i64** %7
store i64* %3, i64** %8
store i64 1, i64* %9
store i32 8, i32* %10
%12 = load i64*, i64** %8
store i64 7, i64* %12
%13 = load i64*, i64** %7
store i64 8, i64* %13
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 1, i8* %15
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8 54, i8* %17
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 2
store i8 1, i8* %19
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i64 3
store i8 0, i8* %21
%22 = load i8, i8* %5
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 4
store i8 %22, i8* %24
%25 = load i8*, i8** %6
%26 = call zeroext i16 @CyBtldr_ComputeChecksum(i8* %25, i32 5)
store i16 %26, i16* %11
%27 = load i16, i16* %11
%28 = trunc i16 %27 to i8
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i64 5
store i8 %28, i8* %30
%31 = load i16, i16* %11
%32 = zext i16 %31 to i32
%33 = ashr i32 %32, 8
%34 = trunc i32 %33 to i8
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i64 6
store i8 %34, i8* %36
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i64 7
store i8 23, i8* %38
ret i32 0
}

LLVM IR (-O2):
define i32 @CyBtldr_CreateSetActiveAppCmd(i8 zeroext %0, i8* %1, i64* nocapture writeonly %2, i64* nocapture writeonly %3) {
store i64 7, i64* %3
store i64 8, i64* %2
store i8 1, i8* %1
%5 = getelementptr inbounds i8, i8* %1, i64 1
store i8 54, i8* %5
%6 = getelementptr inbounds i8, i8* %1, i64 2
store i8 1, i8* %6
%7 = getelementptr inbounds i8, i8* %1, i64 3
store i8 0, i8* %7
%8 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %0, i8* %8
%9 = tail call zeroext i16 @CyBtldr_ComputeChecksum(i8* %1, i32 5)
%10 = trunc i16 %9 to i8
%11 = getelementptr inbounds i8, i8* %1, i64 5
store i8 %10, i8* %11
%12 = lshr i16 %9, 8
%13 = trunc i16 %12 to i8
%14 = getelementptr inbounds i8, i8* %1, i64 6
store i8 %13, i8* %14
%15 = getelementptr inbounds i8, i8* %1, i64 7
store i8 23, i8* %15
ret i32 0
}