#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 */
typedef struct TYPE_11__ TYPE_5__;
typedef struct TYPE_10__ TYPE_4__;
typedef struct TYPE_9__ TYPE_3__;
typedef struct TYPE_8__ TYPE_2__;
typedef struct TYPE_7__ TYPE_1__;

/* Type definitions */
struct TYPE_7__ {
  int /*<<< orphan*/ ptr;
  int /*<<< orphan*/ *data;
};
struct TYPE_11__ {
  TYPE_1__ d;
};
struct TYPE_9__ {
  TYPE_2__ *sign;
};
struct TYPE_10__ {
  long detached;
  TYPE_3__ d;
  int /*<<< orphan*/ type;
};
struct TYPE_8__ {
  TYPE_5__ *contents;
};
typedef TYPE_4__ PKCS7;
typedef int /*<<< orphan*/ ASN1_OCTET_STRING;

/* Variables and functions */
int /*<<< orphan*/ ASN1_OCTET_STRING_free(int /*<<< orphan*/ *);
int NID_pkcs7_signed;
int OBJ_obj2nid(int /*<<< orphan*/);
int /*<<< orphan*/ PKCS7_F_PKCS7_CTRL;
#define PKCS7_OP_GET_DETACHED_SIGNATURE 129
#define PKCS7_OP_SET_DETACHED_SIGNATURE 128
int /*<<< orphan*/ PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE;
int /*<<< orphan*/ PKCS7_R_UNKNOWN_OPERATION;
int /*<<< orphan*/ PKCS7_type_is_data(TYPE_5__ *);
int /*<<< orphan*/ PKCS7err(int /*<<< orphan*/, int /*<<< orphan*/);
long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg) {
  int nid;
  long ret;
  nid = OBJ_obj2nid(p7->type);
  switch (cmd) {
  case PKCS7_OP_SET_DETACHED_SIGNATURE:
    if (nid == NID_pkcs7_signed) {
      ret = p7->detached = (int)larg;
      if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
        ASN1_OCTET_STRING *os;
        os = p7->d.sign->contents->d.data;
        ASN1_OCTET_STRING_free(os);
        p7->d.sign->contents->d.data = NULL;
      }
    } else {
      PKCS7err(PKCS7_F_PKCS7_CTRL,
               PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
      ret = 0;
    }
    break;
  case PKCS7_OP_GET_DETACHED_SIGNATURE:
    if (nid == NID_pkcs7_signed) {
      if (!p7->d.sign || !p7->d.sign->contents->d.ptr)
        ret = 1;
      else
        ret = 0;
      p7->detached = ret;
    } else {
      PKCS7err(PKCS7_F_PKCS7_CTRL,
               PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
      ret = 0;
    }
    break;
  default:
    PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION);
    ret = 0;
  }
  return (ret);
}


x86 assembly (clang, -O0):
<PKCS7_ctrl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%edi
call   1030 <OBJ_obj2nid@plt>
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x80,%eax
je     118f <PKCS7_ctrl+0x4f>
jmp    117c <PKCS7_ctrl+0x3c>
mov    -0x3c(%rbp),%eax
sub    $0x81,%eax
je     122e <PKCS7_ctrl+0xee>
jmp    12ac <PKCS7_ctrl+0x16c>
mov    -0x24(%rbp),%eax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%eax
jne    120a <PKCS7_ctrl+0xca>
mov    -0x18(%rbp),%rax
cltq
mov    -0x8(%rbp),%rcx
mov    %rax,(%rcx)
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1205 <PKCS7_ctrl+0xc5>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rdi
call   1050 <PKCS7_type_is_data@plt>
cmp    $0x0,%eax
je     1205 <PKCS7_ctrl+0xc5>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <ASN1_OCTET_STRING_free@plt>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
movq   $0x0,0x8(%rax)
jmp    1229 <PKCS7_ctrl+0xe9>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
movq   $0x0,-0x30(%rbp)
jmp    12cb <PKCS7_ctrl+0x18b>
mov    -0x24(%rbp),%eax
mov    0x2d88(%rip),%rcx
cmp    (%rcx),%eax
jne    1288 <PKCS7_ctrl+0x148>
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1263 <PKCS7_ctrl+0x123>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
cmpl   $0x0,(%rax)
jne    1270 <PKCS7_ctrl+0x130>
movq   $0x1,-0x30(%rbp)
jmp    1278 <PKCS7_ctrl+0x138>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    12a7 <PKCS7_ctrl+0x167>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
movq   $0x0,-0x30(%rbp)
jmp    12cb <PKCS7_ctrl+0x18b>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PKCS7_ctrl>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x10(%rdi),%edi
call   1030 <OBJ_obj2nid@plt>
cmp    $0x81,%ebp
je     11af <PKCS7_ctrl+0x6f>
cmp    $0x80,%ebp
jne    11d3 <PKCS7_ctrl+0x93>
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%eax
jne    11e5 <PKCS7_ctrl+0xa5>
mov    %rbx,%rax
shl    $0x20,%rax
movslq %ebx,%rbx
mov    %rbx,(%r14)
je     11fc <PKCS7_ctrl+0xbc>
mov    0x8(%r14),%rax
mov    (%rax),%rdi
call   1050 <PKCS7_type_is_data@plt>
test   %eax,%eax
je     11fe <PKCS7_ctrl+0xbe>
mov    0x8(%r14),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rdi
call   1040 <ASN1_OCTET_STRING_free@plt>
mov    0x8(%r14),%rax
mov    (%rax),%rax
movq   $0x0,0x8(%rax)
jmp    11fe <PKCS7_ctrl+0xbe>
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%eax
jne    11e5 <PKCS7_ctrl+0xa5>
mov    0x8(%r14),%rax
test   %rax,%rax
je     1206 <PKCS7_ctrl+0xc6>
mov    (%rax),%rax
xor    %ebx,%ebx
cmpl   $0x0,(%rax)
sete   %bl
mov    %rbx,(%r14)
jmp    11fe <PKCS7_ctrl+0xbe>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
jmp    11f5 <PKCS7_ctrl+0xb5>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1060 <PKCS7err@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x1,%ebx
mov    %rbx,(%r14)
jmp    11fe <PKCS7_ctrl+0xbe>

LLVM IR (-O0):
%struct.TYPE_10__ = type { i64, %struct.TYPE_9__, i32 }
%struct.TYPE_9__ = type { %struct.TYPE_8__* }
%struct.TYPE_8__ = type { %struct.TYPE_11__* }
%struct.TYPE_11__ = type { %struct.TYPE_7__ }
%struct.TYPE_7__ = type { i32, i32* }
@NID_pkcs7_signed = global i32 0
@PKCS7_F_PKCS7_CTRL = global i32 0
@PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE = global i32 0
@PKCS7_R_UNKNOWN_OPERATION = global i32 0
define i64 @PKCS7_ctrl(%struct.TYPE_10__* %0, i32 %1, i64 %2, i8* %3) {
%5 = alloca %struct.TYPE_10__*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32*
store %struct.TYPE_10__* %0, %struct.TYPE_10__** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i8* %3, i8** %8
%12 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%13 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %12, i32 0, i32 2
%14 = load i32, i32* %13
%15 = call i32 @OBJ_obj2nid(i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %6
switch i32 %16, label %96 [
i32 128, label %17
i32 129, label %64
]
17: ; preds = %4
%18 = load i32, i32* %9
%19 = load i32, i32* @NID_pkcs7_signed
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %59
21: ; preds = %17
%22 = load i64, i64* %7
%23 = trunc i64 %22 to i32
%24 = sext i32 %23 to i64
%25 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%26 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %25, i32 0, i32 0
store i64 %24, i64* %26
store i64 %24, i64* %10
%27 = load i64, i64* %10
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %58
29: ; preds = %21
%30 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%31 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %30, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %31, i32 0, i32 0
%33 = load %struct.TYPE_8__*, %struct.TYPE_8__** %32
%34 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %33, i32 0, i32 0
%35 = load %struct.TYPE_11__*, %struct.TYPE_11__** %34
%36 = call i32 @PKCS7_type_is_data(%struct.TYPE_11__* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %58
38: ; preds = %29
%39 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%40 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %39, i32 0, i32 1
%41 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %40, i32 0, i32 0
%42 = load %struct.TYPE_8__*, %struct.TYPE_8__** %41
%43 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %42, i32 0, i32 0
%44 = load %struct.TYPE_11__*, %struct.TYPE_11__** %43
%45 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %44, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %45, i32 0, i32 1
%47 = load i32*, i32** %46
store i32* %47, i32** %11
%48 = load i32*, i32** %11
%49 = call i32 @ASN1_OCTET_STRING_free(i32* %48)
%50 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%51 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %50, i32 0, i32 1
%52 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %51, i32 0, i32 0
%53 = load %struct.TYPE_8__*, %struct.TYPE_8__** %52
%54 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %53, i32 0, i32 0
%55 = load %struct.TYPE_11__*, %struct.TYPE_11__** %54
%56 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %55, i32 0, i32 0
%57 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %56, i32 0, i32 1
store i32* null, i32** %57
br label %58
58: ; preds = %38, %29, %21
br label %63
59: ; preds = %17
%60 = load i32, i32* @PKCS7_F_PKCS7_CTRL
%61 = load i32, i32* @PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE
%62 = call i32 @PKCS7err(i32 %60, i32 %61)
store i64 0, i64* %10
br label %63
63: ; preds = %59, %58
br label %100
64: ; preds = %4
%65 = load i32, i32* %9
%66 = load i32, i32* @NID_pkcs7_signed
%67 = icmp eq i32 %65, %66
br i1 %67, label %68, label %91
68: ; preds = %64
%69 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%70 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %69, i32 0, i32 1
%71 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %70, i32 0, i32 0
%72 = load %struct.TYPE_8__*, %struct.TYPE_8__** %71
%73 = icmp ne %struct.TYPE_8__* %72, null
br i1 %73, label %74, label %85
74: ; preds = %68
%75 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%76 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %75, i32 0, i32 1
%77 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %76, i32 0, i32 0
%78 = load %struct.TYPE_8__*, %struct.TYPE_8__** %77
%79 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %78, i32 0, i32 0
%80 = load %struct.TYPE_11__*, %struct.TYPE_11__** %79
%81 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %80, i32 0, i32 0
%82 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = icmp ne i32 %83, 0
br i1 %84, label %86, label %85
85: ; preds = %74, %68
store i64 1, i64* %10
br label %87
86: ; preds = %74
store i64 0, i64* %10
br label %87
87: ; preds = %86, %85
%88 = load i64, i64* %10
%89 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%90 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %89, i32 0, i32 0
store i64 %88, i64* %90
br label %95
91: ; preds = %64
%92 = load i32, i32* @PKCS7_F_PKCS7_CTRL
%93 = load i32, i32* @PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE
%94 = call i32 @PKCS7err(i32 %92, i32 %93)
store i64 0, i64* %10
br label %95
95: ; preds = %91, %87
br label %100
96: ; preds = %4
%97 = load i32, i32* @PKCS7_F_PKCS7_CTRL
%98 = load i32, i32* @PKCS7_R_UNKNOWN_OPERATION
%99 = call i32 @PKCS7err(i32 %97, i32 %98)
store i64 0, i64* %10
br label %100
100: ; preds = %96, %95, %63
%101 = load i64, i64* %10
ret i64 %101
}

LLVM IR (-O2):
%struct.TYPE_10__ = type { i64, %struct.TYPE_9__, i32 }
%struct.TYPE_9__ = type { %struct.TYPE_8__* }
%struct.TYPE_8__ = type { %struct.TYPE_11__* }
%struct.TYPE_11__ = type { %struct.TYPE_7__ }
%struct.TYPE_7__ = type { i32, i32* }
@NID_pkcs7_signed = global i32 0
@PKCS7_F_PKCS7_CTRL = global i32 0
@PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE = global i32 0
@PKCS7_R_UNKNOWN_OPERATION = global i32 0
define i64 @PKCS7_ctrl(%struct.TYPE_10__* nocapture %0, i32 %1, i64 %2, i8* nocapture readnone %3) {
%5 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %0, i64 0, i32 2
%6 = load i32, i32* %5
%7 = tail call i32 @OBJ_obj2nid(i32 %6)
switch i32 %1, label %59 [
i32 128, label %8
i32 129, label %38
]
8: ; preds = %4
%9 = load i32, i32* @NID_pkcs7_signed
%10 = icmp eq i32 %7, %9
br i1 %10, label %11, label %34
11: ; preds = %8
%12 = shl i64 %2, 32
%13 = ashr exact i64 %12, 32
%14 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %0, i64 0, i32 0
store i64 %13, i64* %14
%15 = icmp eq i64 %12, 0
br i1 %15, label %63, label %16
16: ; preds = %11
%17 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %0, i64 0, i32 1, i32 0
%18 = load %struct.TYPE_8__*, %struct.TYPE_8__** %17
%19 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %18, i64 0, i32 0
%20 = load %struct.TYPE_11__*, %struct.TYPE_11__** %19
%21 = tail call i32 @PKCS7_type_is_data(%struct.TYPE_11__* %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %63, label %23
23: ; preds = %16
%24 = load %struct.TYPE_8__*, %struct.TYPE_8__** %17
%25 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %24, i64 0, i32 0
%26 = load %struct.TYPE_11__*, %struct.TYPE_11__** %25
%27 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %26, i64 0, i32 0, i32 1
%28 = load i32*, i32** %27
%29 = tail call i32 @ASN1_OCTET_STRING_free(i32* %28)
%30 = load %struct.TYPE_8__*, %struct.TYPE_8__** %17
%31 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %30, i64 0, i32 0
%32 = load %struct.TYPE_11__*, %struct.TYPE_11__** %31
%33 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %32, i64 0, i32 0, i32 1
store i32* null, i32** %33
br label %63
34: ; preds = %8
%35 = load i32, i32* @PKCS7_F_PKCS7_CTRL
%36 = load i32, i32* @PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE
%37 = tail call i32 @PKCS7err(i32 %35, i32 %36)
br label %63
38: ; preds = %4
%39 = load i32, i32* @NID_pkcs7_signed
%40 = icmp eq i32 %7, %39
br i1 %40, label %41, label %55
41: ; preds = %38
%42 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %0, i64 0, i32 1, i32 0
%43 = load %struct.TYPE_8__*, %struct.TYPE_8__** %42
%44 = icmp eq %struct.TYPE_8__* %43, null
br i1 %44, label %52, label %45
45: ; preds = %41
%46 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %43, i64 0, i32 0
%47 = load %struct.TYPE_11__*, %struct.TYPE_11__** %46
%48 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %47, i64 0, i32 0, i32 0
%49 = load i32, i32* %48
%50 = icmp eq i32 %49, 0
%51 = zext i1 %50 to i64
br label %52
52: ; preds = %45, %41
%53 = phi i64 [ 1, %41 ], [ %51, %45 ]
%54 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %0, i64 0, i32 0
store i64 %53, i64* %54
br label %63
55: ; preds = %38
%56 = load i32, i32* @PKCS7_F_PKCS7_CTRL
%57 = load i32, i32* @PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE
%58 = tail call i32 @PKCS7err(i32 %56, i32 %57)
br label %63
59: ; preds = %4
%60 = load i32, i32* @PKCS7_F_PKCS7_CTRL
%61 = load i32, i32* @PKCS7_R_UNKNOWN_OPERATION
%62 = tail call i32 @PKCS7err(i32 %60, i32 %61)
br label %63
63: ; preds = %52, %55, %34, %23, %16, %11, %59
%64 = phi i64 [ 0, %59 ], [ %53, %52 ], [ 0, %55 ], [ %13, %23 ], [ %13, %16 ], [ 0, %11 ], [ 0, %34 ]
ret i64 %64
}