int store_long(unsigned long l, int nbytes, unsigned char *c) {
  int i;
  for (i = 0; i < nbytes; i++)
    c[i] = (l >> (8 * (nbytes - i - 1))) & 0xff;
  return nbytes;
}


x86 assembly (clang, -O0):
<store_long>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1158 <store_long+0x58>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
shl    $0x3,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1116 <store_long+0x16>
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<store_long>:
push   %rax
mov    %esi,%eax
test   %esi,%esi
jle    17e6 <store_long+0x6e6>
mov    %eax,%r8d
cmp    $0x10,%eax
jae    111b <store_long+0x1b>
xor    %r11d,%r11d
jmp    17c4 <store_long+0x6c4>
mov    %r8d,%r11d
and    $0xfffffff0,%r11d
movd   %eax,%xmm0
pshufd $0x0,%xmm0,%xmm12
movq   %rdi,%xmm0
pshufd $0x44,%xmm0,%xmm10
lea    -0x10(%r11),%rcx
mov    %rcx,%r9
shr    $0x4,%r9
add    $0x1,%r9
test   %rcx,%rcx
je     17e8 <store_long+0x6e8>
mov    %r9,%rcx
and    $0xfffffffffffffffe,%rcx
movdqa 0xee2(%rip),%xmm0
movdqa 0xeea(%rip),%xmm1
movdqa 0xef1(%rip),%xmm14
movdqa 0xef8(%rip),%xmm15
xor    %r10d,%r10d
nopl   0x0(%rax,%rax,1)
movdqa %xmm15,-0x40(%rsp)
movdqa %xmm14,-0x30(%rsp)
movdqa %xmm1,-0x20(%rsp)
movdqa %xmm0,-0x10(%rsp)
movdqa %xmm0,%xmm4
movdqa %xmm0,-0x60(%rsp)
movdqa %xmm1,-0x70(%rsp)
movdqa %xmm14,-0x80(%rsp)
pcmpeqd %xmm0,%xmm0
pxor   %xmm0,%xmm4
movdqa %xmm1,%xmm3
pxor   %xmm0,%xmm3
pxor   %xmm0,%xmm14
movdqa %xmm15,%xmm1
pxor   %xmm0,%xmm1
paddd  %xmm12,%xmm1
paddd  %xmm12,%xmm14
paddd  %xmm12,%xmm3
paddd  %xmm12,%xmm4
pslld  $0x3,%xmm4
pslld  $0x3,%xmm3
pslld  $0x3,%xmm14
pslld  $0x3,%xmm1
movdqa %xmm1,%xmm2
pxor   %xmm9,%xmm9
punpckhdq %xmm9,%xmm2
movdqa %xmm14,%xmm5
punpckhdq %xmm9,%xmm5
xorps  %xmm6,%xmm6
movss  %xmm14,%xmm6
xorps  %xmm8,%xmm8
movss  %xmm3,%xmm8
movdqa %xmm4,%xmm7
pxor   %xmm0,%xmm0
movss  %xmm4,%xmm0
movdqa %xmm10,%xmm11
psrlq  %xmm0,%xmm11
punpckhdq %xmm9,%xmm7
movdqa %xmm4,%xmm13
psrlq  $0x20,%xmm13
movdqa %xmm10,%xmm0
psrlq  %xmm13,%xmm0
movsd  %xmm11,%xmm0
movdqa %xmm10,%xmm11
psrlq  %xmm7,%xmm11
psrldq $0xc,%xmm4
movdqa %xmm10,%xmm13
psrlq  %xmm4,%xmm13
movdqa %xmm10,%xmm7
psrlq  %xmm8,%xmm7
movdqa %xmm3,%xmm4
psrlq  $0x20,%xmm4
movdqa %xmm10,%xmm8
psrlq  %xmm4,%xmm8
movdqa %xmm3,%xmm4
punpckhdq %xmm9,%xmm4
movsd  %xmm11,%xmm13
movdqa %xmm10,%xmm11
psrlq  %xmm4,%xmm11
psrldq $0xc,%xmm3
movdqa %xmm10,%xmm4
psrlq  %xmm3,%xmm4
movdqa %xmm10,%xmm3
psrlq  %xmm6,%xmm3
movsd  %xmm7,%xmm8
movdqa %xmm14,%xmm7
psrlq  $0x20,%xmm7
movdqa %xmm10,%xmm6
psrlq  %xmm7,%xmm6
movdqa %xmm10,%xmm7
psrlq  %xmm5,%xmm7
pxor   %xmm5,%xmm5
movss  %xmm1,%xmm5
movsd  %xmm11,%xmm4
psrldq $0xc,%xmm14
movdqa %xmm10,%xmm11
psrlq  %xmm14,%xmm11
movsd  %xmm3,%xmm6
movdqa %xmm10,%xmm14
psrlq  %xmm5,%xmm14
movdqa %xmm1,%xmm5
psrlq  $0x20,%xmm5
movdqa %xmm10,%xmm3
psrlq  %xmm5,%xmm3
movdqa %xmm10,%xmm5
psrlq  %xmm2,%xmm5
movsd  %xmm7,%xmm11
psrldq $0xc,%xmm1
movdqa %xmm10,%xmm2
psrlq  %xmm1,%xmm2
movsd  %xmm14,%xmm3
movsd  %xmm5,%xmm2
movapd 0xd49(%rip),%xmm9
andpd  %xmm9,%xmm2
andpd  %xmm9,%xmm3
packuswb %xmm2,%xmm3
andpd  %xmm9,%xmm11
andpd  %xmm9,%xmm6
packuswb %xmm11,%xmm6
packuswb %xmm6,%xmm3
andpd  %xmm9,%xmm4
andpd  %xmm9,%xmm8
packuswb %xmm4,%xmm8
andpd  %xmm9,%xmm13
andpd  %xmm9,%xmm0
packuswb %xmm13,%xmm0
packuswb %xmm0,%xmm8
packuswb %xmm8,%xmm3
movdqu %xmm3,(%rdx,%r10,1)
movdqa %xmm12,%xmm1
movdqa %xmm15,-0x50(%rsp)
psubd  %xmm15,%xmm1
pslld  $0x3,%xmm1
movdqa %xmm12,%xmm7
psubd  -0x80(%rsp),%xmm7
pslld  $0x3,%xmm7
movdqa %xmm12,%xmm4
psubd  -0x70(%rsp),%xmm4
pslld  $0x3,%xmm4
movdqa %xmm12,%xmm5
psubd  -0x60(%rsp),%xmm5
pslld  $0x3,%xmm5
movdqa 0xc9c(%rip),%xmm0
paddd  %xmm0,%xmm5
paddd  %xmm0,%xmm4
paddd  %xmm0,%xmm7
paddd  %xmm0,%xmm1
movdqa %xmm1,%xmm8
pxor   %xmm0,%xmm0
punpckhdq %xmm0,%xmm8
movdqa %xmm7,%xmm14
punpckhdq %xmm0,%xmm14
pxor   %xmm6,%xmm6
xorpd  %xmm11,%xmm11
movss  %xmm7,%xmm11
xorpd  %xmm13,%xmm13
movss  %xmm4,%xmm13
movdqa %xmm5,%xmm2
pxor   %xmm0,%xmm0
movss  %xmm5,%xmm0
movdqa %xmm10,%xmm3
psrlq  %xmm0,%xmm3
punpckhdq %xmm6,%xmm2
movdqa %xmm5,%xmm6
psrlq  $0x20,%xmm6
movdqa %xmm10,%xmm0
psrlq  %xmm6,%xmm0
movsd  %xmm3,%xmm0
movdqa %xmm10,%xmm3
psrlq  %xmm2,%xmm3
psrldq $0xc,%xmm5
movdqa %xmm10,%xmm2
psrlq  %xmm5,%xmm2
movdqa %xmm12,%xmm15
movdqa %xmm10,%xmm12
psrlq  %xmm13,%xmm12
movdqa %xmm4,%xmm6
psrlq  $0x20,%xmm6
movdqa %xmm10,%xmm5
psrlq  %xmm6,%xmm5
movdqa %xmm4,%xmm6
punpckhdq 0xc29(%rip),%xmm6
movsd  %xmm3,%xmm2
movdqa %xmm10,%xmm13
psrlq  %xmm6,%xmm13
psrldq $0xc,%xmm4
movdqa %xmm10,%xmm6
psrlq  %xmm4,%xmm6
movdqa %xmm10,%xmm3
psrlq  %xmm11,%xmm3
movsd  %xmm12,%xmm5
movdqa %xmm7,%xmm11
psrlq  $0x20,%xmm11
movdqa %xmm10,%xmm4
psrlq  %xmm11,%xmm4
movdqa %xmm10,%xmm11
psrlq  %xmm14,%xmm11
xorpd  %xmm12,%xmm12
movss  %xmm1,%xmm12
movsd  %xmm13,%xmm6
psrldq $0xc,%xmm7
movdqa %xmm10,%xmm13
psrlq  %xmm7,%xmm13
movsd  %xmm3,%xmm4
movdqa %xmm10,%xmm14
psrlq  %xmm12,%xmm14
movdqa %xmm1,%xmm7
psrlq  $0x20,%xmm7
movdqa %xmm10,%xmm3
psrlq  %xmm7,%xmm3
movdqa %xmm10,%xmm12
psrlq  %xmm8,%xmm12
movsd  %xmm11,%xmm13
psrldq $0xc,%xmm1
movdqa %xmm10,%xmm7
psrlq  %xmm1,%xmm7
movdqa -0x20(%rsp),%xmm1
movsd  %xmm14,%xmm3
movsd  %xmm12,%xmm7
movdqa %xmm15,%xmm12
andpd  %xmm9,%xmm7
andpd  %xmm9,%xmm3
packuswb %xmm7,%xmm3
andpd  %xmm9,%xmm13
andpd  %xmm9,%xmm4
packuswb %xmm13,%xmm4
packuswb %xmm4,%xmm3
andpd  %xmm9,%xmm6
andpd  %xmm9,%xmm5
packuswb %xmm6,%xmm5
andpd  %xmm9,%xmm2
andpd  %xmm9,%xmm0
packuswb %xmm2,%xmm0
movdqa -0x30(%rsp),%xmm14
packuswb %xmm0,%xmm5
movdqa -0x10(%rsp),%xmm0
packuswb %xmm5,%xmm3
movdqu %xmm3,0x10(%rdx,%r10,1)
movdqa -0x40(%rsp),%xmm15
add    $0x20,%r10
movdqa 0xae3(%rip),%xmm4
paddd  %xmm4,%xmm15
paddd  %xmm4,%xmm14
paddd  %xmm4,%xmm1
paddd  %xmm4,%xmm0
add    $0xfffffffffffffffe,%rcx
jne    1180 <store_long+0x80>
movdqa 0xacf(%rip),%xmm4
movdqa %xmm4,%xmm2
psubd  -0x50(%rsp),%xmm2
movdqa %xmm4,%xmm3
psubd  -0x80(%rsp),%xmm3
movdqa %xmm4,%xmm1
psubd  -0x70(%rsp),%xmm1
psubd  -0x60(%rsp),%xmm4
test   $0x1,%r9b
je     17bf <store_long+0x6bf>
paddd  %xmm12,%xmm2
paddd  %xmm12,%xmm3
paddd  %xmm12,%xmm1
paddd  %xmm12,%xmm4
pslld  $0x3,%xmm4
pslld  $0x3,%xmm1
pslld  $0x3,%xmm3
pslld  $0x3,%xmm2
pxor   %xmm5,%xmm5
movdqa %xmm2,%xmm8
punpckhdq %xmm5,%xmm8
xorpd  %xmm9,%xmm9
movss  %xmm2,%xmm9
movdqa %xmm3,%xmm11
punpckhdq %xmm5,%xmm11
pxor   %xmm12,%xmm12
movss  %xmm3,%xmm12
movdqa %xmm1,%xmm13
punpckhdq %xmm5,%xmm13
xorpd  %xmm6,%xmm6
movss  %xmm1,%xmm6
movdqa %xmm4,%xmm0
punpckhdq %xmm5,%xmm0
movdqa %xmm10,%xmm7
psrlq  %xmm0,%xmm7
movss  %xmm4,%xmm5
movdqa %xmm4,%xmm0
psrldq $0xc,%xmm0
movdqa %xmm10,%xmm14
psrlq  %xmm0,%xmm14
movsd  %xmm7,%xmm14
movdqa %xmm10,%xmm0
psrlq  %xmm5,%xmm0
psrlq  $0x20,%xmm4
movdqa %xmm10,%xmm15
psrlq  %xmm4,%xmm15
movdqa %xmm10,%xmm7
psrlq  %xmm6,%xmm7
movsd  %xmm0,%xmm15
movdqa %xmm1,%xmm0
psrlq  $0x20,%xmm0
movdqa %xmm10,%xmm4
psrlq  %xmm0,%xmm4
movsd  %xmm7,%xmm4
movdqa %xmm10,%xmm0
psrlq  %xmm13,%xmm0
psrldq $0xc,%xmm1
movdqa %xmm10,%xmm7
psrlq  %xmm1,%xmm7
movdqa %xmm10,%xmm1
psrlq  %xmm12,%xmm1
movsd  %xmm0,%xmm7
movdqa %xmm3,%xmm0
psrlq  $0x20,%xmm0
movdqa %xmm10,%xmm6
psrlq  %xmm0,%xmm6
movsd  %xmm1,%xmm6
movdqa %xmm10,%xmm0
psrlq  %xmm11,%xmm0
psrldq $0xc,%xmm3
movdqa %xmm10,%xmm5
psrlq  %xmm3,%xmm5
movdqa %xmm10,%xmm3
psrlq  %xmm9,%xmm3
movsd  %xmm0,%xmm5
movdqa %xmm2,%xmm0
psrlq  $0x20,%xmm0
movdqa %xmm10,%xmm1
psrlq  %xmm0,%xmm1
movsd  %xmm3,%xmm1
movdqa %xmm10,%xmm0
psrlq  %xmm8,%xmm0
psrldq $0xc,%xmm2
psrlq  %xmm2,%xmm10
movsd  %xmm0,%xmm10
movapd 0x919(%rip),%xmm0
andpd  %xmm0,%xmm10
andpd  %xmm0,%xmm1
packuswb %xmm10,%xmm1
andpd  %xmm0,%xmm5
andpd  %xmm0,%xmm6
packuswb %xmm5,%xmm6
packuswb %xmm6,%xmm1
andpd  %xmm0,%xmm7
andpd  %xmm0,%xmm4
packuswb %xmm7,%xmm4
andpd  %xmm0,%xmm15
andpd  %xmm0,%xmm14
packuswb %xmm14,%xmm15
packuswb %xmm15,%xmm4
packuswb %xmm4,%xmm1
movdqu %xmm1,(%rdx,%r10,1)
cmp    %r8,%r11
je     17e6 <store_long+0x6e6>
mov    %eax,%ecx
sub    %r11d,%ecx
lea    -0x8(,%rcx,8),%ecx
mov    %rdi,%rsi
shr    %cl,%rsi
mov    %sil,(%rdx,%r11,1)
add    $0x1,%r11
add    $0xfffffff8,%ecx
cmp    %r11,%r8
jne    17d0 <store_long+0x6d0>
pop    %rcx
ret
movdqa 0x810(%rip),%xmm4
movdqa 0x818(%rip),%xmm1
movdqa 0x820(%rip),%xmm3
movdqa 0x828(%rip),%xmm2
xor    %r10d,%r10d
test   $0x1,%r9b
jne    160f <store_long+0x50f>
jmp    17bf <store_long+0x6bf>

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

LLVM IR (-O2):
define i32 @store_long(i64 %0, i32 returned %1, i8* nocapture writeonly %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %76
5: ; preds = %3
%6 = zext i32 %1 to i64
%7 = icmp ult i32 %1, 16
br i1 %7, label %62, label %8
8: ; preds = %5
%9 = and i64 %6, 4294967280
%10 = insertelement <16 x i32> poison, i32 %1, i64 0
%11 = shufflevector <16 x i32> %10, <16 x i32> poison, <16 x i32> zeroinitializer
%12 = insertelement <16 x i64> poison, i64 %0, i64 0
%13 = shufflevector <16 x i64> %12, <16 x i64> poison, <16 x i32> zeroinitializer
%14 = add nsw i64 %9, -16
%15 = lshr exact i64 %14, 4
%16 = add nuw nsw i64 %15, 1
%17 = and i64 %16, 1
%18 = icmp eq i64 %14, 0
br i1 %18, label %48, label %19
19: ; preds = %8
%20 = and i64 %16, 2305843009213693950
br label %21
21: ; preds = %21, %19
%22 = phi i64 [ 0, %19 ], [ %42, %21 ]
%23 = phi <16 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>, %19 ], [ %43, %21 ]
%24 = phi i64 [ 0, %19 ], [ %44, %21 ]
%25 = xor <16 x i32> %23, <i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1>
%26 = add <16 x i32> %11, %25
%27 = shl nsw <16 x i32> %26, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
%28 = zext <16 x i32> %27 to <16 x i64>
%29 = lshr <16 x i64> %13, %28
%30 = trunc <16 x i64> %29 to <16 x i8>
%31 = getelementptr inbounds i8, i8* %2, i64 %22
%32 = bitcast i8* %31 to <16 x i8>*
store <16 x i8> %30, <16 x i8>* %32
%33 = or i64 %22, 16
%34 = sub <16 x i32> <i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17, i32 -17>, %23
%35 = add <16 x i32> %11, %34
%36 = shl nsw <16 x i32> %35, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
%37 = zext <16 x i32> %36 to <16 x i64>
%38 = lshr <16 x i64> %13, %37
%39 = trunc <16 x i64> %38 to <16 x i8>
%40 = getelementptr inbounds i8, i8* %2, i64 %33
%41 = bitcast i8* %40 to <16 x i8>*
store <16 x i8> %39, <16 x i8>* %41
%42 = add nuw i64 %22, 32
%43 = add <16 x i32> %23, <i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32>
%44 = add i64 %24, 2
%45 = icmp eq i64 %44, %20
br i1 %45, label %46, label %21
46: ; preds = %21
%47 = sub <16 x i32> <i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33, i32 -33>, %23
br label %48
48: ; preds = %46, %8
%49 = phi i64 [ 0, %8 ], [ %42, %46 ]
%50 = phi <16 x i32> [ <i32 -1, i32 -2, i32 -3, i32 -4, i32 -5, i32 -6, i32 -7, i32 -8, i32 -9, i32 -10, i32 -11, i32 -12, i32 -13, i32 -14, i32 -15, i32 -16>, %8 ], [ %47, %46 ]
%51 = icmp eq i64 %17, 0
br i1 %51, label %60, label %52
52: ; preds = %48
%53 = add <16 x i32> %11, %50
%54 = shl nsw <16 x i32> %53, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
%55 = zext <16 x i32> %54 to <16 x i64>
%56 = lshr <16 x i64> %13, %55
%57 = trunc <16 x i64> %56 to <16 x i8>
%58 = getelementptr inbounds i8, i8* %2, i64 %49
%59 = bitcast i8* %58 to <16 x i8>*
store <16 x i8> %57, <16 x i8>* %59
br label %60
60: ; preds = %48, %52
%61 = icmp eq i64 %9, %6
br i1 %61, label %76, label %62
62: ; preds = %5, %60
%63 = phi i64 [ 0, %5 ], [ %9, %60 ]
br label %64
64: ; preds = %62, %64
%65 = phi i64 [ %74, %64 ], [ %63, %62 ]
%66 = trunc i64 %65 to i32
%67 = xor i32 %66, -1
%68 = add i32 %67, %1
%69 = shl nsw i32 %68, 3
%70 = zext i32 %69 to i64
%71 = lshr i64 %0, %70
%72 = trunc i64 %71 to i8
%73 = getelementptr inbounds i8, i8* %2, i64 %65
store i8 %72, i8* %73
%74 = add nuw nsw i64 %65, 1
%75 = icmp eq i64 %74, %6
br i1 %75, label %76, label %64
76: ; preds = %64, %60, %3
ret i32 %1
}