unsigned char util_pack8ByteMSBFirst(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 & 0x01;
    packByte = packByte << 1;
    packByte = packByte | dataByte;
    index = index + 1;
  }
  return packByte;
}


x86 assembly (clang, -O0):
<util_pack8ByteMSBFirst>:
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    116a <util_pack8ByteMSBFirst+0x6a>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x1,%eax
mov    %al,-0x11(%rbp)
movzbl -0x12(%rbp),%eax
shl    $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_pack8ByteMSBFirst+0x24>
movzbl -0x12(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_pack8ByteMSBFirst>:
mov    %esi,%esi
mov    (%rdi,%rsi,1),%al
mov    0x1(%rdi,%rsi,1),%dl
and    $0x1,%dl
add    %al,%al
and    $0x2,%al
or     %dl,%al
mov    0x2(%rdi,%rsi,1),%dl
mov    0x3(%rdi,%rsi,1),%cl
and    $0x1,%cl
shl    $0x2,%al
add    %dl,%dl
and    $0x2,%dl
or     %al,%dl
or     %cl,%dl
mov    0x4(%rdi,%rsi,1),%cl
mov    0x5(%rdi,%rsi,1),%al
and    $0x1,%al
shl    $0x2,%dl
add    %cl,%cl
and    $0x2,%cl
or     %dl,%cl
or     %al,%cl
mov    0x6(%rdi,%rsi,1),%al
mov    0x7(%rdi,%rsi,1),%dl
and    $0x1,%dl
shl    $0x2,%cl
add    %al,%al
and    $0x2,%al
or     %cl,%al
or     %dl,%al
ret

LLVM IR (-O0):
define zeroext i8 @util_pack8ByteMSBFirst(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 %38
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 = and i32 %23, 1
%25 = trunc i32 %24 to i8
store i8 %25, i8* %6
%26 = load i8, i8* %7
%27 = zext i8 %26 to i32
%28 = shl i32 %27, 1
%29 = trunc i32 %28 to i8
store i8 %29, i8* %7
%30 = load i8, i8* %7
%31 = zext i8 %30 to i32
%32 = load i8, i8* %6
%33 = zext i8 %32 to i32
%34 = or i32 %31, %33
%35 = trunc i32 %34 to i8
store i8 %35, i8* %7
%36 = load i32, i32* %5
%37 = add i32 %36, 1
store i32 %37, i32* %5
br label %13
38: ; preds = %13
%39 = load i8, i8* %7
ret i8 %39
}

LLVM IR (-O2):
define zeroext i8 @util_pack8ByteMSBFirst(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 = getelementptr inbounds i8, i8* %4, i64 1
%7 = load i8, i8* %6
%8 = and i8 %7, 1
%9 = shl i8 %5, 1
%10 = and i8 %9, 2
%11 = or i8 %8, %10
%12 = getelementptr inbounds i8, i8* %4, i64 2
%13 = load i8, i8* %12
%14 = getelementptr inbounds i8, i8* %4, i64 3
%15 = load i8, i8* %14
%16 = and i8 %15, 1
%17 = shl nuw nsw i8 %11, 2
%18 = shl i8 %13, 1
%19 = and i8 %18, 2
%20 = or i8 %17, %19
%21 = or i8 %16, %20
%22 = getelementptr inbounds i8, i8* %4, i64 4
%23 = load i8, i8* %22
%24 = getelementptr inbounds i8, i8* %4, i64 5
%25 = load i8, i8* %24
%26 = and i8 %25, 1
%27 = shl nuw nsw i8 %21, 2
%28 = shl i8 %23, 1
%29 = and i8 %28, 2
%30 = or i8 %27, %29
%31 = or i8 %26, %30
%32 = getelementptr inbounds i8, i8* %4, i64 6
%33 = load i8, i8* %32
%34 = getelementptr inbounds i8, i8* %4, i64 7
%35 = load i8, i8* %34
%36 = and i8 %35, 1
%37 = shl i8 %31, 2
%38 = shl i8 %33, 1
%39 = and i8 %38, 2
%40 = or i8 %37, %39
%41 = or i8 %36, %40
ret i8 %41
}