#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_F_DO_PKCS7_SIGNED_ATTRIB;
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*/ PKCS7err(int /*<<< orphan*/, 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)) {
      PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
      return 0;
    }
  }
  if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
    PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
    return 0;
  }
  if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
    PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, 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    11fc <do_pkcs7_signed_attrib+0x9c>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <PKCS7_add0_attrib_signing_time@plt>
cmp    $0x0,%eax
jne    11f7 <do_pkcs7_signed_attrib+0x97>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    12b6 <do_pkcs7_signed_attrib+0x156>
jmp    11fc <do_pkcs7_signed_attrib+0x9c>
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rdx
call   1040 <EVP_DigestFinal_ex@plt>
cmp    $0x0,%eax
jne    1240 <do_pkcs7_signed_attrib+0xe0>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    12b6 <do_pkcs7_signed_attrib+0x156>
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%edx
call   1060 <PKCS7_add1_attrib_digest@plt>
cmp    $0x0,%eax
jne    1283 <do_pkcs7_signed_attrib+0x123>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    12b6 <do_pkcs7_signed_attrib+0x156>
mov    -0x10(%rbp),%rdi
call   1030 <PKCS7_SIGNER_INFO_sign@plt>
cmp    $0x0,%eax
jne    12a8 <do_pkcs7_signed_attrib+0x148>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    12b6 <do_pkcs7_signed_attrib+0x156>
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   1080 <PKCS7_add0_attrib_signing_time@plt>
test   %eax,%eax
je     1218 <do_pkcs7_signed_attrib+0xb8>
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     11fc <do_pkcs7_signed_attrib+0x9c>
mov    %r14,%rdi
call   1030 <PKCS7_SIGNER_INFO_sign@plt>
xor    %r12d,%r12d
test   %eax,%eax
setne  %r12b
jmp    122f <do_pkcs7_signed_attrib+0xcf>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
jmp    120c <do_pkcs7_signed_attrib+0xac>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@plt>
xor    %r12d,%r12d
jmp    122f <do_pkcs7_signed_attrib+0xcf>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
call   1070 <PKCS7err@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
@PKCS7_F_DO_PKCS7_SIGNED_ATTRIB = 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 %27, 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 %26, label %22
22: ; preds = %18
%23 = load i32, i32* @PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
%24 = load i32, i32* @ERR_R_MALLOC_FAILURE
%25 = call i32 @PKCS7err(i32 %23, i32 %24)
store i32 0, i32* %3
store i32 1, i32* %9
br label %50
26: ; preds = %18
br label %27
27: ; preds = %26, %2
%28 = load i32*, i32** %5
%29 = call i32 @EVP_DigestFinal_ex(i32* %28, i8* %13, i32* %8)
%30 = icmp ne i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i32, i32* @PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
%33 = load i32, i32* @ERR_R_EVP_LIB
%34 = call i32 @PKCS7err(i32 %32, i32 %33)
store i32 0, i32* %3
store i32 1, i32* %9
br label %50
35: ; preds = %27
%36 = load i32*, i32** %4
%37 = load i32, i32* %8
%38 = call i32 @PKCS7_add1_attrib_digest(i32* %36, i8* %13, i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %35
%41 = load i32, i32* @PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
%42 = load i32, i32* @ERR_R_MALLOC_FAILURE
%43 = call i32 @PKCS7err(i32 %41, i32 %42)
store i32 0, i32* %3
store i32 1, i32* %9
br label %50
44: ; preds = %35
%45 = load i32*, i32** %4
%46 = call i32 @PKCS7_SIGNER_INFO_sign(i32* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %44
store i32 0, i32* %3
store i32 1, i32* %9
br label %50
49: ; preds = %44
store i32 1, i32* %3
store i32 1, i32* %9
br label %50
50: ; preds = %49, %48, %40, %31, %22
%51 = load i8*, i8** %6
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@EVP_MAX_MD_SIZE = global i32 0
@NID_pkcs9_signingTime = global i32 0
@PKCS7_F_DO_PKCS7_SIGNED_ATTRIB = 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 %18
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 %18
14: ; preds = %11
%15 = load i32, i32* @PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
%16 = load i32, i32* @ERR_R_MALLOC_FAILURE
%17 = tail call i32 @PKCS7err(i32 %15, i32 %16)
br label %37
18: ; preds = %11, %2
%19 = call i32 @EVP_DigestFinal_ex(i32* %1, i8* %6, i32* %3)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
%23 = load i32, i32* @ERR_R_EVP_LIB
%24 = call i32 @PKCS7err(i32 %22, i32 %23)
br label %37
25: ; preds = %18
%26 = load i32, i32* %3
%27 = call i32 @PKCS7_add1_attrib_digest(i32* %0, i8* %6, i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
%31 = load i32, i32* @ERR_R_MALLOC_FAILURE
%32 = call i32 @PKCS7err(i32 %30, i32 %31)
br label %37
33: ; preds = %25
%34 = call i32 @PKCS7_SIGNER_INFO_sign(i32* %0)
%35 = icmp ne i32 %34, 0
%36 = zext i1 %35 to i32
br label %37
37: ; preds = %33, %29, %21, %14
%38 = phi i32 [ 0, %29 ], [ 0, %21 ], [ 0, %14 ], [ %36, %33 ]
ret i32 %38
}