#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int write_bits_to_file(int /*<<< orphan*/ *, unsigned char *, int,
                       int /*<<< orphan*/);
int create_bit_file(char *path, int *arr, int length) {
  int arr_length = length / 8, status = 0;
  if (length % 8 != 0) {
    arr_length += 1;
  }
  FILE *fp = fopen(path, "wb+");
  if (fp == NULL) {
    return -1;
  }
  unsigned char vec[arr_length];
  for (int i = 0; i < length; i++) {
    if (arr[i] == 0) {
      (vec)[(i) / 8] &= ~(1 << ((i) % 8));
    } else {
      (vec)[(i) / 8] |= (1 << ((i) % 8));
    }
  }
  status = write_bits_to_file(fp, vec, length, 0);
  if (status == -1) {
    fclose(fp);
    return -1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<create_bit_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1175 <create_bit_file+0x45>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a0 <create_bit_file+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <create_bit_file+0x191>
mov    -0x20(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1268 <create_bit_file+0x138>
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jne    1224 <create_bit_file+0xf4>
mov    -0x44(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
mov    $0x1,%esi
shl    %cl,%esi
xor    $0xffffffff,%esi
mov    -0x44(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edx
and    %esi,%edx
mov    %dl,(%rax,%rcx,1)
jmp    1255 <create_bit_file+0x125>
mov    -0x44(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    -0x44(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
jmp    125a <create_bit_file+0x12a>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11cd <create_bit_file+0x9d>
mov    -0x50(%rbp),%rsi
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1050 <write_bits_to_file@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12a3 <create_bit_file+0x173>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    12ba <create_bit_file+0x18a>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_bit_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r13
lea    0xeb5(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     11fe <create_bit_file+0xce>
mov    %rax,%r14
mov    %rsp,%r12
mov    %r15d,%eax
and    $0x7,%eax
lea    0x7(%r15),%ecx
test   %r15d,%r15d
cmovns %r15d,%ecx
sar    $0x3,%ecx
cmp    $0x1,%eax
sbb    $0xffffffff,%ecx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,%rsp
test   %r15d,%r15d
jle    11da <create_bit_file+0xaa>
mov    %r15d,%r8d
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%ecx
and    $0x7,%cl
mov    $0x1,%bl
shl    %cl,%bl
mov    %eax,%edi
shr    $0x3,%edi
movzbl (%rsi,%rdi,1),%r9d
mov    $0xfe,%dl
mov    %eax,%ecx
rol    %cl,%dl
and    %r9b,%dl
or     %r9b,%bl
cmpl   $0x0,0x0(%r13,%rax,4)
movzbl %dl,%ecx
movzbl %bl,%edx
cmove  %ecx,%edx
mov    %dl,(%rsi,%rdi,1)
add    $0x1,%rax
cmp    %rax,%r8
jne    11a0 <create_bit_file+0x70>
mov    %r14,%rdi
mov    %r15d,%edx
xor    %ecx,%ecx
call   1050 <write_bits_to_file@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
sete   %bl
mov    %r14,%rdi
call   1030 <fclose@plt>
neg    %ebx
mov    %r12,%rsp
jmp    1203 <create_bit_file+0xd3>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"wb+\00"
define i32 @create_bit_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%15 = load i32, i32* %7
%16 = sdiv i32 %15, 8
store i32 %16, i32* %8
store i32 0, i32* %9
%17 = load i32, i32* %7
%18 = srem i32 %17, 8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = load i32, i32* %8
%22 = add nsw i32 %21, 1
store i32 %22, i32* %8
br label %23
23: ; preds = %20, %3
%24 = load i8*, i8** %5
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %25, i32** %10
%26 = load i32*, i32** %10
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %4
br label %88
29: ; preds = %23
%30 = load i32, i32* %8
%31 = zext i32 %30 to i64
%32 = call i8* @llvm.stacksave()
store i8* %32, i8** %11
%33 = alloca i8, i64 %31
store i64 %31, i64* %12
store i32 0, i32* %13
br label %34
34: ; preds = %71, %29
%35 = load i32, i32* %13
%36 = load i32, i32* %7
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %74
38: ; preds = %34
%39 = load i32*, i32** %6
%40 = load i32, i32* %13
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %58
45: ; preds = %38
%46 = load i32, i32* %13
%47 = srem i32 %46, 8
%48 = shl i32 1, %47
%49 = xor i32 %48, -1
%50 = load i32, i32* %13
%51 = sdiv i32 %50, 8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %33, i64 %52
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = and i32 %55, %49
%57 = trunc i32 %56 to i8
store i8 %57, i8* %53
br label %70
58: ; preds = %38
%59 = load i32, i32* %13
%60 = srem i32 %59, 8
%61 = shl i32 1, %60
%62 = load i32, i32* %13
%63 = sdiv i32 %62, 8
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %33, i64 %64
%66 = load i8, i8* %65
%67 = zext i8 %66 to i32
%68 = or i32 %67, %61
%69 = trunc i32 %68 to i8
store i8 %69, i8* %65
br label %70
70: ; preds = %58, %45
br label %71
71: ; preds = %70
%72 = load i32, i32* %13
%73 = add nsw i32 %72, 1
store i32 %73, i32* %13
br label %34
74: ; preds = %34
%75 = load i32*, i32** %10
%76 = load i32, i32* %7
%77 = call i32 @write_bits_to_file(i32* %75, i8* %33, i32 %76, i32 0)
store i32 %77, i32* %9
%78 = load i32, i32* %9
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %83
80: ; preds = %74
%81 = load i32*, i32** %10
%82 = call i32 @fclose(i32* %81)
store i32 -1, i32* %4
store i32 1, i32* %14
br label %86
83: ; preds = %74
%84 = load i32*, i32** %10
%85 = call i32 @fclose(i32* %84)
store i32 0, i32* %4
store i32 1, i32* %14
br label %86
86: ; preds = %83, %80
%87 = load i8*, i8** %11
br label %88
88: ; preds = %86, %28
%89 = load i32, i32* %4
ret i32 %89
}

LLVM IR (-O2):
@.str = [4 x i8] c"wb+\00"
define i32 @create_bit_file(i8* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %41, label %6
6: ; preds = %3
%7 = and i32 %2, 7
%8 = icmp ne i32 %7, 0
%9 = sdiv i32 %2, 8
%10 = zext i1 %8 to i32
%11 = add nsw i32 %9, %10
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = icmp sgt i32 %2, 0
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = zext i32 %2 to i64
br label %23
18: ; preds = %23, %6
%19 = call i32 @write_bits_to_file(i32* %4, i8* %14, i32 %2, i32 0)
%20 = icmp eq i32 %19, -1
%21 = call i32 @fclose(i32* %4)
%22 = sext i1 %20 to i32
br label %41
23: ; preds = %16, %23
%24 = phi i64 [ 0, %16 ], [ %39, %23 ]
%25 = getelementptr inbounds i32, i32* %1, i64 %24
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 0
%28 = trunc i64 %24 to i8
%29 = and i8 %28, 7
%30 = shl nuw i8 1, %29
%31 = lshr i64 %24, 3
%32 = and i64 %31, 536870911
%33 = getelementptr inbounds i8, i8* %14, i64 %32
%34 = load i8, i8* %33
%35 = xor i8 %30, -1
%36 = and i8 %34, %35
%37 = or i8 %34, %30
%38 = select i1 %27, i8 %36, i8 %37
store i8 %38, i8* %33
%39 = add nuw nsw i64 %24, 1
%40 = icmp eq i64 %39, %17
br i1 %40, label %18, label %23
41: ; preds = %3, %18
%42 = phi i32 [ %22, %18 ], [ -1, %3 ]
ret i32 %42
}