__attribute__((visibility("hidden"))) int bin_append_posn(const int arg,
                                                          const int length,
                                                          char *binary,
                                                          const int bin_posn) {
  int i;
  int start;
  start = 0x01 << (length - 1);
  for (i = 0; i < length; i++) {
    if (arg & (start >> i)) {
      binary[bin_posn + i] = '1';
    } else {
      binary[bin_posn + i] = '0';
    }
  }
  return bin_posn + length;
}


x86 assembly (clang, -O0):
<bin_append_posn>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1186 <bin_append_posn+0x86>
mov    -0x4(%rbp),%eax
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
sar    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1162 <bin_append_posn+0x62>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    -0x18(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x31,(%rax,%rcx,1)
jmp    1173 <bin_append_posn+0x73>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    -0x18(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x30,(%rax,%rcx,1)
jmp    1178 <bin_append_posn+0x78>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1128 <bin_append_posn+0x28>
mov    -0x14(%rbp),%eax
add    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin_append_posn>:
push   %r15
push   %r14
push   %rbx
mov    %ecx,%eax
lea    -0x1(%rsi),%ecx
mov    $0x1,%r8d
shl    %cl,%r8d
test   %esi,%esi
jle    166b <bin_append_posn+0x56b>
movslq %eax,%r10
mov    %esi,%r9d
cmp    $0x10,%esi
jae    112d <bin_append_posn+0x2d>
xor    %ecx,%ecx
jmp    1640 <bin_append_posn+0x540>
mov    %r9d,%ecx
and    $0xfffffff0,%ecx
movd   %r8d,%xmm0
pshufd $0x0,%xmm0,%xmm8
movd   %edi,%xmm0
pshufd $0x0,%xmm0,%xmm12
lea    -0x10(%rcx),%rbx
mov    %rbx,%r14
shr    $0x4,%r14
add    $0x1,%r14
test   %rbx,%rbx
je     1673 <bin_append_posn+0x573>
mov    %r14,%rbx
and    $0xfffffffffffffffe,%rbx
lea    (%r10,%rdx,1),%r15
add    $0x10,%r15
movdqa 0xe88(%rip),%xmm13
movdqa 0xe8f(%rip),%xmm14
movdqa 0xe96(%rip),%xmm15
movdqa 0xe9e(%rip),%xmm5
xor    %r11d,%r11d
pxor   %xmm11,%xmm11
movdqa 0xebd(%rip),%xmm10
movdqa 0xe94(%rip),%xmm9
nopl   0x0(%rax)
pshuflw $0xfe,%xmm5,%xmm0
movdqa %xmm8,%xmm2
psrad  %xmm0,%xmm2
pshuflw $0x54,%xmm5,%xmm0
movdqa %xmm8,%xmm1
psrad  %xmm0,%xmm1
punpcklqdq %xmm2,%xmm1
pshufd $0xee,%xmm5,%xmm0
pshuflw $0xfe,%xmm0,%xmm2
movdqa %xmm8,%xmm3
psrad  %xmm2,%xmm3
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm8,%xmm2
psrad  %xmm0,%xmm2
punpckhqdq %xmm3,%xmm2
shufps $0xcc,%xmm2,%xmm1
pshuflw $0xfe,%xmm15,%xmm0
movdqa %xmm8,%xmm3
psrad  %xmm0,%xmm3
pshuflw $0x54,%xmm15,%xmm0
movdqa %xmm8,%xmm2
psrad  %xmm0,%xmm2
punpcklqdq %xmm3,%xmm2
pshufd $0xee,%xmm15,%xmm0
pshuflw $0xfe,%xmm0,%xmm3
movdqa %xmm8,%xmm6
psrad  %xmm3,%xmm6
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm8,%xmm3
psrad  %xmm0,%xmm3
punpckhqdq %xmm6,%xmm3
shufps $0xcc,%xmm3,%xmm2
pshuflw $0xfe,%xmm14,%xmm0
movdqa %xmm8,%xmm3
psrad  %xmm0,%xmm3
pshuflw $0x54,%xmm14,%xmm0
movdqa %xmm8,%xmm6
psrad  %xmm0,%xmm6
punpcklqdq %xmm3,%xmm6
pshufd $0xee,%xmm14,%xmm0
pshuflw $0xfe,%xmm0,%xmm3
movdqa %xmm8,%xmm4
psrad  %xmm3,%xmm4
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm8,%xmm3
psrad  %xmm0,%xmm3
punpckhqdq %xmm4,%xmm3
shufps $0xcc,%xmm3,%xmm6
pshuflw $0xfe,%xmm13,%xmm0
movdqa %xmm8,%xmm3
psrad  %xmm0,%xmm3
pshuflw $0x54,%xmm13,%xmm4
movdqa %xmm8,%xmm0
psrad  %xmm4,%xmm0
punpcklqdq %xmm3,%xmm0
pshufd $0xee,%xmm13,%xmm3
pshuflw $0xfe,%xmm3,%xmm4
movdqa %xmm8,%xmm7
psrad  %xmm4,%xmm7
pshuflw $0x54,%xmm3,%xmm3
movdqa %xmm8,%xmm4
psrad  %xmm3,%xmm4
punpckhqdq %xmm7,%xmm4
shufps $0xcc,%xmm4,%xmm0
andps  %xmm12,%xmm0
andps  %xmm12,%xmm6
andps  %xmm12,%xmm2
andps  %xmm12,%xmm1
pcmpeqd %xmm11,%xmm1
pcmpeqd %xmm11,%xmm2
packssdw %xmm2,%xmm1
pcmpeqd %xmm11,%xmm6
pcmpeqd %xmm11,%xmm0
packssdw %xmm0,%xmm6
packsswb %xmm6,%xmm1
paddb  %xmm9,%xmm1
movdqu %xmm1,-0x10(%r15,%r11,1)
movdqa %xmm13,%xmm2
movdqa 0xd2a(%rip),%xmm1
paddd  %xmm1,%xmm2
movdqa %xmm14,%xmm3
paddd  %xmm1,%xmm3
movdqa %xmm15,%xmm0
paddd  %xmm1,%xmm0
movdqa %xmm5,%xmm4
paddd  %xmm1,%xmm4
pshuflw $0xfe,%xmm4,%xmm1
movdqa %xmm8,%xmm6
psrad  %xmm1,%xmm6
pshuflw $0x54,%xmm4,%xmm7
movdqa %xmm8,%xmm1
psrad  %xmm7,%xmm1
punpcklqdq %xmm6,%xmm1
pshufd $0xee,%xmm4,%xmm4
pshuflw $0xfe,%xmm4,%xmm6
movdqa %xmm8,%xmm7
psrad  %xmm6,%xmm7
pshuflw $0x54,%xmm4,%xmm4
movdqa %xmm8,%xmm6
psrad  %xmm4,%xmm6
punpckhqdq %xmm7,%xmm6
shufps $0xcc,%xmm6,%xmm1
pshuflw $0xfe,%xmm0,%xmm4
movdqa %xmm8,%xmm7
psrad  %xmm4,%xmm7
pshuflw $0x54,%xmm0,%xmm4
movdqa %xmm8,%xmm6
psrad  %xmm4,%xmm6
punpcklqdq %xmm7,%xmm6
pshufd $0xee,%xmm0,%xmm0
pshuflw $0xfe,%xmm0,%xmm4
movdqa %xmm8,%xmm7
psrad  %xmm4,%xmm7
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm8,%xmm4
psrad  %xmm0,%xmm4
punpckhqdq %xmm7,%xmm4
shufps $0xcc,%xmm4,%xmm6
pshuflw $0xfe,%xmm3,%xmm0
movdqa %xmm8,%xmm4
psrad  %xmm0,%xmm4
pshuflw $0x54,%xmm3,%xmm7
movdqa %xmm8,%xmm0
psrad  %xmm7,%xmm0
punpcklqdq %xmm4,%xmm0
pshufd $0xee,%xmm3,%xmm3
pshuflw $0xfe,%xmm3,%xmm4
movdqa %xmm8,%xmm7
psrad  %xmm4,%xmm7
pshuflw $0x54,%xmm3,%xmm3
movdqa %xmm8,%xmm4
psrad  %xmm3,%xmm4
punpckhqdq %xmm7,%xmm4
shufps $0xcc,%xmm4,%xmm0
pshuflw $0xfe,%xmm2,%xmm3
movdqa %xmm8,%xmm4
psrad  %xmm3,%xmm4
pshuflw $0x54,%xmm2,%xmm7
movdqa %xmm8,%xmm3
psrad  %xmm7,%xmm3
punpcklqdq %xmm4,%xmm3
pshufd $0xee,%xmm2,%xmm2
pshuflw $0xfe,%xmm2,%xmm4
movdqa %xmm8,%xmm7
psrad  %xmm4,%xmm7
pshuflw $0x54,%xmm2,%xmm2
movdqa %xmm8,%xmm4
psrad  %xmm2,%xmm4
punpckhqdq %xmm7,%xmm4
shufps $0xcc,%xmm4,%xmm3
andps  %xmm12,%xmm6
andps  %xmm12,%xmm1
pcmpeqd %xmm11,%xmm1
pcmpeqd %xmm11,%xmm6
packssdw %xmm6,%xmm1
andps  %xmm12,%xmm3
andps  %xmm12,%xmm0
pcmpeqd %xmm11,%xmm0
pcmpeqd %xmm11,%xmm3
packssdw %xmm3,%xmm0
packsswb %xmm0,%xmm1
paddb  %xmm9,%xmm1
movdqu %xmm1,(%r15,%r11,1)
add    $0x20,%r11
paddd  %xmm10,%xmm5
paddd  %xmm10,%xmm15
paddd  %xmm10,%xmm14
paddd  %xmm10,%xmm13
add    $0xfffffffffffffffe,%rbx
jne    11b0 <bin_append_posn+0xb0>
test   $0x1,%r14b
je     163b <bin_append_posn+0x53b>
pshuflw $0xfe,%xmm5,%xmm0
movdqa %xmm8,%xmm2
psrad  %xmm0,%xmm2
pshuflw $0x54,%xmm5,%xmm0
movdqa %xmm8,%xmm1
psrad  %xmm0,%xmm1
punpcklqdq %xmm2,%xmm1
pshufd $0xee,%xmm5,%xmm0
pshuflw $0xfe,%xmm0,%xmm2
movdqa %xmm8,%xmm3
psrad  %xmm2,%xmm3
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm8,%xmm2
psrad  %xmm0,%xmm2
punpckhqdq %xmm3,%xmm2
shufps $0xcc,%xmm2,%xmm1
pshuflw $0xfe,%xmm15,%xmm0
movdqa %xmm8,%xmm3
psrad  %xmm0,%xmm3
pshuflw $0x54,%xmm15,%xmm0
movdqa %xmm8,%xmm2
psrad  %xmm0,%xmm2
punpcklqdq %xmm3,%xmm2
pshufd $0xee,%xmm15,%xmm0
pshuflw $0xfe,%xmm0,%xmm3
movdqa %xmm8,%xmm4
psrad  %xmm3,%xmm4
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm8,%xmm3
psrad  %xmm0,%xmm3
punpckhqdq %xmm4,%xmm3
shufps $0xcc,%xmm3,%xmm2
pshuflw $0xfe,%xmm14,%xmm0
movdqa %xmm8,%xmm3
psrad  %xmm0,%xmm3
pshuflw $0x54,%xmm14,%xmm4
movdqa %xmm8,%xmm0
psrad  %xmm4,%xmm0
punpcklqdq %xmm3,%xmm0
pshufd $0xee,%xmm14,%xmm3
pshuflw $0xfe,%xmm3,%xmm4
movdqa %xmm8,%xmm5
psrad  %xmm4,%xmm5
pshuflw $0x54,%xmm3,%xmm3
movdqa %xmm8,%xmm4
psrad  %xmm3,%xmm4
punpckhqdq %xmm5,%xmm4
shufps $0xcc,%xmm4,%xmm0
pshuflw $0xfe,%xmm13,%xmm3
movdqa %xmm8,%xmm4
psrad  %xmm3,%xmm4
pshuflw $0x54,%xmm13,%xmm5
movdqa %xmm8,%xmm3
psrad  %xmm5,%xmm3
punpcklqdq %xmm4,%xmm3
pshufd $0xee,%xmm13,%xmm4
pshuflw $0xfe,%xmm4,%xmm5
movdqa %xmm8,%xmm6
psrad  %xmm5,%xmm6
pshuflw $0x54,%xmm4,%xmm4
psrad  %xmm4,%xmm8
punpckhqdq %xmm6,%xmm8
shufps $0xcc,%xmm8,%xmm3
andps  %xmm12,%xmm3
andps  %xmm12,%xmm0
andps  %xmm12,%xmm2
andps  %xmm12,%xmm1
pxor   %xmm4,%xmm4
add    %r10,%r11
pcmpeqd %xmm4,%xmm1
pcmpeqd %xmm4,%xmm2
packssdw %xmm2,%xmm1
pcmpeqd %xmm4,%xmm0
pcmpeqd %xmm4,%xmm3
packssdw %xmm3,%xmm0
packsswb %xmm0,%xmm1
paddb  0xa0b(%rip),%xmm1
movdqu %xmm1,(%rdx,%r11,1)
cmp    %r9,%rcx
je     166b <bin_append_posn+0x56b>
add    %r10,%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r8d,%ebx
sar    %cl,%ebx
and    %edi,%ebx
cmp    $0x1,%ebx
mov    $0x31,%bl
sbb    $0x0,%bl
mov    %bl,(%rdx,%rcx,1)
add    $0x1,%rcx
cmp    %rcx,%r9
jne    1650 <bin_append_posn+0x550>
add    %esi,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
movdqa 0x984(%rip),%xmm13
movdqa 0x98b(%rip),%xmm14
movdqa 0x992(%rip),%xmm15
movdqa 0x99a(%rip),%xmm5
xor    %r11d,%r11d
test   $0x1,%r14b
jne    14cf <bin_append_posn+0x3cf>
jmp    163b <bin_append_posn+0x53b>

LLVM IR (-O0):
define hidden i32 @bin_append_posn(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 1
%13 = shl i32 1, %12
store i32 %13, i32* %10
store i32 0, i32* %9
br label %14
14: ; preds = %40, %4
%15 = load i32, i32* %9
%16 = load i32, i32* %6
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %43
18: ; preds = %14
%19 = load i32, i32* %5
%20 = load i32, i32* %10
%21 = load i32, i32* %9
%22 = ashr i32 %20, %21
%23 = and i32 %19, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %18
%26 = load i8*, i8** %7
%27 = load i32, i32* %8
%28 = load i32, i32* %9
%29 = add nsw i32 %27, %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
store i8 49, i8* %31
br label %39
32: ; preds = %18
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = load i32, i32* %9
%36 = add nsw i32 %34, %35
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %33, i64 %37
store i8 48, i8* %38
br label %39
39: ; preds = %32, %25
br label %40
40: ; preds = %39
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %14
43: ; preds = %14
%44 = load i32, i32* %8
%45 = load i32, i32* %6
%46 = add nsw i32 %44, %45
ret i32 %46
}

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