#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int flags;
  int length;
  unsigned char *data;
};
typedef TYPE_1__ ASN1_BIT_STRING;

/* Variables and functions */
int ASN1_STRING_FLAG_BITS_LEFT;
int /*<<< orphan*/ ASN1error(int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
unsigned char *OPENSSL_realloc_clean(unsigned char *, int, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) {
  int w, v, iv;
  unsigned char *c;
  w = n / 8;
  v = 1 << (7 - (n & 0x07));
  iv = ~v;
  if (!value)
    v = 0;
  if (a == NULL)
    return 0;
  a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  if ((a->length < (w + 1)) || (a->data == NULL)) {
    if (!value)
      return (1);
    c = OPENSSL_realloc_clean(a->data, a->length, w + 1);
    if (c == NULL) {
      ASN1error(ERR_R_MALLOC_FAILURE);
      return 0;
    }
    if (w + 1 - a->length > 0)
      memset(c + a->length, 0, w + 1 - a->length);
    a->data = c;
    a->length = w + 1;
  }
  a->data[w] = ((a->data[w]) & iv) | v;
  while ((a->length > 0) && (a->data[a->length - 1] == 0))
    a->length--;
  return (1);
}


x86 assembly (clang, -O0):
<ASN1_BIT_STRING_set_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
and    $0x7,%eax
mov    $0x7,%ecx
sub    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1181 <ASN1_BIT_STRING_set_bit+0x51>
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1198 <ASN1_BIT_STRING_set_bit+0x68>
movl   $0x0,-0x4(%rbp)
jmp    1303 <ASN1_BIT_STRING_set_bit+0x1d3>
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
or     $0x7,%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jl     11d3 <ASN1_BIT_STRING_set_bit+0xa3>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    127e <ASN1_BIT_STRING_set_bit+0x14e>
cmpl   $0x0,-0x18(%rbp)
jne    11e9 <ASN1_BIT_STRING_set_bit+0xb9>
movl   $0x1,-0x4(%rbp)
jmp    1303 <ASN1_BIT_STRING_set_bit+0x1d3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1040 <OPENSSL_realloc_clean@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122c <ASN1_BIT_STRING_set_bit+0xfc>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1050 <ASN1error@plt>
movl   $0x0,-0x4(%rbp)
jmp    1303 <ASN1_BIT_STRING_set_bit+0x1d3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    -0x10(%rbp),%rcx
sub    0x4(%rcx),%eax
cmp    $0x0,%eax
jle    1265 <ASN1_BIT_STRING_set_bit+0x135>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq 0x4(%rax),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
mov    -0x10(%rbp),%rax
sub    0x4(%rax),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    -0x24(%rbp),%eax
or     -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmpl   $0x0,0x4(%rcx)
mov    %al,-0x31(%rbp)
jle    12da <ASN1_BIT_STRING_set_bit+0x1aa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x10(%rbp),%rcx
mov    0x4(%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    12ea <ASN1_BIT_STRING_set_bit+0x1ba>
jmp    12fc <ASN1_BIT_STRING_set_bit+0x1cc>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x4(%rax)
jmp    12a5 <ASN1_BIT_STRING_set_bit+0x175>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ASN1_BIT_STRING_set_bit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ecx
lea    0x7(%rcx),%r13d
test   %esi,%esi
cmovns %esi,%r13d
not    %cl
and    $0x7,%cl
mov    $0x1,%ebp
shl    %cl,%ebp
mov    $0xfffffffe,%r15d
rol    %cl,%r15d
test   %edx,%edx
cmove  %edx,%ebp
xor    %r14d,%r14d
test   %rdi,%rdi
je     1237 <ASN1_BIT_STRING_set_bit+0x107>
mov    %rdi,%rbx
sar    $0x3,%r13d
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
not    %eax
and    (%rdi),%eax
and    $0xfffffff8,%eax
mov    %eax,(%rdi)
mov    0x4(%rdi),%esi
cmp    %r13d,%esi
jle    1197 <ASN1_BIT_STRING_set_bit+0x67>
mov    0x8(%rbx),%r12
test   %r12,%r12
jne    11dd <ASN1_BIT_STRING_set_bit+0xad>
test   %edx,%edx
je     1221 <ASN1_BIT_STRING_set_bit+0xf1>
lea    0x1(%r13),%edx
mov    0x8(%rbx),%rdi
mov    %edx,0x4(%rsp)
call   1040 <OPENSSL_realloc_clean@plt>
test   %rax,%rax
je     1229 <ASN1_BIT_STRING_set_bit+0xf9>
mov    %rax,%r12
movslq 0x4(%rbx),%rax
mov    0x4(%rsp),%r14d
mov    %r14d,%edx
sub    %eax,%edx
jle    11d5 <ASN1_BIT_STRING_set_bit+0xa5>
mov    %r12,%rdi
add    %rax,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    %r12,0x8(%rbx)
mov    %r14d,0x4(%rbx)
movslq %r13d,%rax
movzbl (%r12,%rax,1),%ecx
and    %r15d,%ecx
or     %ebp,%ecx
mov    %cl,(%r12,%rax,1)
mov    0x4(%rbx),%eax
mov    $0x1,%r14d
test   %eax,%eax
jle    1237 <ASN1_BIT_STRING_set_bit+0x107>
mov    0x8(%rbx),%rcx
nop
lea    -0x1(%rax),%rdx
mov    %edx,%esi
cmpb   $0x0,(%rcx,%rsi,1)
jne    1237 <ASN1_BIT_STRING_set_bit+0x107>
add    $0xffffffff,%eax
mov    %eax,0x4(%rbx)
lea    0x1(%rdx),%rsi
mov    %rdx,%rax
cmp    $0x1,%rsi
ja     1200 <ASN1_BIT_STRING_set_bit+0xd0>
jmp    1237 <ASN1_BIT_STRING_set_bit+0x107>
mov    $0x1,%r14d
jmp    1237 <ASN1_BIT_STRING_set_bit+0x107>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1050 <ASN1error@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32, i8* }
@ASN1_STRING_FLAG_BITS_LEFT = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @ASN1_BIT_STRING_set_bit(%struct.TYPE_3__* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store %struct.TYPE_3__* %0, %struct.TYPE_3__** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* %6
%13 = sdiv i32 %12, 8
store i32 %13, i32* %8
%14 = load i32, i32* %6
%15 = and i32 %14, 7
%16 = sub nsw i32 7, %15
%17 = shl i32 1, %16
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = xor i32 %18, -1
store i32 %19, i32* %10
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %3
store i32 0, i32* %9
br label %23
23: ; preds = %22, %3
%24 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%25 = icmp eq %struct.TYPE_3__* %24, null
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 0, i32* %4
br label %141
27: ; preds = %23
%28 = load i32, i32* @ASN1_STRING_FLAG_BITS_LEFT
%29 = or i32 %28, 7
%30 = xor i32 %29, -1
%31 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%32 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %33, %30
store i32 %34, i32* %32
%35 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%36 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
%40 = icmp slt i32 %37, %39
br i1 %40, label %46, label %41
41: ; preds = %27
%42 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %42, i32 0, i32 2
%44 = load i8*, i8** %43
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %95
46: ; preds = %41, %27
%47 = load i32, i32* %7
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %46
store i32 1, i32* %4
br label %141
50: ; preds = %46
%51 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%52 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %51, i32 0, i32 2
%53 = load i8*, i8** %52
%54 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%55 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
%59 = call i8* @OPENSSL_realloc_clean(i8* %53, i32 %56, i32 %58)
store i8* %59, i8** %11
%60 = load i8*, i8** %11
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %65
62: ; preds = %50
%63 = load i32, i32* @ERR_R_MALLOC_FAILURE
%64 = call i32 @ASN1error(i32 %63)
store i32 0, i32* %4
br label %141
65: ; preds = %50
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
%68 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%69 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %68, i32 0, i32 1
%70 = load i32, i32* %69
%71 = sub nsw i32 %67, %70
%72 = icmp sgt i32 %71, 0
br i1 %72, label %73, label %87
73: ; preds = %65
%74 = load i8*, i8** %11
%75 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%76 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %75, i32 0, i32 1
%77 = load i32, i32* %76
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %74, i64 %78
%80 = load i32, i32* %8
%81 = add nsw i32 %80, 1
%82 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%83 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %82, i32 0, i32 1
%84 = load i32, i32* %83
%85 = sub nsw i32 %81, %84
%86 = call i32 @memset(i8* %79, i32 0, i32 %85)
br label %87
87: ; preds = %73, %65
%88 = load i8*, i8** %11
%89 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%90 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %89, i32 0, i32 2
store i8* %88, i8** %90
%91 = load i32, i32* %8
%92 = add nsw i32 %91, 1
%93 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%94 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %93, i32 0, i32 1
store i32 %92, i32* %94
br label %95
95: ; preds = %87, %41
%96 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%97 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %96, i32 0, i32 2
%98 = load i8*, i8** %97
%99 = load i32, i32* %8
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %98, i64 %100
%102 = load i8, i8* %101
%103 = zext i8 %102 to i32
%104 = load i32, i32* %10
%105 = and i32 %103, %104
%106 = load i32, i32* %9
%107 = or i32 %105, %106
%108 = trunc i32 %107 to i8
%109 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%110 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %109, i32 0, i32 2
%111 = load i8*, i8** %110
%112 = load i32, i32* %8
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds i8, i8* %111, i64 %113
store i8 %108, i8* %114
br label %115
115: ; preds = %135, %95
%116 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%117 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %116, i32 0, i32 1
%118 = load i32, i32* %117
%119 = icmp sgt i32 %118, 0
br i1 %119, label %120, label %133
120: ; preds = %115
%121 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%122 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %121, i32 0, i32 2
%123 = load i8*, i8** %122
%124 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%125 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %124, i32 0, i32 1
%126 = load i32, i32* %125
%127 = sub nsw i32 %126, 1
%128 = sext i32 %127 to i64
%129 = getelementptr inbounds i8, i8* %123, i64 %128
%130 = load i8, i8* %129
%131 = zext i8 %130 to i32
%132 = icmp eq i32 %131, 0
br label %133
133: ; preds = %120, %115
%134 = phi i1 [ false, %115 ], [ %132, %120 ]
br i1 %134, label %135, label %140
135: ; preds = %133
%136 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%137 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %136, i32 0, i32 1
%138 = load i32, i32* %137
%139 = add nsw i32 %138, -1
store i32 %139, i32* %137
br label %115
140: ; preds = %133
store i32 1, i32* %4
br label %141
141: ; preds = %140, %62, %49, %26
%142 = load i32, i32* %4
ret i32 %142
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32, i8* }
@ASN1_STRING_FLAG_BITS_LEFT = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @ASN1_BIT_STRING_set_bit(%struct.TYPE_3__* %0, i32 %1, i32 %2) {
%4 = sdiv i32 %1, 8
%5 = and i32 %1, 7
%6 = xor i32 %5, 7
%7 = shl nuw i32 1, %6
%8 = xor i32 %7, -1
%9 = icmp eq i32 %2, 0
%10 = select i1 %9, i32 0, i32 %7
%11 = icmp eq %struct.TYPE_3__* %0, null
br i1 %11, label %70, label %12
12: ; preds = %3
%13 = load i32, i32* @ASN1_STRING_FLAG_BITS_LEFT
%14 = or i32 %13, 7
%15 = xor i32 %14, -1
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, %15
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 1
%20 = load i32, i32* %19
%21 = add nsw i32 %4, 1
%22 = icmp sgt i32 %20, %4
br i1 %22, label %23, label %27
23: ; preds = %12
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 2
%25 = load i8*, i8** %24
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %45
27: ; preds = %23, %12
br i1 %9, label %70, label %28
28: ; preds = %27
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 2
%30 = load i8*, i8** %29
%31 = tail call i8* @OPENSSL_realloc_clean(i8* %30, i32 %20, i32 %21)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @ERR_R_MALLOC_FAILURE
%35 = tail call i32 @ASN1error(i32 %34)
br label %70
36: ; preds = %28
%37 = load i32, i32* %19
%38 = sub nsw i32 %21, %37
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = sext i32 %37 to i64
%42 = getelementptr inbounds i8, i8* %31, i64 %41
%43 = tail call i32 @memset(i8* %42, i32 0, i32 %38)
br label %44
44: ; preds = %40, %36
store i8* %31, i8** %29
store i32 %21, i32* %19
br label %45
45: ; preds = %44, %23
%46 = phi i8* [ %31, %44 ], [ %25, %23 ]
%47 = sext i32 %4 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = and i32 %50, %8
%52 = or i32 %51, %10
%53 = trunc i32 %52 to i8
store i8 %53, i8* %48
%54 = load i32, i32* %19
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %70
56: ; preds = %45
%57 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 2
%58 = load i8*, i8** %57
%59 = zext i32 %54 to i64
br label %60
60: ; preds = %56, %67
%61 = phi i64 [ %59, %56 ], [ %62, %67 ]
%62 = add nsw i64 %61, -1
%63 = and i64 %62, 4294967295
%64 = getelementptr inbounds i8, i8* %58, i64 %63
%65 = load i8, i8* %64
%66 = icmp eq i8 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %60
%68 = trunc i64 %62 to i32
store i32 %68, i32* %19
%69 = icmp ugt i64 %61, 1
br i1 %69, label %60, label %70
70: ; preds = %67, %60, %45, %27, %3, %33
%71 = phi i32 [ 0, %33 ], [ 0, %3 ], [ 1, %27 ], [ 1, %45 ], [ 1, %60 ], [ 1, %67 ]
ret i32 %71
}