#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 */
int /*<<< orphan*/ memcpy(void *, void *, unsigned int);
int pack_msg(char *inbuf, unsigned int len, char *outbuf, char type) {
  char head[7] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  head[2] = 0x000000ff & (len >> 24);
  head[3] = 0x000000ff & (len >> 16);
  head[4] = 0x000000ff & (len >> 8);
  head[5] = 0x000000ff & (len >> 0);
  head[6] = 0xff & type;
  memcpy((void *)outbuf, (void *)&head, 7);
  memcpy((void *)(outbuf + 7), (void *)inbuf, len);
  return (7 + len);
}


x86 assembly (clang, -O0):
<pack_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x7,%edx
call   1030 <memset@plt>
mov    -0xc(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,-0x1e(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x0,%eax
and    $0xff,%eax
mov    %al,-0x1b(%rbp)
movsbl -0x19(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1a(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x7,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rdi
add    $0x7,%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0xc(%rbp),%eax
add    $0x7,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pack_msg>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
movw   $0x0,0x8(%rsp)
mov    %esi,%eax
shr    $0x18,%eax
mov    %al,0xa(%rsp)
mov    %esi,%eax
shr    $0x10,%eax
mov    %al,0xb(%rsp)
mov    %bh,0xc(%rsp)
mov    %bl,0xd(%rsp)
mov    %cl,0xe(%rsp)
lea    0x8(%rsp),%rsi
mov    %rdx,%rdi
mov    $0x7,%edx
call   1030 <memcpy@plt>
add    $0x7,%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
add    $0x7,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @pack_msg(i8* %0, i32 %1, i8* %2, i8 signext %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca [7 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8 %3, i8* %8
%10 = bitcast [7 x i8]* %9 to i8*
%11 = load i32, i32* %6
%12 = lshr i32 %11, 24
%13 = and i32 255, %12
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 2
store i8 %14, i8* %15
%16 = load i32, i32* %6
%17 = lshr i32 %16, 16
%18 = and i32 255, %17
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 3
store i8 %19, i8* %20
%21 = load i32, i32* %6
%22 = lshr i32 %21, 8
%23 = and i32 255, %22
%24 = trunc i32 %23 to i8
%25 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 4
store i8 %24, i8* %25
%26 = load i32, i32* %6
%27 = lshr i32 %26, 0
%28 = and i32 255, %27
%29 = trunc i32 %28 to i8
%30 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 5
store i8 %29, i8* %30
%31 = load i8, i8* %8
%32 = sext i8 %31 to i32
%33 = and i32 255, %32
%34 = trunc i32 %33 to i8
%35 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 6
store i8 %34, i8* %35
%36 = load i8*, i8** %7
%37 = bitcast [7 x i8]* %9 to i8*
%38 = call i32 @memcpy(i8* %36, i8* %37, i32 7)
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 7
%41 = load i8*, i8** %5
%42 = load i32, i32* %6
%43 = call i32 @memcpy(i8* %40, i8* %41, i32 %42)
%44 = load i32, i32* %6
%45 = add i32 7, %44
ret i32 %45
}

LLVM IR (-O2):
define i32 @pack_msg(i8* %0, i32 %1, i8* %2, i8 signext %3) {
%5 = alloca [7 x i8]
%6 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%7 = bitcast [7 x i8]* %5 to i16*
store i16 0, i16* %7
%8 = lshr i32 %1, 24
%9 = trunc i32 %8 to i8
%10 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 2
store i8 %9, i8* %10
%11 = lshr i32 %1, 16
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 3
store i8 %12, i8* %13
%14 = lshr i32 %1, 8
%15 = trunc i32 %14 to i8
%16 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 4
store i8 %15, i8* %16
%17 = trunc i32 %1 to i8
%18 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 5
store i8 %17, i8* %18
%19 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 6
store i8 %3, i8* %19
%20 = call i32 @memcpy(i8* %2, i8* %6, i32 7)
%21 = getelementptr inbounds i8, i8* %2, i64 7
%22 = call i32 @memcpy(i8* %21, i8* %0, i32 %1)
%23 = add i32 %1, 7
ret i32 %23
}