#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*/ rand;
typedef int /*<<< orphan*/ X509;
typedef int /*<<< orphan*/ BIGNUM;
typedef int /*<<< orphan*/ ASN1_INTEGER;

/* Variables and functions */
int /*<<< orphan*/ ASN1_INTEGER_set(int /*<<< orphan*/ *, unsigned int);
int /*<<< orphan*/ *ASN1_INTEGER_to_BN(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_add_word(int /*<<< orphan*/ *, unsigned int);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_to_ASN1_INTEGER(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_get_serialNumber(int /*<<< orphan*/ *);
int ssl_rand(unsigned int *, int);
int ssl_x509_serial_copyrand(X509 *dstcrt, X509 *srccrt) {
  ASN1_INTEGER *srcptr, *dstptr;
  BIGNUM *bnserial;
  unsigned int rand;
  int rv;
  rv = ssl_rand(&rand, sizeof(rand));
  dstptr = X509_get_serialNumber(dstcrt);
  srcptr = X509_get_serialNumber(srccrt);
  if ((rv == -1) || !dstptr || !srcptr)
    return -1;
  bnserial = ASN1_INTEGER_to_BN(srcptr, NULL);
  if (!bnserial) {
    ASN1_INTEGER_set(dstptr, rand);
  } else {
    BN_add_word(bnserial, rand);
    BN_to_ASN1_INTEGER(bnserial, dstptr);
    BN_free(bnserial);
  }
  return 0;
}


x86 assembly (clang, -O0):
<ssl_x509_serial_copyrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x34(%rbp),%rdi
mov    $0x4,%esi
call   1090 <ssl_rand@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <X509_get_serialNumber@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <X509_get_serialNumber@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
je     11cb <ssl_x509_serial_copyrand+0x5b>
cmpq   $0x0,-0x28(%rbp)
je     11cb <ssl_x509_serial_copyrand+0x5b>
cmpq   $0x0,-0x20(%rbp)
jne    11d7 <ssl_x509_serial_copyrand+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    122d <ssl_x509_serial_copyrand+0xbd>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <ASN1_INTEGER_to_BN@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1204 <ssl_x509_serial_copyrand+0x94>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <ASN1_INTEGER_set@plt>
jmp    1226 <ssl_x509_serial_copyrand+0xb6>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1040 <BN_add_word@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <BN_to_ASN1_INTEGER@plt>
mov    -0x30(%rbp),%rdi
call   1060 <BN_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssl_x509_serial_copyrand>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
call   1090 <ssl_rand@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <X509_get_serialNumber@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <X509_get_serialNumber@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%ebp
je     11f4 <ssl_x509_serial_copyrand+0x84>
test   %r15,%r15
je     11f4 <ssl_x509_serial_copyrand+0x84>
test   %rax,%rax
je     11f4 <ssl_x509_serial_copyrand+0x84>
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <ASN1_INTEGER_to_BN@plt>
mov    0x4(%rsp),%esi
test   %rax,%rax
je     11ec <ssl_x509_serial_copyrand+0x7c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <BN_add_word@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <BN_to_ASN1_INTEGER@plt>
mov    %rbx,%rdi
call   1060 <BN_free@plt>
jmp    11f4 <ssl_x509_serial_copyrand+0x84>
mov    %r15,%rdi
call   1070 <ASN1_INTEGER_set@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ssl_x509_serial_copyrand(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
%11 = call i32 @ssl_rand(i32* %9, i32 4)
store i32 %11, i32* %10
%12 = load i32*, i32** %4
%13 = call i32* @X509_get_serialNumber(i32* %12)
store i32* %13, i32** %7
%14 = load i32*, i32** %5
%15 = call i32* @X509_get_serialNumber(i32* %14)
store i32* %15, i32** %6
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, -1
br i1 %17, label %24, label %18
18: ; preds = %2
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %21, %18, %2
store i32 -1, i32* %3
br label %44
25: ; preds = %21
%26 = load i32*, i32** %6
%27 = call i32* @ASN1_INTEGER_to_BN(i32* %26, i32* null)
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = icmp ne i32* %28, null
br i1 %29, label %34, label %30
30: ; preds = %25
%31 = load i32*, i32** %7
%32 = load i32, i32* %9
%33 = call i32 @ASN1_INTEGER_set(i32* %31, i32 %32)
br label %43
34: ; preds = %25
%35 = load i32*, i32** %8
%36 = load i32, i32* %9
%37 = call i32 @BN_add_word(i32* %35, i32 %36)
%38 = load i32*, i32** %8
%39 = load i32*, i32** %7
%40 = call i32 @BN_to_ASN1_INTEGER(i32* %38, i32* %39)
%41 = load i32*, i32** %8
%42 = call i32 @BN_free(i32* %41)
br label %43
43: ; preds = %34, %30
store i32 0, i32* %3
br label %44
44: ; preds = %43, %24
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
define i32 @ssl_x509_serial_copyrand(i32* %0, i32* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @ssl_rand(i32* %3, i32 4)
%6 = call i32* @X509_get_serialNumber(i32* %0)
%7 = call i32* @X509_get_serialNumber(i32* %1)
%8 = icmp ne i32 %5, -1
%9 = icmp ne i32* %6, null
%10 = select i1 %8, i1 %9, i1 false
%11 = icmp ne i32* %7, null
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %13, label %23
13: ; preds = %2
%14 = call i32* @ASN1_INTEGER_to_BN(i32* %7, i32* null)
%15 = icmp eq i32* %14, null
%16 = load i32, i32* %3
br i1 %15, label %17, label %19
17: ; preds = %13
%18 = call i32 @ASN1_INTEGER_set(i32* %6, i32 %16)
br label %23
19: ; preds = %13
%20 = call i32 @BN_add_word(i32* %14, i32 %16)
%21 = call i32 @BN_to_ASN1_INTEGER(i32* %14, i32* %6)
%22 = call i32 @BN_free(i32* %14)
br label %23
23: ; preds = %17, %19, %2
%24 = phi i32 [ -1, %2 ], [ 0, %19 ], [ 0, %17 ]
ret i32 %24
}