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

/* Variables and functions */
int /*<<< orphan*/ EVP_DigestFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestInit_ex(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ const *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DigestUpdate(int /*<<< orphan*/ *, unsigned char const *,
                                    int);
int EVP_MAX_MD_SIZE;
int /*<<< orphan*/ EVP_MD_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_new();
int EVP_MD_size(int /*<<< orphan*/ const *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, long);
int PKCS1_MGF1(unsigned char *mask, long len, const unsigned char *seed,
               long seedlen, const EVP_MD *dgst) {
  long i, outlen = 0;
  unsigned char cnt[4];
  EVP_MD_CTX *c = EVP_MD_CTX_new();
  unsigned char md[EVP_MAX_MD_SIZE];
  int mdlen;
  int rv = -1;
  if (c == NULL)
    goto err;
  mdlen = EVP_MD_size(dgst);
  if (mdlen < 0)
    goto err;
  for (i = 0; outlen < len; i++) {
    cnt[0] = (unsigned char)((i >> 24) & 255);
    cnt[1] = (unsigned char)((i >> 16) & 255);
    cnt[2] = (unsigned char)((i >> 8)) & 255;
    cnt[3] = (unsigned char)(i & 255);
    if (!EVP_DigestInit_ex(c, dgst, NULL) ||
        !EVP_DigestUpdate(c, seed, seedlen) || !EVP_DigestUpdate(c, cnt, 4))
      goto err;
    if (outlen + mdlen <= len) {
      if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
        goto err;
      outlen += mdlen;
    } else {
      if (!EVP_DigestFinal_ex(c, md, NULL))
        goto err;
      memcpy(mask + outlen, md, len - outlen);
      outlen = len;
    }
  }
  rv = 0;
err:
  EVP_MD_CTX_free(c);
  return rv;
}


x86 assembly (clang, -O0):
<PKCS1_MGF1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
xor    %eax,%eax
call   1090 <EVP_MD_CTX_new@plt>
mov    %rax,-0x48(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
movl   $0xffffffff,-0x60(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11e2 <PKCS1_MGF1+0x72>
jmp    1351 <PKCS1_MGF1+0x1e1>
mov    -0x28(%rbp),%rdi
call   1030 <EVP_MD_size@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    11fd <PKCS1_MGF1+0x8d>
jmp    1351 <PKCS1_MGF1+0x1e1>
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    134a <PKCS1_MGF1+0x1da>
mov    -0x30(%rbp),%rax
sar    $0x18,%rax
and    $0xff,%rax
mov    %al,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
sar    $0x10,%rax
and    $0xff,%rax
mov    %al,-0x3b(%rbp)
mov    -0x30(%rbp),%rax
sar    $0x8,%rax
movzbl %al,%eax
and    $0xff,%eax
mov    %al,-0x3a(%rbp)
mov    -0x30(%rbp),%rax
and    $0xff,%rax
mov    %al,-0x39(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <EVP_DigestInit_ex@plt>
cmp    $0x0,%eax
je     12a6 <PKCS1_MGF1+0x136>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1080 <EVP_DigestUpdate@plt>
cmp    $0x0,%eax
je     12a6 <PKCS1_MGF1+0x136>
mov    -0x48(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   1080 <EVP_DigestUpdate@plt>
cmp    $0x0,%eax
jne    12ab <PKCS1_MGF1+0x13b>
jmp    1351 <PKCS1_MGF1+0x1e1>
mov    -0x38(%rbp),%rax
movslq -0x5c(%rbp),%rcx
add    %rcx,%rax
cmp    -0x10(%rbp),%rax
jg     12f4 <PKCS1_MGF1+0x184>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <EVP_DigestFinal_ex@plt>
cmp    $0x0,%eax
jne    12e3 <PKCS1_MGF1+0x173>
jmp    1351 <PKCS1_MGF1+0x1e1>
movslq -0x5c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1334 <PKCS1_MGF1+0x1c4>
mov    -0x68(%rbp),%rsi
mov    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <EVP_DigestFinal_ex@plt>
cmp    $0x0,%eax
jne    1313 <PKCS1_MGF1+0x1a3>
jmp    1351 <PKCS1_MGF1+0x1e1>
mov    -0x68(%rbp),%rsi
mov    -0x8(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1339 <PKCS1_MGF1+0x1c9>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1205 <PKCS1_MGF1+0x95>
movl   $0x0,-0x60(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <EVP_MD_CTX_free@plt>
mov    -0x60(%rbp),%eax
mov    -0x50(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PKCS1_MGF1>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8,%r14
mov    %rcx,-0x68(%rbp)
mov    %rdx,-0x60(%rbp)
mov    %rsi,-0x38(%rbp)
mov    %rdi,-0x48(%rbp)
xor    %eax,%eax
call   1090 <EVP_MD_CTX_new@plt>
mov    %rax,%rbx
mov    %rsp,%r12
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    $0xffffffff,%r13d
test   %rbx,%rbx
je     12bc <PKCS1_MGF1+0x14c>
mov    %r14,%rdi
call   1030 <EVP_MD_size@plt>
test   %eax,%eax
js     12bc <PKCS1_MGF1+0x14c>
cmpq   $0x0,-0x38(%rbp)
jle    12b9 <PKCS1_MGF1+0x149>
mov    %r14,-0x58(%rbp)
mov    %r12,-0x30(%rbp)
mov    %eax,%eax
mov    %rax,-0x50(%rbp)
xor    %r13d,%r13d
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r13d,%eax
bswap  %eax
mov    %eax,-0x3c(%rbp)
mov    %rbx,%rdi
mov    -0x58(%rbp),%rsi
xor    %edx,%edx
call   1060 <EVP_DigestInit_ex@plt>
test   %eax,%eax
je     127c <PKCS1_MGF1+0x10c>
mov    %rbx,%rdi
mov    -0x60(%rbp),%rsi
mov    -0x68(%rbp),%rdx
call   1080 <EVP_DigestUpdate@plt>
test   %eax,%eax
je     127c <PKCS1_MGF1+0x10c>
mov    %rbx,%rdi
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   1080 <EVP_DigestUpdate@plt>
test   %eax,%eax
je     127c <PKCS1_MGF1+0x10c>
mov    -0x50(%rbp),%rax
lea    (%r14,%rax,1),%r12
cmp    -0x38(%rbp),%r12
jg     1288 <PKCS1_MGF1+0x118>
add    -0x48(%rbp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <EVP_DigestFinal_ex@plt>
test   %eax,%eax
je     127c <PKCS1_MGF1+0x10c>
add    $0x1,%r13
mov    %r12,%r14
cmp    -0x38(%rbp),%r12
jl     1200 <PKCS1_MGF1+0x90>
xor    %r13d,%r13d
mov    -0x30(%rbp),%r12
jmp    12bc <PKCS1_MGF1+0x14c>
mov    -0x30(%rbp),%r12
mov    $0xffffffff,%r13d
jmp    12bc <PKCS1_MGF1+0x14c>
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1050 <EVP_DigestFinal_ex@plt>
test   %eax,%eax
mov    -0x30(%rbp),%r12
mov    $0xffffffff,%r13d
je     12bc <PKCS1_MGF1+0x14c>
mov    -0x48(%rbp),%rdi
add    %r14,%rdi
mov    -0x38(%rbp),%rdx
sub    %r14,%rdx
mov    %r15,%rsi
call   1070 <memcpy@plt>
xor    %r13d,%r13d
mov    %rbx,%rdi
call   1040 <EVP_MD_CTX_free@plt>
mov    %r12,%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_MD_SIZE = global i32 0
define i32 @PKCS1_MGF1(i8* %0, i64 %1, i8* %2, i64 %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i64
%13 = alloca [4 x i8]
%14 = alloca i32*
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store i32* %4, i32** %10
store i64 0, i64* %12
%19 = call i32* (...) @EVP_MD_CTX_new()
store i32* %19, i32** %14
%20 = load i32, i32* @EVP_MAX_MD_SIZE
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %15
%23 = alloca i8, i64 %21
store i64 %21, i64* %16
store i32 -1, i32* %18
%24 = load i32*, i32** %14
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %5
br label %116
27: ; preds = %5
%28 = load i32*, i32** %10
%29 = call i32 @EVP_MD_size(i32* %28)
store i32 %29, i32* %17
%30 = load i32, i32* %17
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
br label %116
33: ; preds = %27
store i64 0, i64* %11
br label %34
34: ; preds = %112, %33
%35 = load i64, i64* %12
%36 = load i64, i64* %7
%37 = icmp slt i64 %35, %36
br i1 %37, label %38, label %115
38: ; preds = %34
%39 = load i64, i64* %11
%40 = ashr i64 %39, 24
%41 = and i64 %40, 255
%42 = trunc i64 %41 to i8
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 0
store i8 %42, i8* %43
%44 = load i64, i64* %11
%45 = ashr i64 %44, 16
%46 = and i64 %45, 255
%47 = trunc i64 %46 to i8
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 1
store i8 %47, i8* %48
%49 = load i64, i64* %11
%50 = ashr i64 %49, 8
%51 = trunc i64 %50 to i8
%52 = zext i8 %51 to i32
%53 = and i32 %52, 255
%54 = trunc i32 %53 to i8
%55 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 2
store i8 %54, i8* %55
%56 = load i64, i64* %11
%57 = and i64 %56, 255
%58 = trunc i64 %57 to i8
%59 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 3
store i8 %58, i8* %59
%60 = load i32*, i32** %14
%61 = load i32*, i32** %10
%62 = call i32 @EVP_DigestInit_ex(i32* %60, i32* %61, i32* null)
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %76
64: ; preds = %38
%65 = load i32*, i32** %14
%66 = load i8*, i8** %8
%67 = load i64, i64* %9
%68 = trunc i64 %67 to i32
%69 = call i32 @EVP_DigestUpdate(i32* %65, i8* %66, i32 %68)
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %64
%72 = load i32*, i32** %14
%73 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 0
%74 = call i32 @EVP_DigestUpdate(i32* %72, i8* %73, i32 4)
%75 = icmp ne i32 %74, 0
br i1 %75, label %77, label %76
76: ; preds = %71, %64, %38
br label %116
77: ; preds = %71
%78 = load i64, i64* %12
%79 = load i32, i32* %17
%80 = sext i32 %79 to i64
%81 = add nsw i64 %78, %80
%82 = load i64, i64* %7
%83 = icmp sle i64 %81, %82
br i1 %83, label %84, label %97
84: ; preds = %77
%85 = load i32*, i32** %14
%86 = load i8*, i8** %6
%87 = load i64, i64* %12
%88 = getelementptr inbounds i8, i8* %86, i64 %87
%89 = call i32 @EVP_DigestFinal_ex(i32* %85, i8* %88, i32* null)
%90 = icmp ne i32 %89, 0
br i1 %90, label %92, label %91
91: ; preds = %84
br label %116
92: ; preds = %84
%93 = load i32, i32* %17
%94 = sext i32 %93 to i64
%95 = load i64, i64* %12
%96 = add nsw i64 %95, %94
store i64 %96, i64* %12
br label %111
97: ; preds = %77
%98 = load i32*, i32** %14
%99 = call i32 @EVP_DigestFinal_ex(i32* %98, i8* %23, i32* null)
%100 = icmp ne i32 %99, 0
br i1 %100, label %102, label %101
101: ; preds = %97
br label %116
102: ; preds = %97
%103 = load i8*, i8** %6
%104 = load i64, i64* %12
%105 = getelementptr inbounds i8, i8* %103, i64 %104
%106 = load i64, i64* %7
%107 = load i64, i64* %12
%108 = sub nsw i64 %106, %107
%109 = call i32 @memcpy(i8* %105, i8* %23, i64 %108)
%110 = load i64, i64* %7
store i64 %110, i64* %12
br label %111
111: ; preds = %102, %92
br label %112
112: ; preds = %111
%113 = load i64, i64* %11
%114 = add nsw i64 %113, 1
store i64 %114, i64* %11
br label %34
115: ; preds = %34
store i32 0, i32* %18
br label %116
116: ; preds = %115, %101, %91, %76, %32, %26
%117 = load i32*, i32** %14
%118 = call i32 @EVP_MD_CTX_free(i32* %117)
%119 = load i32, i32* %18
%120 = load i8*, i8** %15
ret i32 %119
}

LLVM IR (-O2):
@EVP_MAX_MD_SIZE = global i32 0
define i32 @PKCS1_MGF1(i8* %0, i64 %1, i8* %2, i64 %3, i32* %4) {
%6 = alloca [4 x i8]
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%8 = tail call i32* (...) @EVP_MD_CTX_new()
%9 = load i32, i32* @EVP_MAX_MD_SIZE
%10 = zext i32 %9 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
%13 = icmp eq i32* %8, null
br i1 %13, label %60, label %14
14: ; preds = %5
%15 = tail call i32 @EVP_MD_size(i32* %4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %60, label %17
17: ; preds = %14
%18 = trunc i64 %3 to i32
%19 = icmp sgt i64 %1, 0
br i1 %19, label %20, label %60
20: ; preds = %17
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%24 = zext i32 %15 to i64
br label %25
25: ; preds = %20, %57
%26 = phi i64 [ 0, %20 ], [ %58, %57 ]
%27 = phi i64 [ 0, %20 ], [ %44, %57 ]
%28 = lshr i64 %26, 24
%29 = trunc i64 %28 to i8
store i8 %29, i8* %7
%30 = lshr i64 %26, 16
%31 = trunc i64 %30 to i8
store i8 %31, i8* %21
%32 = lshr i64 %26, 8
%33 = trunc i64 %32 to i8
store i8 %33, i8* %22
%34 = trunc i64 %26 to i8
store i8 %34, i8* %23
%35 = call i32 @EVP_DigestInit_ex(i32* %8, i32* %4, i32* null)
%36 = icmp eq i32 %35, 0
br i1 %36, label %60, label %37
37: ; preds = %25
%38 = call i32 @EVP_DigestUpdate(i32* %8, i8* %2, i32 %18)
%39 = icmp eq i32 %38, 0
br i1 %39, label %60, label %40
40: ; preds = %37
%41 = call i32 @EVP_DigestUpdate(i32* %8, i8* %7, i32 4)
%42 = icmp eq i32 %41, 0
br i1 %42, label %60, label %43
43: ; preds = %40
%44 = add nuw nsw i64 %27, %24
%45 = icmp sgt i64 %44, %1
br i1 %45, label %50, label %46
46: ; preds = %43
%47 = getelementptr inbounds i8, i8* %0, i64 %27
%48 = call i32 @EVP_DigestFinal_ex(i32* %8, i8* %47, i32* null)
%49 = icmp eq i32 %48, 0
br i1 %49, label %60, label %57
50: ; preds = %43
%51 = call i32 @EVP_DigestFinal_ex(i32* %8, i8* %12, i32* null)
%52 = icmp eq i32 %51, 0
br i1 %52, label %60, label %53
53: ; preds = %50
%54 = getelementptr inbounds i8, i8* %0, i64 %27
%55 = sub nsw i64 %1, %27
%56 = call i32 @memcpy(i8* %54, i8* %12, i64 %55)
br label %60
57: ; preds = %46
%58 = add nuw nsw i64 %26, 1
%59 = icmp slt i64 %44, %1
br i1 %59, label %25, label %60
60: ; preds = %40, %37, %25, %46, %57, %50, %53, %17, %14, %5
%61 = phi i32 [ -1, %5 ], [ -1, %14 ], [ 0, %17 ], [ -1, %50 ], [ 0, %53 ], [ -1, %40 ], [ -1, %37 ], [ -1, %25 ], [ -1, %46 ], [ 0, %57 ]
%62 = call i32 @EVP_MD_CTX_free(i32* %8)
ret i32 %61
}