#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 */
short calc_crc(unsigned char *, int);
void create_mbus_header(char *buf, char data_len, char c_field, int own_id,
                        char dev_ver, char dev_type) {
  short man_id = (32 * 32 * ('N' - '@')) + (32 * ('X' - '@')) + ('P' - '@');
  short crc;
  buf[0] = (10 - 1) + data_len;
  buf[1] = c_field;
  buf[2] = (man_id >> 0) & 0xFF;
  buf[3] = (man_id >> 8) & 0xFF;
  buf[4] = (own_id >> 0) & 0xFF;
  buf[5] = (own_id >> 8) & 0xFF;
  buf[6] = (own_id >> 16) & 0xFF;
  buf[7] = (own_id >> 24) & 0xFF;
  buf[8] = dev_ver;
  buf[9] = dev_type;
  crc = calc_crc((unsigned char *)buf, 10);
  buf[10] = (crc >> 8) & 0xFF;
  buf[11] = (crc >> 0) & 0xFF;
}


x86 assembly (clang, -O0):
<create_mbus_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %r8d,-0x28(%rbp)
mov    %ecx,%eax
mov    -0x28(%rbp),%ecx
mov    %eax,-0x24(%rbp)
mov    %edx,%eax
mov    -0x24(%rbp),%edx
mov    %eax,-0x20(%rbp)
mov    %esi,%eax
mov    -0x20(%rbp),%esi
mov    %eax,-0x1c(%rbp)
mov    %rdi,%r8
mov    -0x1c(%rbp),%edi
mov    %r9b,%al
mov    %r8,-0x8(%rbp)
mov    %dil,-0x9(%rbp)
mov    %sil,-0xa(%rbp)
mov    %edx,-0x10(%rbp)
mov    %cl,-0x11(%rbp)
mov    %al,-0x12(%rbp)
movw   $0x3b10,-0x14(%rbp)
movsbl -0x9(%rbp),%eax
add    $0x9,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0xa(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x1(%rax)
movswl -0x14(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x2(%rax)
movswl -0x14(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x10(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x4(%rax)
mov    -0x10(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x5(%rax)
mov    -0x10(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x6(%rax)
mov    -0x10(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x7(%rax)
mov    -0x11(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x8(%rax)
mov    -0x12(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x9(%rax)
mov    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1030 <calc_crc@plt>
mov    %ax,-0x16(%rbp)
movswl -0x16(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0xa(%rax)
movswl -0x16(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0xb(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_mbus_header>:
push   %rbx
mov    %rdi,%rbx
add    $0x9,%sil
mov    %sil,(%rdi)
mov    %dl,0x1(%rdi)
movw   $0x3b10,0x2(%rdi)
mov    %cl,0x4(%rdi)
mov    %ch,0x5(%rdi)
mov    %ecx,%eax
shr    $0x10,%eax
mov    %al,0x6(%rdi)
shr    $0x18,%ecx
mov    %cl,0x7(%rdi)
mov    %r8b,0x8(%rdi)
mov    %r9b,0x9(%rdi)
mov    $0xa,%esi
call   1030 <calc_crc@plt>
rol    $0x8,%ax
mov    %ax,0xa(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
define void @create_mbus_header(i8* %0, i8 signext %1, i8 signext %2, i32 %3, i8 signext %4, i8 signext %5) {
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
%11 = alloca i8
%12 = alloca i8
%13 = alloca i16
%14 = alloca i16
store i8* %0, i8** %7
store i8 %1, i8* %8
store i8 %2, i8* %9
store i32 %3, i32* %10
store i8 %4, i8* %11
store i8 %5, i8* %12
store i16 15120, i16* %13
%15 = load i8, i8* %8
%16 = sext i8 %15 to i32
%17 = add nsw i32 9, %16
%18 = trunc i32 %17 to i8
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 %18, i8* %20
%21 = load i8, i8* %9
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8 %21, i8* %23
%24 = load i16, i16* %13
%25 = sext i16 %24 to i32
%26 = ashr i32 %25, 0
%27 = and i32 %26, 255
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 2
store i8 %28, i8* %30
%31 = load i16, i16* %13
%32 = sext i16 %31 to i32
%33 = ashr i32 %32, 8
%34 = and i32 %33, 255
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %7
%37 = getelementptr inbounds i8, i8* %36, i64 3
store i8 %35, i8* %37
%38 = load i32, i32* %10
%39 = ashr i32 %38, 0
%40 = and i32 %39, 255
%41 = trunc i32 %40 to i8
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i64 4
store i8 %41, i8* %43
%44 = load i32, i32* %10
%45 = ashr i32 %44, 8
%46 = and i32 %45, 255
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %7
%49 = getelementptr inbounds i8, i8* %48, i64 5
store i8 %47, i8* %49
%50 = load i32, i32* %10
%51 = ashr i32 %50, 16
%52 = and i32 %51, 255
%53 = trunc i32 %52 to i8
%54 = load i8*, i8** %7
%55 = getelementptr inbounds i8, i8* %54, i64 6
store i8 %53, i8* %55
%56 = load i32, i32* %10
%57 = ashr i32 %56, 24
%58 = and i32 %57, 255
%59 = trunc i32 %58 to i8
%60 = load i8*, i8** %7
%61 = getelementptr inbounds i8, i8* %60, i64 7
store i8 %59, i8* %61
%62 = load i8, i8* %11
%63 = load i8*, i8** %7
%64 = getelementptr inbounds i8, i8* %63, i64 8
store i8 %62, i8* %64
%65 = load i8, i8* %12
%66 = load i8*, i8** %7
%67 = getelementptr inbounds i8, i8* %66, i64 9
store i8 %65, i8* %67
%68 = load i8*, i8** %7
%69 = call signext i16 @calc_crc(i8* %68, i32 10)
store i16 %69, i16* %14
%70 = load i16, i16* %14
%71 = sext i16 %70 to i32
%72 = ashr i32 %71, 8
%73 = and i32 %72, 255
%74 = trunc i32 %73 to i8
%75 = load i8*, i8** %7
%76 = getelementptr inbounds i8, i8* %75, i64 10
store i8 %74, i8* %76
%77 = load i16, i16* %14
%78 = sext i16 %77 to i32
%79 = ashr i32 %78, 0
%80 = and i32 %79, 255
%81 = trunc i32 %80 to i8
%82 = load i8*, i8** %7
%83 = getelementptr inbounds i8, i8* %82, i64 11
store i8 %81, i8* %83
ret void
}

LLVM IR (-O2):
define void @create_mbus_header(i8* %0, i8 signext %1, i8 signext %2, i32 %3, i8 signext %4, i8 signext %5) {
%7 = add i8 %1, 9
store i8 %7, i8* %0
%8 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %2, i8* %8
%9 = getelementptr inbounds i8, i8* %0, i64 2
store i8 16, i8* %9
%10 = getelementptr inbounds i8, i8* %0, i64 3
store i8 59, i8* %10
%11 = trunc i32 %3 to i8
%12 = getelementptr inbounds i8, i8* %0, i64 4
store i8 %11, i8* %12
%13 = lshr i32 %3, 8
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds i8, i8* %0, i64 5
store i8 %14, i8* %15
%16 = lshr i32 %3, 16
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds i8, i8* %0, i64 6
store i8 %17, i8* %18
%19 = lshr i32 %3, 24
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds i8, i8* %0, i64 7
store i8 %20, i8* %21
%22 = getelementptr inbounds i8, i8* %0, i64 8
store i8 %4, i8* %22
%23 = getelementptr inbounds i8, i8* %0, i64 9
store i8 %5, i8* %23
%24 = tail call signext i16 @calc_crc(i8* %0, i32 10)
%25 = lshr i16 %24, 8
%26 = trunc i16 %25 to i8
%27 = getelementptr inbounds i8, i8* %0, i64 10
store i8 %26, i8* %27
%28 = trunc i16 %24 to i8
%29 = getelementptr inbounds i8, i8* %0, i64 11
store i8 %28, i8* %29
ret void
}