#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_REQ;
typedef int /*<<< orphan*/ X509_NAME;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef int /*<<< orphan*/ EVP_MD;

/* Variables and functions */
int /*<<< orphan*/ MBSTRING_ASC;
int /*<<< orphan*/ X509_NAME_add_entry_by_txt(int /*<<< orphan*/ *, char *,
                                              int /*<<< orphan*/,
                                              unsigned char *, int, int,
                                              int /*<<< orphan*/);
int /*<<< orphan*/ *X509_NAME_new();
int /*<<< orphan*/ *X509_REQ_new();
int /*<<< orphan*/ X509_REQ_set_pubkey(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_REQ_set_subject_name(int /*<<< orphan*/ *,
                                             int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_REQ_set_version(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ X509_REQ_sign(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/ const *);
int commonname_type(char *);
X509_REQ *makereq(EVP_PKEY *pkey, char *commonname, const EVP_MD *hash) {
  X509_REQ *req;
  X509_NAME *name;
  char *cnField;
  int type;
  type = commonname_type(commonname);
  switch (type) {
  case 1:
    cnField = "CN";
    break;
  case 2:
    cnField = "emailAddress";
    break;
  default:
    return NULL;
  }
  req = X509_REQ_new();
  X509_REQ_set_version(req, 2);
  X509_REQ_set_pubkey(req, pkey);
  name = X509_NAME_new();
  X509_NAME_add_entry_by_txt(name, cnField, MBSTRING_ASC,
                             (unsigned char *)commonname, -1, -1, 0);
  X509_REQ_set_subject_name(req, name);
  X509_REQ_sign(req, pkey, hash);
  return req;
}


x86 assembly (clang, -O0):
<makereq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <commonname_type@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0x1,%eax
je     11c5 <makereq+0x45>
jmp    11b4 <makereq+0x34>
mov    -0x40(%rbp),%eax
sub    $0x2,%eax
je     11d5 <makereq+0x55>
jmp    11e5 <makereq+0x65>
lea    0xe34(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11f2 <makereq+0x72>
lea    0xe27(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11f2 <makereq+0x72>
movq   $0x0,-0x8(%rbp)
jmp    1275 <makereq+0xf5>
mov    $0x0,%al
call   1050 <X509_REQ_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2,%esi
call   1060 <X509_REQ_set_version@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <X509_REQ_set_pubkey@plt>
mov    $0x0,%al
call   1070 <X509_NAME_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rcx
mov    $0xffffffff,%r9d
xor    %eax,%eax
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   1080 <X509_NAME_add_entry_by_txt@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <X509_REQ_set_subject_name@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <X509_REQ_sign@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makereq>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <commonname_type@plt>
cmp    $0x1,%eax
je     11b3 <makereq+0x33>
cmp    $0x2,%eax
jne    122b <makereq+0xab>
lea    0xe52(%rip),%r13
jmp    11ba <makereq+0x3a>
lea    0xe46(%rip),%r13
xor    %eax,%eax
call   1050 <X509_REQ_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2,%esi
call   1060 <X509_REQ_set_version@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10a0 <X509_REQ_set_pubkey@plt>
xor    %eax,%eax
call   1070 <X509_NAME_new@plt>
mov    %rax,%rbp
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
movl   $0x0,(%rsp)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r12,%rcx
mov    $0xffffffff,%r8d
mov    $0xffffffff,%r9d
call   1080 <X509_NAME_add_entry_by_txt@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <X509_REQ_set_subject_name@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <X509_REQ_sign@plt>
jmp    122d <makereq+0xad>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"CN\00"
@.str.1 = [13 x i8] c"emailAddress\00"
@MBSTRING_ASC = global i32 0
define i32* @makereq(i32* %0, i8* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%12 = load i8*, i8** %6
%13 = call i32 @commonname_type(i8* %12)
store i32 %13, i32* %11
%14 = load i32, i32* %11
switch i32 %14, label %17 [
i32 1, label %15
i32 2, label %16
]
15: ; preds = %3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %10
br label %18
16: ; preds = %3
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %18
17: ; preds = %3
store i32* null, i32** %4
br label %39
18: ; preds = %16, %15
%19 = call i32* (...) @X509_REQ_new()
store i32* %19, i32** %8
%20 = load i32*, i32** %8
%21 = call i32 @X509_REQ_set_version(i32* %20, i32 2)
%22 = load i32*, i32** %8
%23 = load i32*, i32** %5
%24 = call i32 @X509_REQ_set_pubkey(i32* %22, i32* %23)
%25 = call i32* (...) @X509_NAME_new()
store i32* %25, i32** %9
%26 = load i32*, i32** %9
%27 = load i8*, i8** %10
%28 = load i32, i32* @MBSTRING_ASC
%29 = load i8*, i8** %6
%30 = call i32 @X509_NAME_add_entry_by_txt(i32* %26, i8* %27, i32 %28, i8* %29, i32 -1, i32 -1, i32 0)
%31 = load i32*, i32** %8
%32 = load i32*, i32** %9
%33 = call i32 @X509_REQ_set_subject_name(i32* %31, i32* %32)
%34 = load i32*, i32** %8
%35 = load i32*, i32** %5
%36 = load i32*, i32** %7
%37 = call i32 @X509_REQ_sign(i32* %34, i32* %35, i32* %36)
%38 = load i32*, i32** %8
store i32* %38, i32** %4
br label %39
39: ; preds = %18, %17
%40 = load i32*, i32** %4
ret i32* %40
}

LLVM IR (-O2):
@.str = [3 x i8] c"CN\00"
@.str.1 = [13 x i8] c"emailAddress\00"
@MBSTRING_ASC = global i32 0
define i32* @makereq(i32* %0, i8* %1, i32* %2) {
%4 = tail call i32 @commonname_type(i8* %1)
switch i32 %4, label %16 [
i32 1, label %6
i32 2, label %5
]
5: ; preds = %3
br label %6
6: ; preds = %3, %5
%7 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %5 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %3 ]
%8 = tail call i32* (...) @X509_REQ_new()
%9 = tail call i32 @X509_REQ_set_version(i32* %8, i32 2)
%10 = tail call i32 @X509_REQ_set_pubkey(i32* %8, i32* %0)
%11 = tail call i32* (...) @X509_NAME_new()
%12 = load i32, i32* @MBSTRING_ASC
%13 = tail call i32 @X509_NAME_add_entry_by_txt(i32* %11, i8* %7, i32 %12, i8* %1, i32 -1, i32 -1, i32 0)
%14 = tail call i32 @X509_REQ_set_subject_name(i32* %8, i32* %11)
%15 = tail call i32 @X509_REQ_sign(i32* %8, i32* %0, i32* %2)
br label %16
16: ; preds = %3, %6
%17 = phi i32* [ %8, %6 ], [ null, %3 ]
ret i32* %17
}