#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*/ BIO;
typedef int /*<<< orphan*/ ASN1_ITEM;

/* Variables and functions */
int /*<<< orphan*/ ASN1_F_ASN1_ITEM_I2D_BIO;
int ASN1_item_i2d(void *, unsigned char **, int /*<<< orphan*/ const *);
int /*<<< orphan*/ ASN1err(int /*<<< orphan*/, int /*<<< orphan*/);
int BIO_write(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ free(unsigned char *);
int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x) {
  unsigned char *b = NULL;
  int i, j = 0, n, ret = 1;
  n = ASN1_item_i2d(x, &b, it);
  if (b == NULL) {
    ASN1err(ASN1_F_ASN1_ITEM_I2D_BIO, ERR_R_MALLOC_FAILURE);
    return (0);
  }
  for (;;) {
    i = BIO_write(out, &(b[j]), n);
    if (i == n)
      break;
    if (i <= 0) {
      ret = 0;
      break;
    }
    j += i;
    n -= i;
  }
  free(b);
  return (ret);
}


x86 assembly (clang, -O0):
<ASN1_item_i2d_bio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rsi
call   1060 <ASN1_item_i2d@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ac <ASN1_item_i2d_bio+0x6c>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
movl   $0x0,-0x4(%rbp)
jmp    121a <ASN1_item_i2d_bio+0xda>
jmp    11b1 <ASN1_item_i2d_bio+0x71>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
call   1050 <BIO_write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jne    11dc <ASN1_item_i2d_bio+0x9c>
jmp    120b <ASN1_item_i2d_bio+0xcb>
cmpl   $0x0,-0x2c(%rbp)
jg     11f2 <ASN1_item_i2d_bio+0xb2>
movl   $0x0,-0x38(%rbp)
jmp    120b <ASN1_item_i2d_bio+0xcb>
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
jmp    11b1 <ASN1_item_i2d_bio+0x71>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ASN1_item_i2d_bio>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rax
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %rdx,%rdi
mov    %rax,%rdx
call   1060 <ASN1_item_i2d@plt>
mov    (%rsp),%rsi
test   %rsi,%rsi
je     11af <ASN1_item_i2d_bio+0x6f>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%edx
call   1050 <BIO_write@plt>
mov    $0x1,%r14d
cmp    %eax,%ebp
je     11ce <ASN1_item_i2d_bio+0x8e>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %eax,%eax
jle    11cb <ASN1_item_i2d_bio+0x8b>
add    %eax,%ebx
sub    %eax,%ebp
movslq %ebx,%rsi
add    (%rsp),%rsi
mov    %r15,%rdi
mov    %ebp,%edx
call   1050 <BIO_write@plt>
cmp    %eax,%ebp
jne    1190 <ASN1_item_i2d_bio+0x50>
jmp    11ce <ASN1_item_i2d_bio+0x8e>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
xor    %r14d,%r14d
jmp    11d7 <ASN1_item_i2d_bio+0x97>
xor    %r14d,%r14d
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ASN1_F_ASN1_ITEM_I2D_BIO = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @ASN1_item_i2d_bio(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i8* null, i8** %8
store i32 0, i32* %10
store i32 1, i32* %12
%13 = load i8*, i8** %7
%14 = load i32*, i32** %5
%15 = call i32 @ASN1_item_i2d(i8* %13, i8** %8, i32* %14)
store i32 %15, i32* %11
%16 = load i8*, i8** %8
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i32, i32* @ASN1_F_ASN1_ITEM_I2D_BIO
%20 = load i32, i32* @ERR_R_MALLOC_FAILURE
%21 = call i32 @ASN1err(i32 %19, i32 %20)
store i32 0, i32* %4
br label %50
22: ; preds = %3
br label %23
23: ; preds = %39, %22
%24 = load i32*, i32** %6
%25 = load i8*, i8** %8
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32, i32* %11
%30 = call i32 @BIO_write(i32* %24, i8* %28, i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = load i32, i32* %11
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %23
br label %46
35: ; preds = %23
%36 = load i32, i32* %9
%37 = icmp sle i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 0, i32* %12
br label %46
39: ; preds = %35
%40 = load i32, i32* %9
%41 = load i32, i32* %10
%42 = add nsw i32 %41, %40
store i32 %42, i32* %10
%43 = load i32, i32* %9
%44 = load i32, i32* %11
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %11
br label %23
46: ; preds = %38, %34
%47 = load i8*, i8** %8
%48 = call i32 @free(i8* %47)
%49 = load i32, i32* %12
store i32 %49, i32* %4
br label %50
50: ; preds = %46, %18
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@ASN1_F_ASN1_ITEM_I2D_BIO = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i32 @ASN1_item_i2d_bio(i32* %0, i32* %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%6 = call i32 @ASN1_item_i2d(i8* %2, i8** %4, i32* %0)
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = call i32 @BIO_write(i32* %1, i8* %7, i32 %6)
%11 = icmp eq i32 %6, %10
br i1 %11, label %29, label %16
12: ; preds = %3
%13 = load i32, i32* @ASN1_F_ASN1_ITEM_I2D_BIO
%14 = load i32, i32* @ERR_R_MALLOC_FAILURE
%15 = call i32 @ASN1err(i32 %13, i32 %14)
br label %33
16: ; preds = %9, %21
%17 = phi i32 [ %27, %21 ], [ %10, %9 ]
%18 = phi i32 [ %23, %21 ], [ %6, %9 ]
%19 = phi i32 [ %22, %21 ], [ 0, %9 ]
%20 = icmp slt i32 %17, 1
br i1 %20, label %29, label %21
21: ; preds = %16
%22 = add nsw i32 %17, %19
%23 = sub nsw i32 %18, %17
%24 = load i8*, i8** %4
%25 = sext i32 %22 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = call i32 @BIO_write(i32* %1, i8* %26, i32 %23)
%28 = icmp eq i32 %23, %27
br i1 %28, label %29, label %16
29: ; preds = %21, %16, %9
%30 = phi i32 [ 1, %9 ], [ 0, %16 ], [ 1, %21 ]
%31 = load i8*, i8** %4
%32 = call i32 @free(i8* %31)
br label %33
33: ; preds = %29, %12
%34 = phi i32 [ 0, %12 ], [ %30, %29 ]
ret i32 %34
}