#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*/);
#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*/ PKCS7error(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 {
      PKCS7error(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 {
      PKCS7error(PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
      ret = 0;
    }
    break;
  default:
    PKCS7error(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     1225 <PKCS7_ctrl+0xe5>
jmp    129a <PKCS7_ctrl+0x15a>
mov    -0x24(%rbp),%eax
mov    0x2e2f(%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    1220 <PKCS7_ctrl+0xe0>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS7error@plt>
movq   $0x0,-0x30(%rbp)
jmp    12b0 <PKCS7_ctrl+0x170>
mov    -0x24(%rbp),%eax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%eax
jne    127f <PKCS7_ctrl+0x13f>
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     125a <PKCS7_ctrl+0x11a>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
cmpl   $0x0,(%rax)
jne    1267 <PKCS7_ctrl+0x127>
movq   $0x1,-0x30(%rbp)
jmp    126f <PKCS7_ctrl+0x12f>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1295 <PKCS7_ctrl+0x155>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS7error@plt>
movq   $0x0,-0x30(%rbp)
jmp    12b0 <PKCS7_ctrl+0x170>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS7error@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    0x2e5d(%rip),%rcx
cmp    (%rcx),%eax
jne    11dc <PKCS7_ctrl+0x9c>
mov    %rbx,%rax
shl    $0x20,%rax
movslq %ebx,%rbx
mov    %rbx,(%r14)
je     11ea <PKCS7_ctrl+0xaa>
mov    0x8(%r14),%rax
mov    (%rax),%rdi
call   1050 <PKCS7_type_is_data@plt>
test   %eax,%eax
je     11ec <PKCS7_ctrl+0xac>
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    11ec <PKCS7_ctrl+0xac>
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%eax
jne    11dc <PKCS7_ctrl+0x9c>
mov    0x8(%r14),%rax
test   %rax,%rax
je     11f4 <PKCS7_ctrl+0xb4>
mov    (%rax),%rax
xor    %ebx,%ebx
cmpl   $0x0,(%rax)
sete   %bl
mov    %rbx,(%r14)
jmp    11ec <PKCS7_ctrl+0xac>
mov    0x2e06(%rip),%rax
jmp    11e3 <PKCS7_ctrl+0xa3>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1060 <PKCS7error@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x1,%ebx
mov    %rbx,(%r14)
jmp    11ec <PKCS7_ctrl+0xac>

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_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 %94 [
i32 128, label %17
i32 129, label %63
]
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 %62
59: ; preds = %17
%60 = load i32, i32* @PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE
%61 = call i32 @PKCS7error(i32 %60)
store i64 0, i64* %10
br label %62
62: ; preds = %59, %58
br label %97
63: ; preds = %4
%64 = load i32, i32* %9
%65 = load i32, i32* @NID_pkcs7_signed
%66 = icmp eq i32 %64, %65
br i1 %66, label %67, label %90
67: ; preds = %63
%68 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%69 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %68, i32 0, i32 1
%70 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %69, i32 0, i32 0
%71 = load %struct.TYPE_8__*, %struct.TYPE_8__** %70
%72 = icmp ne %struct.TYPE_8__* %71, null
br i1 %72, label %73, label %84
73: ; preds = %67
%74 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%75 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %74, i32 0, i32 1
%76 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %75, i32 0, i32 0
%77 = load %struct.TYPE_8__*, %struct.TYPE_8__** %76
%78 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %77, i32 0, i32 0
%79 = load %struct.TYPE_11__*, %struct.TYPE_11__** %78
%80 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %79, i32 0, i32 0
%81 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %80, i32 0, i32 0
%82 = load i32, i32* %81
%83 = icmp ne i32 %82, 0
br i1 %83, label %85, label %84
84: ; preds = %73, %67
store i64 1, i64* %10
br label %86
85: ; preds = %73
store i64 0, i64* %10
br label %86
86: ; preds = %85, %84
%87 = load i64, i64* %10
%88 = load %struct.TYPE_10__*, %struct.TYPE_10__** %5
%89 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %88, i32 0, i32 0
store i64 %87, i64* %89
br label %93
90: ; preds = %63
%91 = load i32, i32* @PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE
%92 = call i32 @PKCS7error(i32 %91)
store i64 0, i64* %10
br label %93
93: ; preds = %90, %86
br label %97
94: ; preds = %4
%95 = load i32, i32* @PKCS7_R_UNKNOWN_OPERATION
%96 = call i32 @PKCS7error(i32 %95)
store i64 0, i64* %10
br label %97
97: ; preds = %94, %93, %62
%98 = load i64, i64* %10
ret i64 %98
}

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_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 %57 [
i32 128, label %8
i32 129, label %37
]
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 %60, 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 %60, 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 %60
34: ; preds = %8
%35 = load i32, i32* @PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE
%36 = tail call i32 @PKCS7error(i32 %35)
br label %60
37: ; preds = %4
%38 = load i32, i32* @NID_pkcs7_signed
%39 = icmp eq i32 %7, %38
br i1 %39, label %40, label %54
40: ; preds = %37
%41 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %0, i64 0, i32 1, i32 0
%42 = load %struct.TYPE_8__*, %struct.TYPE_8__** %41
%43 = icmp eq %struct.TYPE_8__* %42, null
br i1 %43, label %51, label %44
44: ; preds = %40
%45 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %42, i64 0, i32 0
%46 = load %struct.TYPE_11__*, %struct.TYPE_11__** %45
%47 = getelementptr inbounds %struct.TYPE_11__, %struct.TYPE_11__* %46, i64 0, i32 0, i32 0
%48 = load i32, i32* %47
%49 = icmp eq i32 %48, 0
%50 = zext i1 %49 to i64
br label %51
51: ; preds = %44, %40
%52 = phi i64 [ 1, %40 ], [ %50, %44 ]
%53 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %0, i64 0, i32 0
store i64 %52, i64* %53
br label %60
54: ; preds = %37
%55 = load i32, i32* @PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE
%56 = tail call i32 @PKCS7error(i32 %55)
br label %60
57: ; preds = %4
%58 = load i32, i32* @PKCS7_R_UNKNOWN_OPERATION
%59 = tail call i32 @PKCS7error(i32 %58)
br label %60
60: ; preds = %51, %54, %34, %23, %16, %11, %57
%61 = phi i64 [ 0, %57 ], [ %52, %51 ], [ 0, %54 ], [ %13, %23 ], [ %13, %16 ], [ 0, %11 ], [ 0, %34 ]
ret i64 %61
}