#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*/ ERR_print_errors_fp(int /*<<< orphan*/);
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 /*<<< orphan*/ stdout;
int cert_add_ext(X509 *issuer, X509 *subject, int nid, char *value) {
  X509_EXTENSION *ex;
  X509V3_CTX ctx;
  X509V3_set_ctx_nodb(&ctx);
  X509V3_set_ctx(&ctx, issuer, subject, NULL, NULL, 0);
  ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
  if (!ex) {
    ERR_print_errors_fp(stdout);
    return 0;
  }
  X509_add_ext(subject, ex, -1);
  X509_EXTENSION_free(ex);
  return 1;
}


x86 assembly (clang, -O0):
<cert_add_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x34(%rbp),%rdi
call   1030 <X509V3_set_ctx_nodb@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x34(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
mov    %r8,%rcx
call   1060 <X509V3_set_ctx@plt>
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x34(%rbp),%rsi
call   1040 <X509V3_EXT_conf_nid@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d9 <cert_add_ext+0x79>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1080 <ERR_print_errors_fp@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fb <cert_add_ext+0x9b>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0xffffffff,%edx
call   1050 <X509_add_ext@plt>
mov    -0x30(%rbp),%rdi
call   1070 <X509_EXTENSION_free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cert_add_ext>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <X509V3_set_ctx_nodb@plt>
xor    %r14d,%r14d
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <X509V3_set_ctx@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r13d,%edx
mov    %r12,%rcx
call   1040 <X509V3_EXT_conf_nid@plt>
test   %rax,%rax
je     11d5 <cert_add_ext+0x75>
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
jmp    11e3 <cert_add_ext+0x83>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1080 <ERR_print_errors_fp@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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