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

/* Variables and functions */
int /*<<< orphan*/ *BN_bin2bn(unsigned char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_to_ASN1_INTEGER(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ X509_get_serialNumber(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int set_serial128(X509 *cert) {
  FILE *urandom;
  BIGNUM *b_serial;
  unsigned char c_serial[16];
  urandom = fopen("/dev/urandom", "rb");
  fread(c_serial, 16, 1, urandom);
  fclose(urandom);
  c_serial[0] = 0x2c;
  c_serial[1] = 0xca;
  b_serial = BN_bin2bn(c_serial, 16, NULL);
  BN_to_ASN1_INTEGER(b_serial, X509_get_serialNumber(cert));
  BN_free(b_serial);
  return 0;
}


x86 assembly (clang, -O0):
<set_serial128>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe7d(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x10,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movb   $0x2c,-0x30(%rbp)
movb   $0xca,-0x2f(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x10,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <BN_bin2bn@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <X509_get_serialNumber@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1080 <BN_to_ASN1_INTEGER@plt>
mov    -0x18(%rbp),%rdi
call   1070 <BN_free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_serial128>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe7d(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movw   $0xca2c,(%rsp)
mov    %r15,%rdi
mov    $0x10,%esi
xor    %edx,%edx
call   1050 <BN_bin2bn@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <X509_get_serialNumber@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <BN_to_ASN1_INTEGER@plt>
mov    %rbx,%rdi
call   1070 <BN_free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @set_serial128(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [16 x i8]
store i32* %0, i32** %2
%6 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %3
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = load i32*, i32** %3
%9 = call i32 @fread(i8* %7, i32 16, i32 1, i32* %8)
%10 = load i32*, i32** %3
%11 = call i32 @fclose(i32* %10)
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
store i8 44, i8* %12
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 1
store i8 -54, i8* %13
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%15 = call i32* @BN_bin2bn(i8* %14, i32 16, i32* null)
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = load i32*, i32** %2
%18 = call i32 @X509_get_serialNumber(i32* %17)
%19 = call i32 @BN_to_ASN1_INTEGER(i32* %16, i32 %18)
%20 = load i32*, i32** %4
%21 = call i32 @BN_free(i32* %20)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @set_serial128(i32* %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 @fread(i8* %3, i32 16, i32 1, i32* %4)
%6 = tail call i32 @fclose(i32* %4)
store i8 44, i8* %3
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 1
store i8 -54, i8* %7
%8 = call i32* @BN_bin2bn(i8* %3, i32 16, i32* null)
%9 = call i32 @X509_get_serialNumber(i32* %0)
%10 = call i32 @BN_to_ASN1_INTEGER(i32* %8, i32 %9)
%11 = call i32 @BN_free(i32* %8)
ret i32 0
}