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

/* Variables and functions */
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
void write_little_endian(unsigned *counter, unsigned len, FILE *f) {
  unsigned i;
  for (i = 0; i < len; ++i) {
    unsigned v = counter[i];
    unsigned char bytes[4];
    bytes[0] = ((v >> 0) & 0xff);
    bytes[1] = ((v >> 8) & 0xff);
    bytes[2] = ((v >> 16) & 0xff);
    bytes[3] = ((v >> 24) & 0xff);
    fwrite(bytes, 1, 4, f);
  }
}


x86 assembly (clang, -O0):
<write_little_endian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11a0 <write_little_endian+0x90>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
shr    $0x0,%eax
and    $0xff,%eax
mov    %al,-0x24(%rbp)
mov    -0x20(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x23(%rbp)
mov    -0x20(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x22(%rbp)
mov    -0x20(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,-0x21(%rbp)
lea    -0x24(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fwrite@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112a <write_little_endian+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_little_endian>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
je     1166 <write_little_endian+0x56>
mov    %rdx,%r14
mov    %rdi,%r12
mov    %esi,%r13d
xor    %ebx,%ebx
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbx,4),%eax
mov    %eax,0xc(%rsp)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r14,%rcx
call   1030 <fwrite@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1140 <write_little_endian+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @write_little_endian(i32* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = alloca [4 x i8]
%5 = icmp eq i32 %1, 0
br i1 %5, label %26, label %6
6: ; preds = %3
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
%11 = zext i32 %1 to i64
br label %12
12: ; preds = %6, %12
%13 = phi i64 [ 0, %6 ], [ %24, %12 ]
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = trunc i32 %15 to i8
store i8 %16, i8* %7
%17 = lshr i32 %15, 8
%18 = trunc i32 %17 to i8
store i8 %18, i8* %8
%19 = lshr i32 %15, 16
%20 = trunc i32 %19 to i8
store i8 %20, i8* %9
%21 = lshr i32 %15, 24
%22 = trunc i32 %21 to i8
store i8 %22, i8* %10
%23 = call i32 @fwrite(i8* %7, i32 1, i32 4, i32* %2)
%24 = add nuw nsw i64 %13, 1
%25 = icmp eq i64 %24, %11
br i1 %25, label %26, label %12
26: ; preds = %12, %3
ret void
}