#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*/ EVP_PKEY;
typedef int /*<<< orphan*/ CMS_ContentInfo;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new_file(char *, char *);
int /*<<< orphan*/ BIO_reset(int /*<<< orphan*/ *);
int /*<<< orphan*/ CMS_ContentInfo_free(int /*<<< orphan*/ *);
int CMS_DETACHED;
int CMS_STREAM;
int /*<<< orphan*/ *CMS_sign(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ ERR_load_crypto_strings();
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ OpenSSL_add_all_algorithms();
int /*<<< orphan*/ *PEM_read_bio_PrivateKey(int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *,
                                            int /*<<< orphan*/,
                                            int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_bio_X509(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ SMIME_write_CMS(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ *, int);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  BIO *in = NULL, *out = NULL, *tbio = NULL;
  X509 *scert = NULL;
  EVP_PKEY *skey = NULL;
  CMS_ContentInfo *cms = NULL;
  int ret = 1;
  int flags = CMS_DETACHED | CMS_STREAM;
  OpenSSL_add_all_algorithms();
  ERR_load_crypto_strings();
  tbio = BIO_new_file("signer.pem", "r");
  if (!tbio)
    goto err;
  scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
  BIO_reset(tbio);
  skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
  if (!scert || !skey)
    goto err;
  in = BIO_new_file("sign.txt", "r");
  if (!in)
    goto err;
  cms = CMS_sign(scert, skey, NULL, in, flags);
  if (!cms)
    goto err;
  out = BIO_new_file("smout.txt", "w");
  if (!out)
    goto err;
  if (!(flags & CMS_STREAM))
    BIO_reset(in);
  if (!SMIME_write_CMS(out, cms, in, flags))
    goto err;
  ret = 0;
err:
  if (ret) {
    fprintf(stderr, "Error Signing Data\n");
    ERR_print_errors_fp(stderr);
  }
  CMS_ContentInfo_free(cms);
  X509_free(scert);
  EVP_PKEY_free(skey);
  BIO_free(in);
  BIO_free(out);
  BIO_free(tbio);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x1,-0x44(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    0x2da3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <OpenSSL_add_all_algorithms@plt>
mov    $0x0,%al
call   10a0 <ERR_load_crypto_strings@plt>
lea    0xda9(%rip),%rdi
lea    0xdad(%rip),%rsi
call   10f0 <BIO_new_file@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1277 <main+0x97>
jmp    1389 <main+0x1a9>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
xor    %edx,%edx
mov    %rcx,%rsi
call   10d0 <PEM_read_bio_X509@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <BIO_reset@plt>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
xor    %edx,%edx
mov    %rcx,%rsi
call   10c0 <PEM_read_bio_PrivateKey@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12c2 <main+0xe2>
cmpq   $0x0,-0x38(%rbp)
jne    12c7 <main+0xe7>
jmp    1389 <main+0x1a9>
lea    0xd3f(%rip),%rdi
lea    0xd36(%rip),%rsi
call   10f0 <BIO_new_file@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12ee <main+0x10e>
jmp    1389 <main+0x1a9>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x48(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edx
call   1070 <CMS_sign@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    131b <main+0x13b>
jmp    1389 <main+0x1a9>
lea    0xcf4(%rip),%rdi
lea    0xcf7(%rip),%rsi
call   10f0 <BIO_new_file@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1342 <main+0x162>
jmp    1389 <main+0x1a9>
mov    -0x48(%rbp),%eax
mov    0x2c94(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1360 <main+0x180>
mov    -0x18(%rbp),%rdi
call   10b0 <BIO_reset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x48(%rbp),%ecx
call   1060 <SMIME_write_CMS@plt>
cmp    $0x0,%eax
jne    1382 <main+0x1a2>
jmp    1389 <main+0x1a9>
movl   $0x0,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     13b6 <main+0x1d6>
mov    0x2c36(%rip),%rax
mov    (%rax),%edi
lea    0xc7f(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2c21(%rip),%rax
mov    (%rax),%edi
call   10e0 <ERR_print_errors_fp@plt>
mov    -0x40(%rbp),%rdi
call   1100 <CMS_ContentInfo_free@plt>
mov    -0x30(%rbp),%rdi
call   1090 <X509_free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <EVP_PKEY_free@plt>
mov    -0x18(%rbp),%rdi
call   1080 <BIO_free@plt>
mov    -0x20(%rbp),%rdi
call   1080 <BIO_free@plt>
mov    -0x28(%rbp),%rdi
call   1080 <BIO_free@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e03(%rip),%rax
mov    0x2de4(%rip),%r15
mov    (%r15),%r12d
or     (%rax),%r12d
xor    %eax,%eax
call   1050 <OpenSSL_add_all_algorithms@plt>
xor    %eax,%eax
call   10a0 <ERR_load_crypto_strings@plt>
lea    0xde9(%rip),%rdi
lea    0xded(%rip),%rsi
call   10f0 <BIO_new_file@plt>
mov    %rax,%r14
test   %rax,%rax
je     1313 <main+0x133>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10d0 <PEM_read_bio_X509@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   10b0 <BIO_reset@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10c0 <PEM_read_bio_PrivateKey@plt>
mov    %rbx,(%rsp)
test   %rbx,%rbx
mov    %rax,0x10(%rsp)
je     1300 <main+0x120>
mov    %rax,%rbp
test   %rax,%rax
je     1300 <main+0x120>
lea    0xd92(%rip),%rdi
lea    0xd89(%rip),%rsi
call   10f0 <BIO_new_file@plt>
test   %rax,%rax
je     1300 <main+0x120>
mov    %r15,%rbx
xor    %r13d,%r13d
mov    (%rsp),%rdi
mov    %rbp,%rsi
xor    %edx,%edx
mov    %rax,%rcx
mov    %rax,0x8(%rsp)
mov    %r12d,%r8d
call   1070 <CMS_sign@plt>
test   %rax,%rax
je     1391 <main+0x1b1>
mov    %rax,%r15
lea    0xd58(%rip),%rdi
lea    0xd5b(%rip),%rsi
call   10f0 <BIO_new_file@plt>
test   %rax,%rax
je     130a <main+0x12a>
mov    %rax,%r13
test   %r12d,(%rbx)
jne    12e1 <main+0x101>
mov    0x8(%rsp),%rdi
call   10b0 <BIO_reset@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    0x8(%rsp),%rdx
mov    %r12d,%ecx
call   1060 <SMIME_write_CMS@plt>
xor    %ebp,%ebp
test   %eax,%eax
mov    (%rsp),%rbx
jne    134c <main+0x16c>
jmp    1329 <main+0x149>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
xor    %r15d,%r15d
xor    %r13d,%r13d
mov    (%rsp),%rbx
jmp    1329 <main+0x149>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
xor    %r15d,%r15d
xor    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %ebx,%ebx
xor    %r13d,%r13d
mov    0x2ca0(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xce8(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x0(%rbp),%edi
call   10e0 <ERR_print_errors_fp@plt>
mov    $0x1,%ebp
mov    %r15,%rdi
call   1100 <CMS_ContentInfo_free@plt>
mov    %rbx,%rdi
call   1090 <X509_free@plt>
mov    0x10(%rsp),%rdi
call   1030 <EVP_PKEY_free@plt>
mov    0x8(%rsp),%rdi
call   1080 <BIO_free@plt>
mov    %r13,%rdi
call   1080 <BIO_free@plt>
mov    %r14,%rdi
call   1080 <BIO_free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
mov    (%rsp),%rbx
jmp    1329 <main+0x149>

LLVM IR (-O0):
@CMS_DETACHED = global i32 0
@CMS_STREAM = global i32 0
@.str = [11 x i8] c"signer.pem\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"sign.txt\00"
@.str.3 = [10 x i8] c"smout.txt\00"
@.str.4 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.5 = [20 x i8] c"Error Signing Data\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
store i32* null, i32** %7
store i32* null, i32** %8
store i32* null, i32** %9
store i32* null, i32** %10
store i32* null, i32** %11
store i32 1, i32* %12
%14 = load i32, i32* @CMS_DETACHED
%15 = load i32, i32* @CMS_STREAM
%16 = or i32 %14, %15
store i32 %16, i32* %13
%17 = call i32 (...) @OpenSSL_add_all_algorithms()
%18 = call i32 (...) @ERR_load_crypto_strings()
%19 = call i32* @BIO_new_file(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %8
%20 = load i32*, i32** %8
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %2
br label %72
23: ; preds = %2
%24 = load i32*, i32** %8
%25 = call i32* @PEM_read_bio_X509(i32* %24, i32* null, i32 0, i32* null)
store i32* %25, i32** %9
%26 = load i32*, i32** %8
%27 = call i32 @BIO_reset(i32* %26)
%28 = load i32*, i32** %8
%29 = call i32* @PEM_read_bio_PrivateKey(i32* %28, i32* null, i32 0, i32* null)
store i32* %29, i32** %10
%30 = load i32*, i32** %9
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32*, i32** %10
%34 = icmp ne i32* %33, null
br i1 %34, label %36, label %35
35: ; preds = %32, %23
br label %72
36: ; preds = %32
%37 = call i32* @BIO_new_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %37, i32** %6
%38 = load i32*, i32** %6
%39 = icmp ne i32* %38, null
br i1 %39, label %41, label %40
40: ; preds = %36
br label %72
41: ; preds = %36
%42 = load i32*, i32** %9
%43 = load i32*, i32** %10
%44 = load i32*, i32** %6
%45 = load i32, i32* %13
%46 = call i32* @CMS_sign(i32* %42, i32* %43, i32* null, i32* %44, i32 %45)
store i32* %46, i32** %11
%47 = load i32*, i32** %11
%48 = icmp ne i32* %47, null
br i1 %48, label %50, label %49
49: ; preds = %41
br label %72
50: ; preds = %41
%51 = call i32* @BIO_new_file(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %51, i32** %7
%52 = load i32*, i32** %7
%53 = icmp ne i32* %52, null
br i1 %53, label %55, label %54
54: ; preds = %50
br label %72
55: ; preds = %50
%56 = load i32, i32* %13
%57 = load i32, i32* @CMS_STREAM
%58 = and i32 %56, %57
%59 = icmp ne i32 %58, 0
br i1 %59, label %63, label %60
60: ; preds = %55
%61 = load i32*, i32** %6
%62 = call i32 @BIO_reset(i32* %61)
br label %63
63: ; preds = %60, %55
%64 = load i32*, i32** %7
%65 = load i32*, i32** %11
%66 = load i32*, i32** %6
%67 = load i32, i32* %13
%68 = call i32 @SMIME_write_CMS(i32* %64, i32* %65, i32* %66, i32 %67)
%69 = icmp ne i32 %68, 0
br i1 %69, label %71, label %70
70: ; preds = %63
br label %72
71: ; preds = %63
store i32 0, i32* %12
br label %72
72: ; preds = %71, %70, %54, %49, %40, %35, %22
%73 = load i32, i32* %12
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %72
%76 = load i32, i32* @stderr
%77 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%78 = load i32, i32* @stderr
%79 = call i32 @ERR_print_errors_fp(i32 %78)
br label %80
80: ; preds = %75, %72
%81 = load i32*, i32** %11
%82 = call i32 @CMS_ContentInfo_free(i32* %81)
%83 = load i32*, i32** %9
%84 = call i32 @X509_free(i32* %83)
%85 = load i32*, i32** %10
%86 = call i32 @EVP_PKEY_free(i32* %85)
%87 = load i32*, i32** %6
%88 = call i32 @BIO_free(i32* %87)
%89 = load i32*, i32** %7
%90 = call i32 @BIO_free(i32* %89)
%91 = load i32*, i32** %8
%92 = call i32 @BIO_free(i32* %91)
%93 = load i32, i32* %12
ret i32 %93
}

LLVM IR (-O2):
@CMS_DETACHED = global i32 0
@CMS_STREAM = global i32 0
@.str = [11 x i8] c"signer.pem\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"sign.txt\00"
@.str.3 = [10 x i8] c"smout.txt\00"
@.str.4 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.5 = [20 x i8] c"Error Signing Data\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @CMS_DETACHED
%4 = load i32, i32* @CMS_STREAM
%5 = or i32 %4, %3
%6 = tail call i32 (...) @OpenSSL_add_all_algorithms()
%7 = tail call i32 (...) @ERR_load_crypto_strings()
%8 = tail call i32* @BIO_new_file(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %35, label %10
10: ; preds = %2
%11 = tail call i32* @PEM_read_bio_X509(i32* %8, i32* null, i32 0, i32* null)
%12 = tail call i32 @BIO_reset(i32* %8)
%13 = tail call i32* @PEM_read_bio_PrivateKey(i32* %8, i32* null, i32 0, i32* null)
%14 = icmp ne i32* %11, null
%15 = icmp ne i32* %13, null
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %17, label %35
17: ; preds = %10
%18 = tail call i32* @BIO_new_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %35, label %20
20: ; preds = %17
%21 = tail call i32* @CMS_sign(i32* %11, i32* %13, i32* null, i32* %18, i32 %5)
%22 = icmp eq i32* %21, null
br i1 %22, label %35, label %23
23: ; preds = %20
%24 = tail call i32* @BIO_new_file(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %35, label %26
26: ; preds = %23
%27 = load i32, i32* @CMS_STREAM
%28 = and i32 %27, %5
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 @BIO_reset(i32* %18)
br label %32
32: ; preds = %26, %30
%33 = tail call i32 @SMIME_write_CMS(i32* %24, i32* %21, i32* %18, i32 %5)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %45
35: ; preds = %2, %10, %17, %20, %23, %32
%36 = phi i32* [ %18, %32 ], [ null, %2 ], [ null, %10 ], [ null, %17 ], [ %18, %20 ], [ %18, %23 ]
%37 = phi i32* [ %21, %32 ], [ null, %2 ], [ null, %10 ], [ null, %17 ], [ null, %20 ], [ %21, %23 ]
%38 = phi i32* [ %13, %32 ], [ null, %2 ], [ %13, %10 ], [ %13, %17 ], [ %13, %20 ], [ %13, %23 ]
%39 = phi i32* [ %11, %32 ], [ null, %2 ], [ %11, %10 ], [ %11, %17 ], [ %11, %20 ], [ %11, %23 ]
%40 = phi i32* [ %24, %32 ], [ null, %2 ], [ null, %10 ], [ null, %17 ], [ null, %20 ], [ null, %23 ]
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%43 = load i32, i32* @stderr
%44 = tail call i32 @ERR_print_errors_fp(i32 %43)
br label %45
45: ; preds = %35, %32
%46 = phi i32* [ %36, %35 ], [ %18, %32 ]
%47 = phi i32 [ 1, %35 ], [ 0, %32 ]
%48 = phi i32* [ %37, %35 ], [ %21, %32 ]
%49 = phi i32* [ %38, %35 ], [ %13, %32 ]
%50 = phi i32* [ %39, %35 ], [ %11, %32 ]
%51 = phi i32* [ %40, %35 ], [ %24, %32 ]
%52 = tail call i32 @CMS_ContentInfo_free(i32* %48)
%53 = tail call i32 @X509_free(i32* %50)
%54 = tail call i32 @EVP_PKEY_free(i32* %49)
%55 = tail call i32 @BIO_free(i32* %46)
%56 = tail call i32 @BIO_free(i32* %51)
%57 = tail call i32 @BIO_free(i32* %8)
ret i32 %47
}