#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;

/* Variables and functions */
int /*<<< orphan*/ *X509V3_EXT_conf_nid(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *, int, char *);
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_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_add_ext(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int);
int _add_ext(X509 *cert, int nid, char *value) {
  X509_EXTENSION *ex;
  X509V3_CTX ctx;
  X509V3_set_ctx_nodb(&ctx);
  X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
  ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
  if (!ex)
    return 0;
  X509_add_ext(cert, ex, -1);
  X509_EXTENSION_free(ex);
  return 1;
}


x86 assembly (clang, -O0):
<_add_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x2c(%rbp),%rdi
call   1030 <X509V3_set_ctx_nodb@plt>
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x2c(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
mov    %r8,%rcx
call   1060 <X509V3_set_ctx@plt>
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x2c(%rbp),%rsi
call   1040 <X509V3_EXT_conf_nid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b7 <_add_ext+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11d9 <_add_ext+0x89>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0xffffffff,%edx
call   1050 <X509_add_ext@plt>
mov    -0x28(%rbp),%rdi
call   1070 <X509_EXTENSION_free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_add_ext>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %esi,%ebp
mov    %rdi,%r15
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <X509V3_set_ctx_nodb@plt>
xor    %r14d,%r14d
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <X509V3_set_ctx@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r12,%rcx
call   1040 <X509V3_EXT_conf_nid@plt>
test   %rax,%rax
je     11bf <_add_ext+0x6f>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0xffffffff,%edx
call   1050 <X509_add_ext@plt>
mov    %rbx,%rdi
call   1070 <X509_EXTENSION_free@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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