#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;
typedef int /*<<< orphan*/ PKCS7_SIGNER_INFO;
typedef int /*<<< orphan*/ PKCS7;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef int /*<<< orphan*/ EVP_MD;

/* Variables and functions */
scalar_t__ EVP_PKEY_get_default_digest_nid(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *EVP_get_digestbynid(int);
int /*<<< orphan*/ PKCS7_R_NO_DEFAULT_DIGEST;
int /*<<< orphan*/ PKCS7_SIGNER_INFO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *PKCS7_SIGNER_INFO_new();
int /*<<< orphan*/ PKCS7_SIGNER_INFO_set(int /*<<< orphan*/ *,
                                         int /*<<< orphan*/ *,
                                         int /*<<< orphan*/ *,
                                         int /*<<< orphan*/ const *);
int /*<<< orphan*/ PKCS7_add_signer(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ PKCS7error(int /*<<< orphan*/);
PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
                                       const EVP_MD *dgst) {
  PKCS7_SIGNER_INFO *si = NULL;
  if (dgst == NULL) {
    int def_nid;
    if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
      goto err;
    dgst = EVP_get_digestbynid(def_nid);
    if (dgst == NULL) {
      PKCS7error(PKCS7_R_NO_DEFAULT_DIGEST);
      goto err;
    }
  }
  if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
    goto err;
  if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
    goto err;
  if (!PKCS7_add_signer(p7, si))
    goto err;
  return (si);
err:
  if (si)
    PKCS7_SIGNER_INFO_free(si);
  return (NULL);
}


x86 assembly (clang, -O0):
<PKCS7_add_signature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e6 <PKCS7_add_signature+0x76>
mov    -0x20(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1080 <EVP_PKEY_get_default_digest_nid@plt>
cmp    $0x0,%rax
jg     11b7 <PKCS7_add_signature+0x47>
jmp    124b <PKCS7_add_signature+0xdb>
mov    -0x34(%rbp),%edi
call   1040 <EVP_get_digestbynid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e1 <PKCS7_add_signature+0x71>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1090 <PKCS7error@plt>
jmp    124b <PKCS7_add_signature+0xdb>
jmp    11e6 <PKCS7_add_signature+0x76>
mov    $0x0,%al
call   1050 <PKCS7_SIGNER_INFO_new@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1200 <PKCS7_add_signature+0x90>
jmp    124b <PKCS7_add_signature+0xdb>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1060 <PKCS7_SIGNER_INFO_set@plt>
cmp    $0x0,%eax
jne    1223 <PKCS7_add_signature+0xb3>
jmp    124b <PKCS7_add_signature+0xdb>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <PKCS7_add_signer@plt>
cmp    $0x0,%eax
jne    123e <PKCS7_add_signature+0xce>
jmp    124b <PKCS7_add_signature+0xdb>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1267 <PKCS7_add_signature+0xf7>
cmpq   $0x0,-0x30(%rbp)
je     125f <PKCS7_add_signature+0xef>
mov    -0x30(%rbp),%rdi
call   1070 <PKCS7_SIGNER_INFO_free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PKCS7_add_signature>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
test   %rcx,%rcx
je     11cb <PKCS7_add_signature+0x5b>
xor    %eax,%eax
call   1050 <PKCS7_SIGNER_INFO_new@plt>
test   %rax,%rax
je     11fe <PKCS7_add_signature+0x8e>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1060 <PKCS7_SIGNER_INFO_set@plt>
test   %eax,%eax
je     11c1 <PKCS7_add_signature+0x51>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <PKCS7_add_signer@plt>
test   %eax,%eax
jne    1200 <PKCS7_add_signature+0x90>
mov    %rbx,%rdi
call   1070 <PKCS7_SIGNER_INFO_free@plt>
jmp    11fe <PKCS7_add_signature+0x8e>
lea    0xc(%rsp),%rsi
mov    %r12,%rdi
call   1080 <EVP_PKEY_get_default_digest_nid@plt>
test   %rax,%rax
jle    11fe <PKCS7_add_signature+0x8e>
mov    0xc(%rsp),%edi
call   1040 <EVP_get_digestbynid@plt>
test   %rax,%rax
je     11f0 <PKCS7_add_signature+0x80>
mov    %rax,%r13
jmp    118e <PKCS7_add_signature+0x1e>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1090 <PKCS7error@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PKCS7_R_NO_DEFAULT_DIGEST = global i32 0
define i32* @PKCS7_add_signature(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* null, i32** %10
%12 = load i32*, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %28
14: ; preds = %4
%15 = load i32*, i32** %8
%16 = call i64 @EVP_PKEY_get_default_digest_nid(i32* %15, i32* %11)
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
br label %48
19: ; preds = %14
%20 = load i32, i32* %11
%21 = call i32* @EVP_get_digestbynid(i32 %20)
store i32* %21, i32** %9
%22 = load i32*, i32** %9
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @PKCS7_R_NO_DEFAULT_DIGEST
%26 = call i32 @PKCS7error(i32 %25)
br label %48
27: ; preds = %19
br label %28
28: ; preds = %27, %4
%29 = call i32* (...) @PKCS7_SIGNER_INFO_new()
store i32* %29, i32** %10
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %32
31: ; preds = %28
br label %48
32: ; preds = %28
%33 = load i32*, i32** %10
%34 = load i32*, i32** %7
%35 = load i32*, i32** %8
%36 = load i32*, i32** %9
%37 = call i32 @PKCS7_SIGNER_INFO_set(i32* %33, i32* %34, i32* %35, i32* %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %32
br label %48
40: ; preds = %32
%41 = load i32*, i32** %6
%42 = load i32*, i32** %10
%43 = call i32 @PKCS7_add_signer(i32* %41, i32* %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %40
br label %48
46: ; preds = %40
%47 = load i32*, i32** %10
store i32* %47, i32** %5
br label %55
48: ; preds = %45, %39, %31, %24, %18
%49 = load i32*, i32** %10
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32*, i32** %10
%53 = call i32 @PKCS7_SIGNER_INFO_free(i32* %52)
br label %54
54: ; preds = %51, %48
store i32* null, i32** %5
br label %55
55: ; preds = %54, %46
%56 = load i32*, i32** %5
ret i32* %56
}

LLVM IR (-O2):
@PKCS7_R_NO_DEFAULT_DIGEST = global i32 0
define i32* @PKCS7_add_signature(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = icmp eq i32* %3, null
br i1 %6, label %7, label %20
7: ; preds = %4
%8 = bitcast i32* %5 to i8*
%9 = call i64 @EVP_PKEY_get_default_digest_nid(i32* %2, i32* %5)
%10 = icmp slt i64 %9, 1
br i1 %10, label %18, label %11
11: ; preds = %7
%12 = load i32, i32* %5
%13 = call i32* @EVP_get_digestbynid(i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @PKCS7_R_NO_DEFAULT_DIGEST
%17 = call i32 @PKCS7error(i32 %16)
br label %18
18: ; preds = %15, %7
br label %32
19: ; preds = %11
br label %20
20: ; preds = %19, %4
%21 = phi i32* [ %13, %19 ], [ %3, %4 ]
%22 = call i32* (...) @PKCS7_SIGNER_INFO_new()
%23 = icmp eq i32* %22, null
br i1 %23, label %32, label %24
24: ; preds = %20
%25 = call i32 @PKCS7_SIGNER_INFO_set(i32* %22, i32* %1, i32* %2, i32* %21)
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = call i32 @PKCS7_add_signer(i32* %0, i32* %22)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24, %27
%31 = call i32 @PKCS7_SIGNER_INFO_free(i32* %22)
br label %32
32: ; preds = %18, %20, %30, %27
%33 = phi i32* [ %22, %27 ], [ null, %30 ], [ null, %20 ], [ null, %18 ]
ret i32* %33
}