#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_F_PKCS7_ADD_SIGNATURE;
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*/ PKCS7err(int /*<<< orphan*/, 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) {
      PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, 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    11ef <PKCS7_add_signature+0x7f>
mov    -0x20(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1090 <EVP_PKEY_get_default_digest_nid@plt>
cmp    $0x0,%rax
jg     11b7 <PKCS7_add_signature+0x47>
jmp    1254 <PKCS7_add_signature+0xe4>
mov    -0x34(%rbp),%edi
call   1040 <EVP_get_digestbynid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ea <PKCS7_add_signature+0x7a>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1080 <PKCS7err@plt>
jmp    1254 <PKCS7_add_signature+0xe4>
jmp    11ef <PKCS7_add_signature+0x7f>
mov    $0x0,%al
call   1050 <PKCS7_SIGNER_INFO_new@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1209 <PKCS7_add_signature+0x99>
jmp    1254 <PKCS7_add_signature+0xe4>
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    122c <PKCS7_add_signature+0xbc>
jmp    1254 <PKCS7_add_signature+0xe4>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <PKCS7_add_signer@plt>
cmp    $0x0,%eax
jne    1247 <PKCS7_add_signature+0xd7>
jmp    1254 <PKCS7_add_signature+0xe4>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1270 <PKCS7_add_signature+0x100>
cmpq   $0x0,-0x30(%rbp)
je     1268 <PKCS7_add_signature+0xf8>
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     1207 <PKCS7_add_signature+0x97>
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    1209 <PKCS7_add_signature+0x99>
mov    %rbx,%rdi
call   1070 <PKCS7_SIGNER_INFO_free@plt>
jmp    1207 <PKCS7_add_signature+0x97>
lea    0xc(%rsp),%rsi
mov    %r12,%rdi
call   1090 <EVP_PKEY_get_default_digest_nid@plt>
test   %rax,%rax
jle    1207 <PKCS7_add_signature+0x97>
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    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1080 <PKCS7err@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_F_PKCS7_ADD_SIGNATURE = global i32 0
@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 %29
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 %49
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 %28
24: ; preds = %19
%25 = load i32, i32* @PKCS7_F_PKCS7_ADD_SIGNATURE
%26 = load i32, i32* @PKCS7_R_NO_DEFAULT_DIGEST
%27 = call i32 @PKCS7err(i32 %25, i32 %26)
br label %49
28: ; preds = %19
br label %29
29: ; preds = %28, %4
%30 = call i32* (...) @PKCS7_SIGNER_INFO_new()
store i32* %30, i32** %10
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %29
br label %49
33: ; preds = %29
%34 = load i32*, i32** %10
%35 = load i32*, i32** %7
%36 = load i32*, i32** %8
%37 = load i32*, i32** %9
%38 = call i32 @PKCS7_SIGNER_INFO_set(i32* %34, i32* %35, i32* %36, i32* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %33
br label %49
41: ; preds = %33
%42 = load i32*, i32** %6
%43 = load i32*, i32** %10
%44 = call i32 @PKCS7_add_signer(i32* %42, i32* %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %41
br label %49
47: ; preds = %41
%48 = load i32*, i32** %10
store i32* %48, i32** %5
br label %56
49: ; preds = %46, %40, %32, %24, %18
%50 = load i32*, i32** %10
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32*, i32** %10
%54 = call i32 @PKCS7_SIGNER_INFO_free(i32* %53)
br label %55
55: ; preds = %52, %49
store i32* null, i32** %5
br label %56
56: ; preds = %55, %47
%57 = load i32*, i32** %5
ret i32* %57
}

LLVM IR (-O2):
@PKCS7_F_PKCS7_ADD_SIGNATURE = global i32 0
@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 %21
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 %19, 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 %20
15: ; preds = %11
%16 = load i32, i32* @PKCS7_F_PKCS7_ADD_SIGNATURE
%17 = load i32, i32* @PKCS7_R_NO_DEFAULT_DIGEST
%18 = call i32 @PKCS7err(i32 %16, i32 %17)
br label %19
19: ; preds = %15, %7
br label %33
20: ; preds = %11
br label %21
21: ; preds = %20, %4
%22 = phi i32* [ %13, %20 ], [ %3, %4 ]
%23 = call i32* (...) @PKCS7_SIGNER_INFO_new()
%24 = icmp eq i32* %23, null
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = call i32 @PKCS7_SIGNER_INFO_set(i32* %23, i32* %1, i32* %2, i32* %22)
%27 = icmp eq i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i32 @PKCS7_add_signer(i32* %0, i32* %23)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25, %28
%32 = call i32 @PKCS7_SIGNER_INFO_free(i32* %23)
br label %33
33: ; preds = %19, %21, %31, %28
%34 = phi i32* [ %23, %28 ], [ null, %31 ], [ null, %21 ], [ null, %19 ]
ret i32* %34
}