#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 crc16(unsigned char *, int);
int /*<<< orphan*/ printf(char *, int, unsigned char);
int main(void) {
  unsigned char cmd[8];
  cmd[0] = 0x00;
  cmd[1] = 0x04;
  cmd[2] = 0x00;
  cmd[3] = 0x00;
  cmd[4] = 0x00;
  cmd[5] = 0x01;
  unsigned short crc;
  char *ptr;
  crc = crc16(cmd, 6);
  ptr = (char *)&crc;
  cmd[6] = *ptr;
  cmd[7] = *++ptr;
  int i;
  for (i = 0; i < 8; ++i) {
    printf("cmd[%d] = 0x%02x\n", i, cmd[i]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0xc(%rbp)
movb   $0x4,-0xb(%rbp)
movb   $0x0,-0xa(%rbp)
movb   $0x0,-0x9(%rbp)
movb   $0x0,-0x8(%rbp)
movb   $0x1,-0x7(%rbp)
lea    -0xc(%rbp),%rdi
mov    $0x6,%esi
call   1040 <crc16@plt>
mov    %ax,-0xe(%rbp)
lea    -0xe(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x6(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    0x1(%rax),%al
mov    %al,-0x5(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jge    11b6 <main+0x96>
mov    -0x1c(%rbp),%esi
movslq -0x1c(%rbp),%rax
lea    0xe62(%rip),%rdi
movzbl -0xc(%rbp,%rax,1),%edx
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1186 <main+0x66>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x400,0x8(%rsp)
movw   $0x100,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x6,%esi
call   1040 <crc16@plt>
mov    %ax,0xe(%rsp)
movzbl 0x8(%rsp),%edx
lea    0xeac(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <printf@plt>
movzbl 0x9(%rsp),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <printf@plt>
movzbl 0xa(%rsp),%edx
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <printf@plt>
movzbl 0xb(%rsp),%edx
mov    %rbx,%rdi
mov    $0x3,%esi
call   1030 <printf@plt>
movzbl 0xc(%rsp),%edx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1030 <printf@plt>
movzbl 0xd(%rsp),%edx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1030 <printf@plt>
movzbl 0xe(%rsp),%edx
mov    %rbx,%rdi
mov    $0x6,%esi
call   1030 <printf@plt>
movzbl 0xf(%rsp),%edx
mov    %rbx,%rdi
mov    $0x7,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"cmd[%d] = 0x%02x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [8 x i8]
%3 = alloca i16
%4 = alloca i8*
%5 = alloca i32
store i32 0, i32* %1
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
store i8 0, i8* %6
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 1
store i8 4, i8* %7
%8 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 2
store i8 0, i8* %8
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 3
store i8 0, i8* %9
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 4
store i8 0, i8* %10
%11 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 5
store i8 1, i8* %11
%12 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%13 = call zeroext i16 @crc16(i8* %12, i32 6)
store i16 %13, i16* %3
%14 = bitcast i16* %3 to i8*
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 6
store i8 %16, i8* %17
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %4
%20 = load i8, i8* %19
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 7
store i8 %20, i8* %21
store i32 0, i32* %5
br label %22
22: ; preds = %32, %0
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 8
br i1 %24, label %25, label %35
25: ; preds = %22
%26 = load i32, i32* %5
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %26, i8 zeroext %30)
br label %32
32: ; preds = %25
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %22
35: ; preds = %22
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [18 x i8] c"cmd[%d] = 0x%02x\0A\00"
define i32 @main() {
%1 = alloca [8 x i8]
%2 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 0
store i8 0, i8* %2
%3 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 1
store i8 4, i8* %3
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 2
store i8 0, i8* %4
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 3
store i8 0, i8* %5
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 4
store i8 0, i8* %6
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 5
store i8 1, i8* %7
%8 = call zeroext i16 @crc16(i8* %2, i32 6)
%9 = trunc i16 %8 to i8
%10 = lshr i16 %8, 8
%11 = trunc i16 %10 to i8
%12 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 6
store i8 %9, i8* %12
%13 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 7
store i8 %11, i8* %13
%14 = load i8, i8* %2
%15 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 0, i8 zeroext %14)
%16 = load i8, i8* %3
%17 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 1, i8 zeroext %16)
%18 = load i8, i8* %4
%19 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 2, i8 zeroext %18)
%20 = load i8, i8* %5
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 3, i8 zeroext %20)
%22 = load i8, i8* %6
%23 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 4, i8 zeroext %22)
%24 = load i8, i8* %7
%25 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 5, i8 zeroext %24)
%26 = load i8, i8* %12
%27 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 6, i8 zeroext %26)
%28 = load i8, i8* %13
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 7, i8 zeroext %28)
ret i32 0
}