#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 /*<<< orphan*/ int32_t;

/* Variables and functions */
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ms_gswap4(int /*<<< orphan*/ *);
int msr_pack_int_32(int32_t *packed, int32_t *data, int ns, int max_bytes,
                    int pad, int *pnbytes, int *pnsamples, int swapflag) {
  int points_remaining = ns;
  int i = 0;
  while (points_remaining > 0 && max_bytes >= 4) {
    *packed = data[i];
    if (swapflag)
      ms_gswap4(packed);
    packed++;
    max_bytes -= 4;
    points_remaining--;
    i++;
  }
  *pnbytes = (ns - points_remaining) * 4;
  if (pad) {
    memset((void *)packed, 0, max_bytes);
    *pnbytes += max_bytes;
  }
  *pnsamples = ns - points_remaining;
  return 0;
}


x86 assembly (clang, -O0):
<msr_pack_int_32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x31(%rbp)
jle    116b <msr_pack_int_32+0x4b>
cmpl   $0x4,-0x18(%rbp)
setge  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    117b <msr_pack_int_32+0x5b>
jmp    11cb <msr_pack_int_32+0xab>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,0x18(%rbp)
je     119f <msr_pack_int_32+0x7f>
mov    -0x8(%rbp),%rdi
call   1030 <ms_gswap4@plt>
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1152 <msr_pack_int_32+0x32>
mov    -0x14(%rbp),%ecx
sub    -0x2c(%rbp),%ecx
shl    $0x2,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x1c(%rbp)
je     11fd <msr_pack_int_32+0xdd>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
sub    -0x2c(%rbp),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msr_pack_int_32>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %rdi,%rbx
test   %edx,%edx
mov    %r9,0x10(%rsp)
mov    %rdx,0x8(%rsp)
mov    %r8d,0x4(%rsp)
jle    1196 <msr_pack_int_32+0x76>
cmp    $0x4,%ebp
jl     1196 <msr_pack_int_32+0x76>
mov    %rsi,%r12
mov    0x58(%rsp),%r14d
mov    %rdx,%r15
cs nopw 0x0(%rax,%rax,1)
mov    (%r12),%eax
mov    %eax,(%rbx)
test   %r14d,%r14d
je     1173 <msr_pack_int_32+0x53>
mov    %rbx,%rdi
call   1030 <ms_gswap4@plt>
add    $0x4,%rbx
lea    -0x4(%rbp),%r13d
lea    -0x1(%r15),%eax
cmp    $0x2,%r15d
jl     119b <msr_pack_int_32+0x7b>
add    $0x4,%r12
mov    %eax,%r15d
cmp    $0x7,%ebp
mov    %r13d,%ebp
jg     1160 <msr_pack_int_32+0x40>
jmp    119b <msr_pack_int_32+0x7b>
mov    %ebp,%r13d
mov    %edx,%eax
mov    0x50(%rsp),%r14
mov    0x8(%rsp),%rbp
sub    %eax,%ebp
lea    0x0(,%rbp,4),%eax
mov    0x10(%rsp),%r15
mov    %eax,(%r15)
cmpl   $0x0,0x4(%rsp)
je     11cd <msr_pack_int_32+0xad>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1040 <memset@plt>
add    %r13d,(%r15)
mov    %ebp,(%r14)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @msr_pack_int_32(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4, i32* %5, i32* %6, i32 %7) {
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32* %0, i32** %9
store i32* %1, i32** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store i32* %5, i32** %14
store i32* %6, i32** %15
store i32 %7, i32* %16
%19 = load i32, i32* %11
store i32 %19, i32* %17
store i32 0, i32* %18
br label %20
20: ; preds = %40, %8
%21 = load i32, i32* %17
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %12
%25 = icmp sge i32 %24, 4
br label %26
26: ; preds = %23, %20
%27 = phi i1 [ false, %20 ], [ %25, %23 ]
br i1 %27, label %28, label %49
28: ; preds = %26
%29 = load i32*, i32** %10
%30 = load i32, i32* %18
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
%34 = load i32*, i32** %9
store i32 %33, i32* %34
%35 = load i32, i32* %16
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %28
%38 = load i32*, i32** %9
%39 = call i32 @ms_gswap4(i32* %38)
br label %40
40: ; preds = %37, %28
%41 = load i32*, i32** %9
%42 = getelementptr inbounds i32, i32* %41, i32 1
store i32* %42, i32** %9
%43 = load i32, i32* %12
%44 = sub nsw i32 %43, 4
store i32 %44, i32* %12
%45 = load i32, i32* %17
%46 = add nsw i32 %45, -1
store i32 %46, i32* %17
%47 = load i32, i32* %18
%48 = add nsw i32 %47, 1
store i32 %48, i32* %18
br label %20
49: ; preds = %26
%50 = load i32, i32* %11
%51 = load i32, i32* %17
%52 = sub nsw i32 %50, %51
%53 = mul nsw i32 %52, 4
%54 = load i32*, i32** %14
store i32 %53, i32* %54
%55 = load i32, i32* %13
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %66
57: ; preds = %49
%58 = load i32*, i32** %9
%59 = bitcast i32* %58 to i8*
%60 = load i32, i32* %12
%61 = call i32 @memset(i8* %59, i32 0, i32 %60)
%62 = load i32, i32* %12
%63 = load i32*, i32** %14
%64 = load i32, i32* %63
%65 = add nsw i32 %64, %62
store i32 %65, i32* %63
br label %66
66: ; preds = %57, %49
%67 = load i32, i32* %11
%68 = load i32, i32* %17
%69 = sub nsw i32 %67, %68
%70 = load i32*, i32** %15
store i32 %69, i32* %70
ret i32 0
}

LLVM IR (-O2):
define i32 @msr_pack_int_32(i32* %0, i32* nocapture %1, i32 %2, i32 %3, i32 %4, i32* nocapture %5, i32* nocapture writeonly %6, i32 %7) {
%9 = icmp sgt i32 %2, 0
%10 = icmp sgt i32 %3, 3
%11 = and i1 %9, %10
br i1 %11, label %12, label %31
12: ; preds = %8
%13 = icmp eq i32 %7, 0
br label %14
14: ; preds = %12, %23
%15 = phi i64 [ 0, %12 ], [ %27, %23 ]
%16 = phi i32 [ %2, %12 ], [ %26, %23 ]
%17 = phi i32* [ %0, %12 ], [ %24, %23 ]
%18 = phi i32 [ %3, %12 ], [ %25, %23 ]
%19 = getelementptr inbounds i32, i32* %1, i64 %15
%20 = load i32, i32* %19
store i32 %20, i32* %17
br i1 %13, label %23, label %21
21: ; preds = %14
%22 = tail call i32 @ms_gswap4(i32* %17)
br label %23
23: ; preds = %21, %14
%24 = getelementptr inbounds i32, i32* %17, i64 1
%25 = add nsw i32 %18, -4
%26 = add nsw i32 %16, -1
%27 = add nuw nsw i64 %15, 1
%28 = icmp sgt i32 %16, 1
%29 = icmp sgt i32 %18, 7
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %14, label %31
31: ; preds = %23, %8
%32 = phi i32 [ %3, %8 ], [ %25, %23 ]
%33 = phi i32* [ %0, %8 ], [ %24, %23 ]
%34 = phi i32 [ %2, %8 ], [ %26, %23 ]
%35 = sub nsw i32 %2, %34
%36 = shl nsw i32 %35, 2
store i32 %36, i32* %5
%37 = icmp eq i32 %4, 0
br i1 %37, label %43, label %38
38: ; preds = %31
%39 = bitcast i32* %33 to i8*
%40 = tail call i32 @memset(i8* %39, i32 0, i32 %32)
%41 = load i32, i32* %5
%42 = add nsw i32 %41, %32
store i32 %42, i32* %5
br label %43
43: ; preds = %38, %31
store i32 %35, i32* %6
ret i32 0
}