#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*/ 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 OBJ_create(char *, char *, char *);
int /*<<< orphan*/ *X509_EXTENSION_create_by_NID(int /*<<< orphan*/ *, int,
                                                 int /*<<< orphan*/,
                                                 int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_add_ext(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int);
int add_media_extention(X509 *cert, unsigned char *data, int datalen) {
  int mediaNID;
  ASN1_OCTET_STRING *octetString;
  X509_EXTENSION *ext;
  mediaNID = OBJ_create("1.3.6.1.4.1.40107.1", "Media",
                        "Reschly's X509 Media Extension");
  octetString = ASN1_OCTET_STRING_new();
  ASN1_OCTET_STRING_set(octetString, (unsigned char *)data, datalen);
  ext = X509_EXTENSION_create_by_NID(NULL, mediaNID, 0, octetString);
  if (!ext)
    return 0;
  X509_add_ext(cert, ext, -1);
  return 1;
}


x86 assembly (clang, -O0):
<add_media_extention>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    0xe96(%rip),%rdi
lea    0xea3(%rip),%rsi
lea    0xea2(%rip),%rdx
call   1050 <OBJ_create@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <ASN1_OCTET_STRING_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <ASN1_OCTET_STRING_set@plt>
mov    -0x20(%rbp),%esi
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1060 <X509_EXTENSION_create_by_NID@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c8 <add_media_extention+0x78>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <add_media_extention+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0xffffffff,%edx
call   1070 <X509_add_ext@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_media_extention>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe95(%rip),%rdi
lea    0xea2(%rip),%rsi
lea    0xea1(%rip),%rdx
call   1050 <OBJ_create@plt>
mov    %eax,%r13d
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <ASN1_OCTET_STRING_new@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1040 <ASN1_OCTET_STRING_set@plt>
xor    %edi,%edi
mov    %r13d,%esi
xor    %edx,%edx
mov    %rbp,%rcx
call   1060 <X509_EXTENSION_create_by_NID@plt>
test   %rax,%rax
je     11c4 <add_media_extention+0x74>
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0xffffffff,%edx
call   1070 <X509_add_ext@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"1.3.6.1.4.1.40107.1\00"
@.str.1 = [6 x i8] c"Media\00"
@.str.2 = [31 x i8] c"Reschly's X509 Media Extension\00"
define i32 @add_media_extention(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = call i32 @OBJ_create(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 %11, i32* %8
%12 = call i32* (...) @ASN1_OCTET_STRING_new()
store i32* %12, i32** %9
%13 = load i32*, i32** %9
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @ASN1_OCTET_STRING_set(i32* %13, i8* %14, i32 %15)
%17 = load i32, i32* %8
%18 = load i32*, i32** %9
%19 = call i32* @X509_EXTENSION_create_by_NID(i32* null, i32 %17, i32 0, i32* %18)
store i32* %19, i32** %10
%20 = load i32*, i32** %10
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %3
store i32 0, i32* %4
br label %27
23: ; preds = %3
%24 = load i32*, i32** %5
%25 = load i32*, i32** %10
%26 = call i32 @X509_add_ext(i32* %24, i32* %25, i32 -1)
store i32 1, i32* %4
br label %27
27: ; preds = %23, %22
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@.str = [20 x i8] c"1.3.6.1.4.1.40107.1\00"
@.str.1 = [6 x i8] c"Media\00"
@.str.2 = [31 x i8] c"Reschly's X509 Media Extension\00"
define i32 @add_media_extention(i32* %0, i8* %1, i32 %2) {
%4 = tail call i32 @OBJ_create(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%5 = tail call i32* (...) @ASN1_OCTET_STRING_new()
%6 = tail call i32 @ASN1_OCTET_STRING_set(i32* %5, i8* %1, i32 %2)
%7 = tail call i32* @X509_EXTENSION_create_by_NID(i32* null, i32 %4, i32 0, i32* %5)
%8 = icmp eq i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @X509_add_ext(i32* %0, i32* %7, i32 -1)
br label %11
11: ; preds = %3, %9
%12 = phi i32 [ 1, %9 ], [ 0, %3 ]
ret i32 %12
}