#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 */
void asn1EncodeFlags(unsigned long flags, unsigned char *data, size_t length) {
  int c = 4;
  while ((c-- > 0) && (length > 0)) {
    *data = (flags >> (c << 3)) & 0xFF;
    data++;
    length--;
  }
}


x86 assembly (clang, -O0):
<asn1EncodeFlags>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
jle    113b <asn1EncodeFlags+0x3b>
cmpq   $0x0,-0x18(%rbp)
seta   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    114b <asn1EncodeFlags+0x4b>
jmp    1185 <asn1EncodeFlags+0x85>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x3,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
and    $0xff,%rax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1117 <asn1EncodeFlags+0x17>
pop    %rbp
ret

x86 assembly (clang, -O2):
<asn1EncodeFlags>:
test   %rdx,%rdx
je     1203 <asn1EncodeFlags+0x103>
mov    %rdi,%rax
add    $0xffffffffffffffff,%rdx
cmp    $0x3,%rdx
mov    $0x3,%ecx
cmovb  %rdx,%rcx
movq   %rcx,%xmm0
pshufd $0x44,%xmm0,%xmm0
movdqa 0xed1(%rip),%xmm1
por    %xmm0,%xmm1
movdqa 0xed5(%rip),%xmm2
movdqa %xmm2,%xmm3
pcmpgtd %xmm1,%xmm3
pcmpeqd %xmm2,%xmm1
pshufd $0xf5,%xmm1,%xmm1
pand   %xmm3,%xmm1
pshufd $0xf5,%xmm3,%xmm2
por    %xmm1,%xmm2
movd   %xmm2,%ecx
not    %ecx
test   $0x1,%cl
je     116d <asn1EncodeFlags+0x6d>
mov    %rax,%rcx
shr    $0x18,%rcx
mov    %cl,(%rsi)
pxor   0xeab(%rip),%xmm0
movdqa 0xeb3(%rip),%xmm1
movdqa %xmm1,%xmm2
pcmpgtd %xmm0,%xmm2
pcmpeqd %xmm0,%xmm1
pshufd $0xf5,%xmm1,%xmm1
pand   %xmm2,%xmm1
pshufd $0xf5,%xmm2,%xmm2
por    %xmm1,%xmm2
pcmpeqd %xmm1,%xmm1
pxor   %xmm1,%xmm2
pextrw $0x4,%xmm2,%ecx
test   $0x1,%cl
je     11b7 <asn1EncodeFlags+0xb7>
mov    %rax,%rcx
shr    $0x10,%rcx
mov    %cl,0x1(%rsi)
movdqa 0xe81(%rip),%xmm2
movdqa %xmm2,%xmm3
pcmpgtd %xmm0,%xmm3
pcmpeqd %xmm0,%xmm2
pshufd $0xf5,%xmm2,%xmm2
pand   %xmm3,%xmm2
pshufd $0xf5,%xmm3,%xmm0
por    %xmm2,%xmm0
pxor   %xmm0,%xmm1
pextrw $0x0,%xmm1,%ecx
test   $0x1,%cl
je     11ee <asn1EncodeFlags+0xee>
mov    %ah,0x2(%rsi)
pcmpeqd %xmm1,%xmm1
pxor   %xmm1,%xmm0
pextrw $0x4,%xmm0,%ecx
test   $0x1,%cl
je     1203 <asn1EncodeFlags+0x103>
mov    %al,0x3(%rsi)
ret

LLVM IR (-O0):
define void @asn1EncodeFlags(i64 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 4, i32* %7
br label %8
8: ; preds = %17, %3
%9 = load i32, i32* %7
%10 = add nsw i32 %9, -1
store i32 %10, i32* %7
%11 = icmp sgt i32 %9, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i64, i64* %6
%14 = icmp ugt i64 %13, 0
br label %15
15: ; preds = %12, %8
%16 = phi i1 [ false, %8 ], [ %14, %12 ]
br i1 %16, label %17, label %30
17: ; preds = %15
%18 = load i64, i64* %4
%19 = load i32, i32* %7
%20 = shl i32 %19, 3
%21 = zext i32 %20 to i64
%22 = lshr i64 %18, %21
%23 = and i64 %22, 255
%24 = trunc i64 %23 to i8
%25 = load i8*, i8** %5
store i8 %24, i8* %25
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
%28 = load i64, i64* %6
%29 = add i64 %28, -1
store i64 %29, i64* %6
br label %8
30: ; preds = %15
ret void
}

LLVM IR (-O2):
define void @asn1EncodeFlags(i64 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %32, label %5
5: ; preds = %3
%6 = add i64 %2, -1
%7 = call i64 @llvm.umin.i64(i64 %6, i64 3)
%8 = insertelement <4 x i64> poison, i64 %7, i64 0
%9 = shufflevector <4 x i64> %8, <4 x i64> poison, <4 x i32> zeroinitializer
%10 = icmp uge <4 x i64> %9, <i64 0, i64 1, i64 2, i64 3>
%11 = extractelement <4 x i1> %10, i64 0
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = lshr i64 %0, 24
%14 = trunc i64 %13 to i8
store i8 %14, i8* %1
br label %15
15: ; preds = %12, %5
%16 = extractelement <4 x i1> %10, i64 1
br i1 %16, label %17, label %21
17: ; preds = %15
%18 = getelementptr i8, i8* %1, i64 1
%19 = lshr i64 %0, 16
%20 = trunc i64 %19 to i8
store i8 %20, i8* %18
br label %21
21: ; preds = %17, %15
%22 = extractelement <4 x i1> %10, i64 2
br i1 %22, label %23, label %27
23: ; preds = %21
%24 = getelementptr i8, i8* %1, i64 2
%25 = lshr i64 %0, 8
%26 = trunc i64 %25 to i8
store i8 %26, i8* %24
br label %27
27: ; preds = %23, %21
%28 = extractelement <4 x i1> %10, i64 3
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = getelementptr i8, i8* %1, i64 3
%31 = trunc i64 %0 to i8
store i8 %31, i8* %30
br label %32
32: ; preds = %27, %29, %3
ret void
}