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

/* Variables and functions */
int *ASN1_STRING_get0_data(int /*<<< orphan*/ const *);
int ASN1_STRING_length(int /*<<< orphan*/ const *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_get0_subject_key_id(int /*<<< orphan*/ *);
int /*<<< orphan*/ *d2i_X509(int /*<<< orphan*/ *, int const **,
                             int /*<<< orphan*/ const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
int /*<<< orphan*/ stderr;
int get_cert_skid(const uint8_t *cert, const uint32_t cert_size, char **skid) {
  X509 *X509cert;
  const ASN1_OCTET_STRING *asn1_id;
  const uint8_t *data;
  int data_len, i;
  char *id_str, *ptr;
  int ret = -1;
  X509cert = d2i_X509(NULL, &cert, cert_size);
  if (X509cert == NULL) {
    fprintf(stderr, "invalid x509 certificate\n");
    goto out;
  }
  asn1_id = X509_get0_subject_key_id(X509cert);
  if (asn1_id == NULL) {
    fprintf(stderr, "Failed to get Subject Key ID\n");
    goto out;
  }
  data = ASN1_STRING_get0_data(asn1_id);
  data_len = ASN1_STRING_length(asn1_id);
  id_str = malloc(data_len * 2 + 1);
  if (id_str == NULL) {
    fprintf(stderr, "Failed to allocated id string\n");
    goto out;
  }
  ptr = id_str;
  for (i = 0; i < data_len; i++) {
    snprintf(ptr, 3, "%02x", data[i]);
    ptr += 2;
  }
  *skid = id_str;
  ret = 0;
out:
  if (X509cert)
    X509_free(X509cert);
  return ret;
}


x86 assembly (clang, -O0):
<get_cert_skid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
call   10a0 <d2i_X509@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d3 <get_cert_skid+0x53>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12bc <get_cert_skid+0x13c>
mov    -0x20(%rbp),%rdi
call   1040 <X509_get0_subject_key_id@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1205 <get_cert_skid+0x85>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12bc <get_cert_skid+0x13c>
mov    -0x28(%rbp),%rdi
call   1080 <ASN1_STRING_get0_data@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <ASN1_STRING_length@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1255 <get_cert_skid+0xd5>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12bc <get_cert_skid+0x13c>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    12aa <get_cert_skid+0x12a>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    $0x3,%esi
lea    0xdcc(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1264 <get_cert_skid+0xe4>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12d0 <get_cert_skid+0x150>
mov    -0x20(%rbp),%rdi
call   1090 <X509_free@plt>
mov    -0x4c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cert_skid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %esi,%edx
mov    %rdi,0x10(%rsp)
lea    0x10(%rsp),%rsi
xor    %edi,%edi
call   10a0 <d2i_X509@plt>
test   %rax,%rax
je     123a <get_cert_skid+0xba>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <X509_get0_subject_key_id@plt>
test   %rax,%rax
je     1256 <get_cert_skid+0xd6>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <ASN1_STRING_get0_data@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <ASN1_STRING_length@plt>
mov    %eax,%r13d
lea    0x1(%r13,%r13,1),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1268 <get_cert_skid+0xe8>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
test   %r13d,%r13d
jle    122e <get_cert_skid+0xae>
mov    %r13d,%ebp
add    %rbp,%rbp
xor    %r14d,%r14d
lea    0xe51(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
lea    (%rbx,%r14,1),%rdi
mov    (%r12,%r14,2),%ecx
mov    $0x3,%esi
mov    %r13,%rdx
call   1050 <snprintf@plt>
add    $0x2,%r14
cmp    %r14,%rbp
jne    1210 <get_cert_skid+0x90>
mov    0x8(%rsp),%rax
mov    %rbx,(%rax)
xor    %ebx,%ebx
jmp    1282 <get_cert_skid+0x102>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    128a <get_cert_skid+0x10a>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
jmp    1278 <get_cert_skid+0xf8>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %r15,%rdi
call   1090 <X509_free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"invalid x509 certificate\0A\00"
@.str.1 = [30 x i8] c"Failed to get Subject Key ID\0A\00"
@.str.2 = [31 x i8] c"Failed to allocated id string\0A\00"
@.str.3 = [5 x i8] c"%02x\00"
define i32 @get_cert_skid(i32* %0, i32 %1, i8** %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
store i32 -1, i32* %14
%15 = load i32, i32* %5
%16 = call i32* @d2i_X509(i32* null, i32** %4, i32 %15)
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %66
22: ; preds = %3
%23 = load i32*, i32** %7
%24 = call i32* @X509_get0_subject_key_id(i32* %23)
store i32* %24, i32** %8
%25 = load i32*, i32** %8
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %66
30: ; preds = %22
%31 = load i32*, i32** %8
%32 = call i32* @ASN1_STRING_get0_data(i32* %31)
store i32* %32, i32** %9
%33 = load i32*, i32** %8
%34 = call i32 @ASN1_STRING_length(i32* %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = mul nsw i32 %35, 2
%37 = add nsw i32 %36, 1
%38 = call i8* @malloc(i32 %37)
store i8* %38, i8** %12
%39 = load i8*, i8** %12
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %66
44: ; preds = %30
%45 = load i8*, i8** %12
store i8* %45, i8** %13
store i32 0, i32* %11
br label %46
46: ; preds = %60, %44
%47 = load i32, i32* %11
%48 = load i32, i32* %10
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %63
50: ; preds = %46
%51 = load i8*, i8** %13
%52 = load i32*, i32** %9
%53 = load i32, i32* %11
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = call i32 @snprintf(i8* %51, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %56)
%58 = load i8*, i8** %13
%59 = getelementptr inbounds i8, i8* %58, i64 2
store i8* %59, i8** %13
br label %60
60: ; preds = %50
%61 = load i32, i32* %11
%62 = add nsw i32 %61, 1
store i32 %62, i32* %11
br label %46
63: ; preds = %46
%64 = load i8*, i8** %12
%65 = load i8**, i8*** %6
store i8* %64, i8** %65
store i32 0, i32* %14
br label %66
66: ; preds = %63, %41, %27, %19
%67 = load i32*, i32** %7
%68 = icmp ne i32* %67, null
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i32*, i32** %7
%71 = call i32 @X509_free(i32* %70)
br label %72
72: ; preds = %69, %66
%73 = load i32, i32* %14
ret i32 %73
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"invalid x509 certificate\0A\00"
@.str.1 = [30 x i8] c"Failed to get Subject Key ID\0A\00"
@.str.2 = [31 x i8] c"Failed to allocated id string\0A\00"
@.str.3 = [5 x i8] c"%02x\00"
define i32 @get_cert_skid(i32* %0, i32 %1, i8** nocapture writeonly %2) {
%4 = alloca i32*
store i32* %0, i32** %4
%5 = call i32* @d2i_X509(i32* null, i32** %4, i32 %1)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %43
10: ; preds = %3
%11 = call i32* @X509_get0_subject_key_id(i32* %5)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %40
16: ; preds = %10
%17 = call i32* @ASN1_STRING_get0_data(i32* %11)
%18 = call i32 @ASN1_STRING_length(i32* %11)
%19 = shl nsw i32 %18, 1
%20 = or i32 %19, 1
%21 = call i8* @malloc(i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = icmp sgt i32 %18, 0
br i1 %24, label %25, label %39
25: ; preds = %23
%26 = zext i32 %18 to i64
br label %30
27: ; preds = %16
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %40
30: ; preds = %25, %30
%31 = phi i64 [ 0, %25 ], [ %37, %30 ]
%32 = phi i8* [ %21, %25 ], [ %36, %30 ]
%33 = getelementptr inbounds i32, i32* %17, i64 %31
%34 = load i32, i32* %33
%35 = call i32 @snprintf(i8* %32, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds i8, i8* %32, i64 2
%37 = add nuw nsw i64 %31, 1
%38 = icmp eq i64 %37, %26
br i1 %38, label %39, label %30
39: ; preds = %30, %23
store i8* %21, i8** %2
br label %40
40: ; preds = %39, %13, %27
%41 = phi i32 [ 0, %39 ], [ -1, %27 ], [ -1, %13 ]
%42 = call i32 @X509_free(i32* %5)
br label %43
43: ; preds = %7, %40
%44 = phi i32 [ %41, %40 ], [ -1, %7 ]
ret i32 %44
}