#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*/ X509_EXTENSION_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_add_ext(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int set_extension(X509 *issuer, X509 *cert, int nid, char *value) {
  X509_EXTENSION *ext;
  X509V3_CTX ctx;
  X509V3_set_ctx(&ctx, issuer, cert, NULL, NULL, 0);
  ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
  if (!ext) {
    printf("error setting extension\n");
    ERR_print_errors_fp(stdout);
    return -1;
  }
  X509_add_ext(cert, ext, -1);
  X509_EXTENSION_free(ext);
  return 0;
}


x86 assembly (clang, -O0):
<set_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
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    11dc <set_extension+0x7c>
lea    0xe43(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1080 <ERR_print_errors_fp@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <set_extension+0x9e>
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   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_extension>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rsi
xor    %r14d,%r14d
lea    0xc(%rsp),%r12
mov    %r12,%rdi
mov    %r15,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <X509V3_set_ctx@plt>
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <X509V3_EXT_conf_nid@plt>
test   %rax,%rax
je     11c3 <set_extension+0x63>
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>
jmp    11e3 <set_extension+0x83>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1080 <ERR_print_errors_fp@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"error setting extension\0A\00"
@stdout = global i32 0
define i32 @set_extension(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 = load i32*, i32** %6
%13 = load i32*, i32** %7
%14 = call i32 @X509V3_set_ctx(i32* %11, i32* %12, i32* %13, i32* null, i32* null, i32 0)
%15 = load i32, i32* %8
%16 = load i8*, i8** %9
%17 = call i32* @X509V3_EXT_conf_nid(i32* null, i32* %11, i32 %15, i8* %16)
store i32* %17, i32** %10
%18 = load i32*, i32** %10
%19 = icmp ne i32* %18, null
br i1 %19, label %24, label %20
20: ; preds = %4
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @stdout
%23 = call i32 @ERR_print_errors_fp(i32 %22)
store i32 -1, 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 0, i32* %5
br label %30
30: ; preds = %24, %20
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
@stdout = global i32 0
@str = [24 x i8] c"error setting extension\00"
define i32 @set_extension(i32* %0, i32* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i32 @X509V3_set_ctx(i32* %5, i32* %0, i32* %1, i32* null, i32* null, i32 0)
%8 = call i32* @X509V3_EXT_conf_nid(i32* null, i32* %5, i32 %2, i8* %3)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%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* %8, i32 -1)
%16 = call i32 @X509_EXTENSION_free(i32* %8)
br label %17
17: ; preds = %14, %10
%18 = phi i32 [ 0, %14 ], [ -1, %10 ]
ret i32 %18
}