unsigned char util_pack8ByteLSBFirst(unsigned char *pByteArray,
                                     unsigned int startPosition) {
  unsigned int index;
  unsigned char dataByte;
  unsigned char packByte;
  unsigned char *pStartAddress;
  index = 0;
  packByte = 0x00;
  pStartAddress = pByteArray + startPosition;
  while (index < 8) {
    dataByte = *(pStartAddress + index);
    dataByte = (dataByte << 7) & 0x80;
    packByte = packByte >> 1;
    packByte = packByte | dataByte;
    index = index + 1;
  }
  return packByte;
}


x86 assembly (clang, -O0):
<util_pack8ByteLSBFirst>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x0,-0x12(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x8,-0x10(%rbp)
jae    116f <util_pack8ByteLSBFirst+0x6f>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
shl    $0x7,%eax
and    $0x80,%eax
mov    %al,-0x11(%rbp)
movzbl -0x12(%rbp),%eax
sar    $0x1,%eax
mov    %al,-0x12(%rbp)
movzbl -0x12(%rbp),%eax
movzbl -0x11(%rbp),%ecx
or     %ecx,%eax
mov    %al,-0x12(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1124 <util_pack8ByteLSBFirst+0x24>
movzbl -0x12(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_pack8ByteLSBFirst>:
mov    %esi,%eax
mov    (%rdi,%rax,1),%cl
mov    0x1(%rdi,%rax,1),%dl
shl    $0x7,%cl
shl    $0x7,%dl
shr    %cl
or     %dl,%cl
mov    0x2(%rdi,%rax,1),%dl
shr    %cl
shl    $0x7,%dl
or     %cl,%dl
mov    0x3(%rdi,%rax,1),%cl
shr    %dl
shl    $0x7,%cl
or     %dl,%cl
mov    0x4(%rdi,%rax,1),%dl
shr    %cl
shl    $0x7,%dl
or     %cl,%dl
mov    0x5(%rdi,%rax,1),%cl
shr    %dl
shl    $0x7,%cl
or     %dl,%cl
mov    0x6(%rdi,%rax,1),%dl
shr    %cl
shl    $0x7,%dl
or     %cl,%dl
mov    0x7(%rdi,%rax,1),%al
shr    %dl
shl    $0x7,%al
or     %dl,%al
ret

LLVM IR (-O0):
define zeroext i8 @util_pack8ByteLSBFirst(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i8 0, i8* %7
%9 = load i8*, i8** %3
%10 = load i32, i32* %4
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
store i8* %12, i8** %8
br label %13
13: ; preds = %16, %2
%14 = load i32, i32* %5
%15 = icmp ult i32 %14, 8
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = load i8*, i8** %8
%18 = load i32, i32* %5
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
store i8 %21, i8* %6
%22 = load i8, i8* %6
%23 = zext i8 %22 to i32
%24 = shl i32 %23, 7
%25 = and i32 %24, 128
%26 = trunc i32 %25 to i8
store i8 %26, i8* %6
%27 = load i8, i8* %7
%28 = zext i8 %27 to i32
%29 = ashr i32 %28, 1
%30 = trunc i32 %29 to i8
store i8 %30, i8* %7
%31 = load i8, i8* %7
%32 = zext i8 %31 to i32
%33 = load i8, i8* %6
%34 = zext i8 %33 to i32
%35 = or i32 %32, %34
%36 = trunc i32 %35 to i8
store i8 %36, i8* %7
%37 = load i32, i32* %5
%38 = add i32 %37, 1
store i32 %38, i32* %5
br label %13
39: ; preds = %13
%40 = load i8, i8* %7
ret i8 %40
}

LLVM IR (-O2):
define zeroext i8 @util_pack8ByteLSBFirst(i8* nocapture %0, i32 %1) {
%3 = zext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
%6 = shl i8 %5, 7
%7 = getelementptr inbounds i8, i8* %4, i64 1
%8 = load i8, i8* %7
%9 = tail call i8 @llvm.fshl.i8(i8 %8, i8 %6, i8 7)
%10 = getelementptr inbounds i8, i8* %4, i64 2
%11 = load i8, i8* %10
%12 = tail call i8 @llvm.fshl.i8(i8 %11, i8 %9, i8 7)
%13 = getelementptr inbounds i8, i8* %4, i64 3
%14 = load i8, i8* %13
%15 = tail call i8 @llvm.fshl.i8(i8 %14, i8 %12, i8 7)
%16 = getelementptr inbounds i8, i8* %4, i64 4
%17 = load i8, i8* %16
%18 = tail call i8 @llvm.fshl.i8(i8 %17, i8 %15, i8 7)
%19 = getelementptr inbounds i8, i8* %4, i64 5
%20 = load i8, i8* %19
%21 = tail call i8 @llvm.fshl.i8(i8 %20, i8 %18, i8 7)
%22 = getelementptr inbounds i8, i8* %4, i64 6
%23 = load i8, i8* %22
%24 = tail call i8 @llvm.fshl.i8(i8 %23, i8 %21, i8 7)
%25 = getelementptr inbounds i8, i8* %4, i64 7
%26 = load i8, i8* %25
%27 = tail call i8 @llvm.fshl.i8(i8 %26, i8 %24, i8 7)
ret i8 %27
}