#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*/ X509_EXTENSION;
typedef int /*<<< orphan*/ X509V3_CTX;
typedef int /*<<< orphan*/ X509;
typedef int /*<<< orphan*/ ASN1_OCTET_STRING;

/* Variables and functions */
int /*<<< orphan*/ *ASN1_OCTET_STRING_new();
int /*<<< orphan*/ ASN1_OCTET_STRING_set(int /*<<< orphan*/ *, unsigned char *,
                                         int);
int /*<<< orphan*/ X509V3_set_ctx(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ X509V3_set_ctx_nodb(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_EXTENSION_create_by_NID(int /*<<< orphan*/ *, int,
                                                 int /*<<< orphan*/,
                                                 int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_EXTENSION_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_add_ext(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int);
int add_custom_ext(X509 *cert, int nid, unsigned char *value, int len) {
  X509_EXTENSION *ex;
  ASN1_OCTET_STRING *os = ASN1_OCTET_STRING_new();
  ASN1_OCTET_STRING_set(os, value, len);
  X509V3_CTX ctx;
  X509V3_set_ctx_nodb(&ctx);
  X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
  ex = X509_EXTENSION_create_by_NID(NULL, nid, 0, os);
  if (!X509_add_ext(cert, ex, -1))
    return 0;
  X509_EXTENSION_free(ex);
  return 1;
}


x86 assembly (clang, -O0):
<add_custom_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    $0x0,%al
call   1040 <ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <ASN1_OCTET_STRING_set@plt>
lea    -0x3c(%rbp),%rdi
call   1030 <X509V3_set_ctx_nodb@plt>
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x3c(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
mov    %r8,%rcx
call   1080 <X509V3_set_ctx@plt>
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1060 <X509_EXTENSION_create_by_NID@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0xffffffff,%edx
call   1070 <X509_add_ext@plt>
cmp    $0x0,%eax
jne    1203 <add_custom_ext+0x93>
movl   $0x0,-0x4(%rbp)
jmp    1213 <add_custom_ext+0xa3>
mov    -0x30(%rbp),%rdi
call   1090 <X509_EXTENSION_free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_custom_ext>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %rdx,%r13
mov    %esi,%r15d
mov    %rdi,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <ASN1_OCTET_STRING_new@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1050 <ASN1_OCTET_STRING_set@plt>
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1030 <X509V3_set_ctx_nodb@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <X509V3_set_ctx@plt>
xor    %edi,%edi
mov    %r15d,%esi
xor    %edx,%edx
mov    %rbp,%rcx
call   1060 <X509_EXTENSION_create_by_NID@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0xffffffff,%edx
call   1070 <X509_add_ext@plt>
test   %eax,%eax
je     11f9 <add_custom_ext+0x89>
mov    %rbp,%rdi
call   1090 <X509_EXTENSION_free@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @add_custom_ext(i32* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = call i32* (...) @ASN1_OCTET_STRING_new()
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = load i8*, i8** %8
%16 = load i32, i32* %9
%17 = call i32 @ASN1_OCTET_STRING_set(i32* %14, i8* %15, i32 %16)
%18 = call i32 @X509V3_set_ctx_nodb(i32* %12)
%19 = load i32*, i32** %6
%20 = load i32*, i32** %6
%21 = call i32 @X509V3_set_ctx(i32* %12, i32* %19, i32* %20, i32* null, i32* null, i32 0)
%22 = load i32, i32* %7
%23 = load i32*, i32** %11
%24 = call i32* @X509_EXTENSION_create_by_NID(i32* null, i32 %22, i32 0, i32* %23)
store i32* %24, i32** %10
%25 = load i32*, i32** %6
%26 = load i32*, i32** %10
%27 = call i32 @X509_add_ext(i32* %25, i32* %26, i32 -1)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %4
store i32 0, i32* %5
br label %33
30: ; preds = %4
%31 = load i32*, i32** %10
%32 = call i32 @X509_EXTENSION_free(i32* %31)
store i32 1, i32* %5
br label %33
33: ; preds = %30, %29
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
define i32 @add_custom_ext(i32* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = tail call i32* (...) @ASN1_OCTET_STRING_new()
%7 = tail call i32 @ASN1_OCTET_STRING_set(i32* %6, i8* %2, i32 %3)
%8 = bitcast i32* %5 to i8*
%9 = call i32 @X509V3_set_ctx_nodb(i32* %5)
%10 = call i32 @X509V3_set_ctx(i32* %5, i32* %0, i32* %0, i32* null, i32* null, i32 0)
%11 = call i32* @X509_EXTENSION_create_by_NID(i32* null, i32 %1, i32 0, i32* %6)
%12 = call i32 @X509_add_ext(i32* %0, i32* %11, i32 -1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %4
%15 = call i32 @X509_EXTENSION_free(i32* %11)
br label %16
16: ; preds = %4, %14
%17 = phi i32 [ 1, %14 ], [ 0, %4 ]
ret i32 %17
}