#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 */

/* Variables and functions */
int /*<<< orphan*/ SHA256(unsigned char *, int, unsigned char *);
int SHA256_DIGEST_LENGTH;
int get_random();
int htonl(int);
unsigned char *malloc(size_t);
int /*<<< orphan*/ memcpy(unsigned char *, ...);
int gen_sha256_nonce(const unsigned char *mac, size_t mac_len, int boardId,
                     unsigned char *nonce, int *nonce_len) {
  unsigned char *d = NULL;
  int value, len;
  if (!(d = malloc(mac_len + sizeof(int) * 2))) {
    return -1;
  }
  len = 0;
  memcpy(d, mac, mac_len);
  len += mac_len;
  value = htonl(boardId);
  memcpy(d + len, &value, sizeof(int));
  len += sizeof(int);
  value = htonl(get_random());
  memcpy(d + len, &value, sizeof(int));
  len += sizeof(int);
  SHA256(d, len, nonce);
  *nonce_len = SHA256_DIGEST_LENGTH;
  return 0;
}


x86 assembly (clang, -O0):
<gen_sha256_nonce>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x8,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    119a <gen_sha256_nonce+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <gen_sha256_nonce+0x100>
movl   $0x0,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rcx
movslq -0x40(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
movslq -0x40(%rbp),%rax
add    %rax,%rdi
xor    %eax,%eax
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movslq -0x40(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <get_random@plt>
mov    %eax,%edi
call   1050 <htonl@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
movslq -0x40(%rbp),%rax
add    %rax,%rdi
xor    %eax,%eax
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movslq -0x40(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1030 <SHA256@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_sha256_nonce>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r12
mov    %rcx,%r15
mov    %edx,%r13d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0x8(%rsi),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     120d <gen_sha256_nonce+0xbd>
mov    %rax,%rbx
mov    %r12,0x10(%rsp)
xor    %r12d,%r12d
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <memcpy@plt>
mov    %r13d,%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
movslq %ebp,%rbp
lea    (%rbx,%rbp,1),%rdi
lea    0xc(%rsp),%r14
mov    $0x4,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <memcpy@plt>
xor    %eax,%eax
call   1040 <get_random@plt>
mov    %eax,%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
lea    0x4(%rbp),%eax
movslq %eax,%rdi
add    %rbx,%rdi
mov    $0x4,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <memcpy@plt>
add    $0x8,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
call   1030 <SHA256@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    0x10(%rsp),%rcx
mov    %eax,(%rcx)
jmp    1213 <gen_sha256_nonce+0xc3>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA256_DIGEST_LENGTH = global i32 0
define i32 @gen_sha256_nonce(i8* %0, i64 %1, i32 %2, i8* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32* %4, i32** %11
store i8* null, i8** %12
%15 = load i64, i64* %8
%16 = add i64 %15, 8
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %12
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %5
store i32 -1, i32* %6
br label %58
20: ; preds = %5
store i32 0, i32* %14
%21 = load i8*, i8** %12
%22 = load i8*, i8** %7
%23 = load i64, i64* %8
%24 = call i32 (i8*, ...) @memcpy(i8* %21, i8* %22, i64 %23)
%25 = load i64, i64* %8
%26 = load i32, i32* %14
%27 = sext i32 %26 to i64
%28 = add i64 %27, %25
%29 = trunc i64 %28 to i32
store i32 %29, i32* %14
%30 = load i32, i32* %9
%31 = call i32 @htonl(i32 %30)
store i32 %31, i32* %13
%32 = load i8*, i8** %12
%33 = load i32, i32* %14
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = call i32 (i8*, ...) @memcpy(i8* %35, i32* %13, i64 4)
%37 = load i32, i32* %14
%38 = sext i32 %37 to i64
%39 = add i64 %38, 4
%40 = trunc i64 %39 to i32
store i32 %40, i32* %14
%41 = call i32 (...) @get_random()
%42 = call i32 @htonl(i32 %41)
store i32 %42, i32* %13
%43 = load i8*, i8** %12
%44 = load i32, i32* %14
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = call i32 (i8*, ...) @memcpy(i8* %46, i32* %13, i64 4)
%48 = load i32, i32* %14
%49 = sext i32 %48 to i64
%50 = add i64 %49, 4
%51 = trunc i64 %50 to i32
store i32 %51, i32* %14
%52 = load i8*, i8** %12
%53 = load i32, i32* %14
%54 = load i8*, i8** %10
%55 = call i32 @SHA256(i8* %52, i32 %53, i8* %54)
%56 = load i32, i32* @SHA256_DIGEST_LENGTH
%57 = load i32*, i32** %11
store i32 %56, i32* %57
store i32 0, i32* %6
br label %58
58: ; preds = %20, %19
%59 = load i32, i32* %6
ret i32 %59
}

LLVM IR (-O2):
@SHA256_DIGEST_LENGTH = global i32 0
define i32 @gen_sha256_nonce(i8* %0, i64 %1, i32 %2, i8* %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = add i64 %1, 8
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %28, label %11
11: ; preds = %5
%12 = tail call i32 (i8*, ...) @memcpy(i8* %9, i8* %0, i64 %1)
%13 = tail call i32 @htonl(i32 %2)
store i32 %13, i32* %6
%14 = shl i64 %1, 32
%15 = ashr exact i64 %14, 32
%16 = getelementptr inbounds i8, i8* %9, i64 %15
%17 = call i32 (i8*, ...) @memcpy(i8* %16, i32* %6, i64 4)
%18 = call i32 (...) @get_random()
%19 = call i32 @htonl(i32 %18)
store i32 %19, i32* %6
%20 = add i64 %14, 17179869184
%21 = ashr exact i64 %20, 32
%22 = getelementptr inbounds i8, i8* %9, i64 %21
%23 = call i32 (i8*, ...) @memcpy(i8* %22, i32* %6, i64 4)
%24 = trunc i64 %21 to i32
%25 = add i32 %24, 4
%26 = call i32 @SHA256(i8* %9, i32 %25, i8* %3)
%27 = load i32, i32* @SHA256_DIGEST_LENGTH
store i32 %27, i32* %4
br label %28
28: ; preds = %5, %11
%29 = phi i32 [ 0, %11 ], [ -1, %5 ]
ret i32 %29
}