#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 *, unsigned long, unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *,
                          unsigned long);
int /*<<< orphan*/ ull_to_bytes(unsigned char *, int, unsigned long);
void mgf1(unsigned char *out, unsigned long outlen, const unsigned char *in,
          unsigned long inlen) {
  unsigned char inbuf[inlen + 4];
  unsigned char outbuf[32];
  unsigned long i;
  memcpy(inbuf, in, inlen);
  for (i = 0; (i + 1) * 32 <= outlen; i++) {
    ull_to_bytes(inbuf + inlen, 4, i);
    SHA256(inbuf, inlen + 4, out);
    out += 32;
  }
  ull_to_bytes(inbuf + inlen, 4, i);
  SHA256(inbuf, inlen + 4, outbuf);
  memcpy(out, outbuf, outlen - i * 32);
}


x86 assembly (clang, -O0):
<mgf1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    %rcx,%rax
add    $0x4,%rax
mov    %rsp,%rdx
mov    %rdx,-0x28(%rbp)
add    $0x13,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
shl    $0x5,%rax
cmp    -0x10(%rbp),%rax
ja     11e6 <mgf1+0xb6>
mov    -0x60(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    $0x4,%esi
call   1040 <ull_to_bytes@plt>
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x8(%rbp),%rdx
call   1030 <SHA256@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x8(%rbp)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    1188 <mgf1+0x58>
mov    -0x60(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    $0x4,%esi
call   1040 <ull_to_bytes@plt>
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
lea    -0x50(%rbp),%rdx
call   1030 <SHA256@plt>
mov    -0x8(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x58(%rbp),%rax
shl    $0x5,%rax
sub    %rax,%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mgf1>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x4(%rcx),%r13
mov    %rsp,%r12
lea    0x13(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rdi
mov    %rdx,%rsi
mov    %rcx,%rdx
call   1050 <memcpy@plt>
cmp    $0x20,%rbx
mov    %r13,-0x38(%rbp)
jae    117f <mgf1+0x4f>
mov    %r12,%rax
xor    %r12d,%r12d
jmp    11e5 <mgf1+0xb5>
mov    %r14,-0x40(%rbp)
lea    (%r12,%r14,1),%rax
mov    %rax,-0x48(%rbp)
mov    $0x20,%r13d
mov    %r12,-0x30(%rbp)
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
lea    (%r15,%r13,1),%r14
add    $0xffffffffffffffe0,%r14
mov    -0x48(%rbp),%rdi
mov    $0x4,%esi
mov    %r12,%rdx
call   1040 <ull_to_bytes@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %r14,%rdx
call   1030 <SHA256@plt>
add    $0x1,%r12
add    $0x20,%r13
cmp    %rbx,%r13
jbe    11a0 <mgf1+0x70>
mov    -0x30(%rbp),%rax
add    %r13,%r15
add    $0xffffffffffffffe0,%r15
mov    -0x40(%rbp),%r14
mov    %rax,%r13
add    %rax,%r14
mov    %r14,%rdi
mov    $0x4,%esi
mov    %r12,%rdx
call   1040 <ull_to_bytes@plt>
lea    -0x70(%rbp),%r14
mov    %r13,%rdi
mov    -0x38(%rbp),%rsi
mov    %r14,%rdx
call   1030 <SHA256@plt>
shl    $0x5,%r12
sub    %r12,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @mgf1(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca [32 x i8]
%12 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%13 = load i64, i64* %8
%14 = add i64 %13, 4
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = load i8*, i8** %7
%18 = load i64, i64* %8
%19 = call i32 @memcpy(i8* %16, i8* %17, i64 %18)
store i64 0, i64* %12
br label %20
20: ; preds = %37, %4
%21 = load i64, i64* %12
%22 = add i64 %21, 1
%23 = mul i64 %22, 32
%24 = load i64, i64* %6
%25 = icmp ule i64 %23, %24
br i1 %25, label %26, label %40
26: ; preds = %20
%27 = load i64, i64* %8
%28 = getelementptr inbounds i8, i8* %16, i64 %27
%29 = load i64, i64* %12
%30 = call i32 @ull_to_bytes(i8* %28, i32 4, i64 %29)
%31 = load i64, i64* %8
%32 = add i64 %31, 4
%33 = load i8*, i8** %5
%34 = call i32 @SHA256(i8* %16, i64 %32, i8* %33)
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 32
store i8* %36, i8** %5
br label %37
37: ; preds = %26
%38 = load i64, i64* %12
%39 = add i64 %38, 1
store i64 %39, i64* %12
br label %20
40: ; preds = %20
%41 = load i64, i64* %8
%42 = getelementptr inbounds i8, i8* %16, i64 %41
%43 = load i64, i64* %12
%44 = call i32 @ull_to_bytes(i8* %42, i32 4, i64 %43)
%45 = load i64, i64* %8
%46 = add i64 %45, 4
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%48 = call i32 @SHA256(i8* %16, i64 %46, i8* %47)
%49 = load i8*, i8** %5
%50 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%51 = load i64, i64* %6
%52 = load i64, i64* %12
%53 = mul i64 %52, 32
%54 = sub i64 %51, %53
%55 = call i32 @memcpy(i8* %49, i8* %50, i64 %54)
%56 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
define void @mgf1(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca [32 x i8]
%6 = add i64 %3, 4
%7 = alloca i8, i64 %6
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%9 = call i32 @memcpy(i8* %7, i8* %2, i64 %3)
%10 = icmp ult i64 %1, 32
br i1 %10, label %23, label %11
11: ; preds = %4
%12 = getelementptr inbounds i8, i8* %7, i64 %3
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ 1, %11 ], [ %20, %13 ]
%15 = phi i64 [ 0, %11 ], [ %14, %13 ]
%16 = phi i8* [ %0, %11 ], [ %19, %13 ]
%17 = call i32 @ull_to_bytes(i8* %12, i32 4, i64 %15)
%18 = call i32 @SHA256(i8* %7, i64 %6, i8* %16)
%19 = getelementptr inbounds i8, i8* %16, i64 32
%20 = add i64 %14, 1
%21 = shl i64 %20, 5
%22 = icmp ugt i64 %21, %1
br i1 %22, label %23, label %13
23: ; preds = %13, %4
%24 = phi i8* [ %0, %4 ], [ %19, %13 ]
%25 = phi i64 [ 0, %4 ], [ %14, %13 ]
%26 = getelementptr inbounds i8, i8* %7, i64 %3
%27 = call i32 @ull_to_bytes(i8* %26, i32 4, i64 %25)
%28 = call i32 @SHA256(i8* %7, i64 %6, i8* %8)
%29 = mul i64 %25, -32
%30 = add i64 %29, %1
%31 = call i32 @memcpy(i8* %24, i8* %8, i64 %30)
ret void
}