#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 /*<<< orphan*/ ASN1_F_ASN1_BIT_STRING_SET_BIT;
int ASN1_STRING_FLAG_BITS_LEFT;
int /*<<< orphan*/ ASN1err(int /*<<< orphan*/, 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) {
      ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, 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    130c <ASN1_BIT_STRING_set_bit+0x1dc>
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    1287 <ASN1_BIT_STRING_set_bit+0x157>
cmpl   $0x0,-0x18(%rbp)
jne    11e9 <ASN1_BIT_STRING_set_bit+0xb9>
movl   $0x1,-0x4(%rbp)
jmp    130c <ASN1_BIT_STRING_set_bit+0x1dc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1050 <OPENSSL_realloc_clean@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1235 <ASN1_BIT_STRING_set_bit+0x105>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   1030 <ASN1err@plt>
movl   $0x0,-0x4(%rbp)
jmp    130c <ASN1_BIT_STRING_set_bit+0x1dc>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    -0x10(%rbp),%rcx
sub    0x4(%rcx),%eax
cmp    $0x0,%eax
jle    126e <ASN1_BIT_STRING_set_bit+0x13e>
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   1040 <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    12e3 <ASN1_BIT_STRING_set_bit+0x1b3>
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    12f3 <ASN1_BIT_STRING_set_bit+0x1c3>
jmp    1305 <ASN1_BIT_STRING_set_bit+0x1d5>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x4(%rax)
jmp    12ae <ASN1_BIT_STRING_set_bit+0x17e>
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     1240 <ASN1_BIT_STRING_set_bit+0x110>
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   1050 <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   1040 <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    1240 <ASN1_BIT_STRING_set_bit+0x110>
mov    0x8(%rbx),%rcx
nop
lea    -0x1(%rax),%rdx
mov    %edx,%esi
cmpb   $0x0,(%rcx,%rsi,1)
jne    1240 <ASN1_BIT_STRING_set_bit+0x110>
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    1240 <ASN1_BIT_STRING_set_bit+0x110>
mov    $0x1,%r14d
jmp    1240 <ASN1_BIT_STRING_set_bit+0x110>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1030 <ASN1err@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
@ASN1_F_ASN1_BIT_STRING_SET_BIT = 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 %142
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 %96
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 %142
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 %66
62: ; preds = %50
%63 = load i32, i32* @ASN1_F_ASN1_BIT_STRING_SET_BIT
%64 = load i32, i32* @ERR_R_MALLOC_FAILURE
%65 = call i32 @ASN1err(i32 %63, i32 %64)
store i32 0, i32* %4
br label %142
66: ; preds = %50
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 1
%69 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%70 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %69, i32 0, i32 1
%71 = load i32, i32* %70
%72 = sub nsw i32 %68, %71
%73 = icmp sgt i32 %72, 0
br i1 %73, label %74, label %88
74: ; preds = %66
%75 = load i8*, i8** %11
%76 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%77 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %76, i32 0, i32 1
%78 = load i32, i32* %77
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %75, i64 %79
%81 = load i32, i32* %8
%82 = add nsw i32 %81, 1
%83 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%84 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %83, i32 0, i32 1
%85 = load i32, i32* %84
%86 = sub nsw i32 %82, %85
%87 = call i32 @memset(i8* %80, i32 0, i32 %86)
br label %88
88: ; preds = %74, %66
%89 = load i8*, i8** %11
%90 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%91 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %90, i32 0, i32 2
store i8* %89, i8** %91
%92 = load i32, i32* %8
%93 = add nsw i32 %92, 1
%94 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%95 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %94, i32 0, i32 1
store i32 %93, i32* %95
br label %96
96: ; preds = %88, %41
%97 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%98 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %97, i32 0, i32 2
%99 = load i8*, i8** %98
%100 = load i32, i32* %8
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8, i8* %99, i64 %101
%103 = load i8, i8* %102
%104 = zext i8 %103 to i32
%105 = load i32, i32* %10
%106 = and i32 %104, %105
%107 = load i32, i32* %9
%108 = or i32 %106, %107
%109 = trunc i32 %108 to i8
%110 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%111 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %110, i32 0, i32 2
%112 = load i8*, i8** %111
%113 = load i32, i32* %8
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds i8, i8* %112, i64 %114
store i8 %109, i8* %115
br label %116
116: ; preds = %136, %96
%117 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%118 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %117, i32 0, i32 1
%119 = load i32, i32* %118
%120 = icmp sgt i32 %119, 0
br i1 %120, label %121, label %134
121: ; preds = %116
%122 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%123 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %122, i32 0, i32 2
%124 = load i8*, i8** %123
%125 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%126 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %125, i32 0, i32 1
%127 = load i32, i32* %126
%128 = sub nsw i32 %127, 1
%129 = sext i32 %128 to i64
%130 = getelementptr inbounds i8, i8* %124, i64 %129
%131 = load i8, i8* %130
%132 = zext i8 %131 to i32
%133 = icmp eq i32 %132, 0
br label %134
134: ; preds = %121, %116
%135 = phi i1 [ false, %116 ], [ %133, %121 ]
br i1 %135, label %136, label %141
136: ; preds = %134
%137 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%138 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %137, i32 0, i32 1
%139 = load i32, i32* %138
%140 = add nsw i32 %139, -1
store i32 %140, i32* %138
br label %116
141: ; preds = %134
store i32 1, i32* %4
br label %142
142: ; preds = %141, %62, %49, %26
%143 = load i32, i32* %4
ret i32 %143
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32, i8* }
@ASN1_STRING_FLAG_BITS_LEFT = global i32 0
@ASN1_F_ASN1_BIT_STRING_SET_BIT = 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 %71, 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 %46
27: ; preds = %23, %12
br i1 %9, label %71, 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 %37
33: ; preds = %28
%34 = load i32, i32* @ASN1_F_ASN1_BIT_STRING_SET_BIT
%35 = load i32, i32* @ERR_R_MALLOC_FAILURE
%36 = tail call i32 @ASN1err(i32 %34, i32 %35)
br label %71
37: ; preds = %28
%38 = load i32, i32* %19
%39 = sub nsw i32 %21, %38
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = sext i32 %38 to i64
%43 = getelementptr inbounds i8, i8* %31, i64 %42
%44 = tail call i32 @memset(i8* %43, i32 0, i32 %39)
br label %45
45: ; preds = %41, %37
store i8* %31, i8** %29
store i32 %21, i32* %19
br label %46
46: ; preds = %45, %23
%47 = phi i8* [ %31, %45 ], [ %25, %23 ]
%48 = sext i32 %4 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = and i32 %51, %8
%53 = or i32 %52, %10
%54 = trunc i32 %53 to i8
store i8 %54, i8* %49
%55 = load i32, i32* %19
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %71
57: ; preds = %46
%58 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %0, i64 0, i32 2
%59 = load i8*, i8** %58
%60 = zext i32 %55 to i64
br label %61
61: ; preds = %57, %68
%62 = phi i64 [ %60, %57 ], [ %63, %68 ]
%63 = add nsw i64 %62, -1
%64 = and i64 %63, 4294967295
%65 = getelementptr inbounds i8, i8* %59, i64 %64
%66 = load i8, i8* %65
%67 = icmp eq i8 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %61
%69 = trunc i64 %63 to i32
store i32 %69, i32* %19
%70 = icmp ugt i64 %62, 1
br i1 %70, label %61, label %71
71: ; preds = %68, %61, %46, %27, %3, %33
%72 = phi i32 [ 0, %33 ], [ 0, %3 ], [ 1, %27 ], [ 1, %46 ], [ 1, %61 ], [ 1, %68 ]
ret i32 %72
}