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

/* Variables and functions */
int /*<<< orphan*/ ERR_R_EVP_LIB;
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ EVP_DigestFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                      unsigned int *);
int EVP_MAX_MD_SIZE;
int /*<<< orphan*/ NID_pkcs9_signingTime;
int /*<<< orphan*/ PKCS7_SIGNER_INFO_sign(int /*<<< orphan*/ *);
int /*<<< orphan*/ PKCS7_add0_attrib_signing_time(int /*<<< orphan*/ *,
                                                  int /*<<< orphan*/ *);
int /*<<< orphan*/ PKCS7_add1_attrib_digest(int /*<<< orphan*/ *,
                                            unsigned char *, unsigned int);
int /*<<< orphan*/ PKCS7_get_signed_attribute(int /*<<< orphan*/ *,
                                              int /*<<< orphan*/);
int /*<<< orphan*/ PKCS7error(int /*<<< orphan*/);
int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx) {
  unsigned char md_data[EVP_MAX_MD_SIZE];
  unsigned int md_len;
  if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
    if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
      PKCS7error(ERR_R_MALLOC_FAILURE);
      return 0;
    }
  }
  if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
    PKCS7error(ERR_R_EVP_LIB);
    return 0;
  }
  if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
    PKCS7error(ERR_R_MALLOC_FAILURE);
    return 0;
  }
  if (!PKCS7_SIGNER_INFO_sign(si))
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<do_pkcs7_signed_attrib>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1050 <PKCS7_get_signed_attribute@plt>
cmp    $0x0,%eax
jne    11f3 <do_pkcs7_signed_attrib+0x93>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <PKCS7_add0_attrib_signing_time@plt>
cmp    $0x0,%eax
jne    11ee <do_pkcs7_signed_attrib+0x8e>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <PKCS7error@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129b <do_pkcs7_signed_attrib+0x13b>
jmp    11f3 <do_pkcs7_signed_attrib+0x93>
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rdx
call   1040 <EVP_DigestFinal_ex@plt>
cmp    $0x0,%eax
jne    122e <do_pkcs7_signed_attrib+0xce>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1080 <PKCS7error@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129b <do_pkcs7_signed_attrib+0x13b>
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%edx
call   1060 <PKCS7_add1_attrib_digest@plt>
cmp    $0x0,%eax
jne    1268 <do_pkcs7_signed_attrib+0x108>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1080 <PKCS7error@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129b <do_pkcs7_signed_attrib+0x13b>
mov    -0x10(%rbp),%rdi
call   1030 <PKCS7_SIGNER_INFO_sign@plt>
cmp    $0x0,%eax
jne    128d <do_pkcs7_signed_attrib+0x12d>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129b <do_pkcs7_signed_attrib+0x13b>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_pkcs7_signed_attrib>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1050 <PKCS7_get_signed_attribute@plt>
test   %eax,%eax
jne    11b2 <do_pkcs7_signed_attrib+0x52>
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <PKCS7_add0_attrib_signing_time@plt>
test   %eax,%eax
je     1206 <do_pkcs7_signed_attrib+0xa6>
lea    -0x24(%rbp),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <EVP_DigestFinal_ex@plt>
test   %eax,%eax
je     11ea <do_pkcs7_signed_attrib+0x8a>
mov    -0x24(%rbp),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <PKCS7_add1_attrib_digest@plt>
test   %eax,%eax
je     11f3 <do_pkcs7_signed_attrib+0x93>
mov    %r14,%rdi
call   1030 <PKCS7_SIGNER_INFO_sign@plt>
xor    %r12d,%r12d
test   %eax,%eax
setne  %r12b
jmp    1214 <do_pkcs7_signed_attrib+0xb4>
mov    0x2de7(%rip),%rax
jmp    11fa <do_pkcs7_signed_attrib+0x9a>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1080 <PKCS7error@plt>
xor    %r12d,%r12d
jmp    1214 <do_pkcs7_signed_attrib+0xb4>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1080 <PKCS7error@plt>
mov    %r12d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_MD_SIZE = global i32 0
@NID_pkcs9_signingTime = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@ERR_R_EVP_LIB = global i32 0
define i32 @do_pkcs7_signed_attrib(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
%10 = load i32, i32* @EVP_MAX_MD_SIZE
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32*, i32** %4
%15 = load i32, i32* @NID_pkcs9_signingTime
%16 = call i32 @PKCS7_get_signed_attribute(i32* %14, i32 %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %2
%19 = load i32*, i32** %4
%20 = call i32 @PKCS7_add0_attrib_signing_time(i32* %19, i32* null)
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i32, i32* @ERR_R_MALLOC_FAILURE
%24 = call i32 @PKCS7error(i32 %23)
store i32 0, i32* %3
store i32 1, i32* %9
br label %47
25: ; preds = %18
br label %26
26: ; preds = %25, %2
%27 = load i32*, i32** %5
%28 = call i32 @EVP_DigestFinal_ex(i32* %27, i8* %13, i32* %8)
%29 = icmp ne i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %26
%31 = load i32, i32* @ERR_R_EVP_LIB
%32 = call i32 @PKCS7error(i32 %31)
store i32 0, i32* %3
store i32 1, i32* %9
br label %47
33: ; preds = %26
%34 = load i32*, i32** %4
%35 = load i32, i32* %8
%36 = call i32 @PKCS7_add1_attrib_digest(i32* %34, i8* %13, i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %33
%39 = load i32, i32* @ERR_R_MALLOC_FAILURE
%40 = call i32 @PKCS7error(i32 %39)
store i32 0, i32* %3
store i32 1, i32* %9
br label %47
41: ; preds = %33
%42 = load i32*, i32** %4
%43 = call i32 @PKCS7_SIGNER_INFO_sign(i32* %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %41
store i32 0, i32* %3
store i32 1, i32* %9
br label %47
46: ; preds = %41
store i32 1, i32* %3
store i32 1, i32* %9
br label %47
47: ; preds = %46, %45, %38, %30, %22
%48 = load i8*, i8** %6
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@EVP_MAX_MD_SIZE = global i32 0
@NID_pkcs9_signingTime = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@ERR_R_EVP_LIB = global i32 0
define i32 @do_pkcs7_signed_attrib(i32* %0, i32* %1) {
%3 = alloca i32
%4 = load i32, i32* @EVP_MAX_MD_SIZE
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32* %3 to i8*
%8 = load i32, i32* @NID_pkcs9_signingTime
%9 = tail call i32 @PKCS7_get_signed_attribute(i32* %0, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = tail call i32 @PKCS7_add0_attrib_signing_time(i32* %0, i32* null)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @ERR_R_MALLOC_FAILURE
%16 = tail call i32 @PKCS7error(i32 %15)
br label %34
17: ; preds = %11, %2
%18 = call i32 @EVP_DigestFinal_ex(i32* %1, i8* %6, i32* %3)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @ERR_R_EVP_LIB
%22 = call i32 @PKCS7error(i32 %21)
br label %34
23: ; preds = %17
%24 = load i32, i32* %3
%25 = call i32 @PKCS7_add1_attrib_digest(i32* %0, i8* %6, i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @ERR_R_MALLOC_FAILURE
%29 = call i32 @PKCS7error(i32 %28)
br label %34
30: ; preds = %23
%31 = call i32 @PKCS7_SIGNER_INFO_sign(i32* %0)
%32 = icmp ne i32 %31, 0
%33 = zext i1 %32 to i32
br label %34
34: ; preds = %30, %27, %20, %14
%35 = phi i32 [ 0, %27 ], [ 0, %20 ], [ 0, %14 ], [ %33, %30 ]
ret i32 %35
}