#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*/ sha1tmp;
typedef int /*<<< orphan*/ padtmp;
typedef int /*<<< orphan*/ SHA_CTX;

/* Variables and functions */
int EVP_CTRL_SSL3_MASTER_SECRET;
int /*<<< orphan*/ OPENSSL_cleanse(unsigned char *, int);
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
scalar_t__ SHA1_Update(int /*<<< orphan*/ *, unsigned char *, int);
int SHA_DIGEST_LENGTH;
int /*<<< orphan*/ memset(unsigned char *, int, int);
int sha1_ctrl(SHA_CTX *sha1, int cmd, int mslen, void *ms) {
  unsigned char padtmp[40];
  unsigned char sha1tmp[SHA_DIGEST_LENGTH];
  if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
    return -2;
  if (sha1 == NULL)
    return 0;
  if (mslen != 48)
    return 0;
  if (SHA1_Update(sha1, ms, mslen) <= 0)
    return 0;
  memset(padtmp, 0x36, sizeof(padtmp));
  if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
    return 0;
  if (!SHA1_Final(sha1tmp, sha1))
    return 0;
  if (!SHA1_Init(sha1))
    return 0;
  if (SHA1_Update(sha1, ms, mslen) <= 0)
    return 0;
  memset(padtmp, 0x5c, sizeof(padtmp));
  if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
    return 0;
  if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
    return 0;
  OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
  return 1;
}


x86 assembly (clang, -O0):
<sha1_ctrl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%eax
je     11be <sha1_ctrl+0x6e>
movl   $0xfffffffe,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
cmpq   $0x0,-0x10(%rbp)
jne    11dc <sha1_ctrl+0x8c>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
cmpl   $0x30,-0x18(%rbp)
je     11f9 <sha1_ctrl+0xa9>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <SHA1_Update@plt>
cmp    $0x0,%rax
jg     1226 <sha1_ctrl+0xd6>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
lea    -0x50(%rbp),%rdi
mov    $0x36,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x28,%edx
call   1050 <SHA1_Update@plt>
cmp    $0x0,%rax
jne    1268 <sha1_ctrl+0x118>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
mov    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <SHA1_Final@plt>
cmp    $0x0,%eax
jne    1291 <sha1_ctrl+0x141>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
mov    -0x10(%rbp),%rdi
call   1060 <SHA1_Init@plt>
cmp    $0x0,%eax
jne    12b6 <sha1_ctrl+0x166>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <SHA1_Update@plt>
cmp    $0x0,%rax
jg     12e3 <sha1_ctrl+0x193>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
lea    -0x50(%rbp),%rdi
mov    $0x5c,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x28,%edx
call   1050 <SHA1_Update@plt>
cmp    $0x0,%rax
jne    1325 <sha1_ctrl+0x1d5>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
mov    -0x70(%rbp),%rsi
mov    -0x78(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    %eax,%edx
call   1050 <SHA1_Update@plt>
cmp    $0x0,%rax
jne    1355 <sha1_ctrl+0x205>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1372 <sha1_ctrl+0x222>
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rax
mov    %eax,%esi
call   1040 <OPENSSL_cleanse@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha1_ctrl>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r13
mov    %rsp,%rcx
mov    0x2e7a(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e4f(%rip),%rax
mov    $0xfffffffe,%r15d
cmp    %esi,(%rax)
jne    1286 <sha1_ctrl+0x136>
mov    %rdi,%r12
xor    %r15d,%r15d
test   %rdi,%rdi
je     1286 <sha1_ctrl+0x136>
cmp    $0x30,%edx
jne    1286 <sha1_ctrl+0x136>
mov    %rcx,-0x30(%rbp)
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x30,%edx
call   1050 <SHA1_Update@plt>
test   %rax,%rax
jle    1282 <sha1_ctrl+0x132>
lea    -0x60(%rbp),%rdi
mov    $0x36,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    %r12,%rdi
lea    -0x60(%rbp),%rsi
mov    $0x28,%edx
call   1050 <SHA1_Update@plt>
test   %rax,%rax
je     1282 <sha1_ctrl+0x132>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <SHA1_Final@plt>
test   %eax,%eax
je     1282 <sha1_ctrl+0x132>
mov    %r12,%rdi
call   1060 <SHA1_Init@plt>
test   %eax,%eax
je     1282 <sha1_ctrl+0x132>
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x30,%edx
call   1050 <SHA1_Update@plt>
test   %rax,%rax
jle    1282 <sha1_ctrl+0x132>
lea    -0x60(%rbp),%r13
mov    %r13,%rdi
mov    $0x5c,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x28,%edx
call   1050 <SHA1_Update@plt>
test   %rax,%rax
je     1282 <sha1_ctrl+0x132>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <SHA1_Update@plt>
test   %rax,%rax
mov    -0x30(%rbp),%rcx
je     1286 <sha1_ctrl+0x136>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <OPENSSL_cleanse@plt>
mov    -0x30(%rbp),%rcx
mov    $0x1,%r15d
jmp    1286 <sha1_ctrl+0x136>
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA_DIGEST_LENGTH = global i32 0
@EVP_CTRL_SSL3_MASTER_SECRET = global i32 0
define i32 @sha1_ctrl(i32* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [40 x i8]
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%14 = load i32, i32* @SHA_DIGEST_LENGTH
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
%18 = load i32, i32* %7
%19 = load i32, i32* @EVP_CTRL_SSL3_MASTER_SECRET
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 -2, i32* %5
store i32 1, i32* %13
br label %79
22: ; preds = %4
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
26: ; preds = %22
%27 = load i32, i32* %8
%28 = icmp ne i32 %27, 48
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
30: ; preds = %26
%31 = load i32*, i32** %6
%32 = load i8*, i8** %9
%33 = load i32, i32* %8
%34 = call i64 @SHA1_Update(i32* %31, i8* %32, i32 %33)
%35 = icmp sle i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
37: ; preds = %30
%38 = getelementptr inbounds [40 x i8], [40 x i8]* %10, i64 0, i64 0
%39 = call i32 @memset(i8* %38, i32 54, i32 40)
%40 = load i32*, i32** %6
%41 = getelementptr inbounds [40 x i8], [40 x i8]* %10, i64 0, i64 0
%42 = call i64 @SHA1_Update(i32* %40, i8* %41, i32 40)
%43 = icmp ne i64 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %37
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
45: ; preds = %37
%46 = load i32*, i32** %6
%47 = call i32 @SHA1_Final(i8* %17, i32* %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %45
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
50: ; preds = %45
%51 = load i32*, i32** %6
%52 = call i32 @SHA1_Init(i32* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %55, label %54
54: ; preds = %50
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
55: ; preds = %50
%56 = load i32*, i32** %6
%57 = load i8*, i8** %9
%58 = load i32, i32* %8
%59 = call i64 @SHA1_Update(i32* %56, i8* %57, i32 %58)
%60 = icmp sle i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
62: ; preds = %55
%63 = getelementptr inbounds [40 x i8], [40 x i8]* %10, i64 0, i64 0
%64 = call i32 @memset(i8* %63, i32 92, i32 40)
%65 = load i32*, i32** %6
%66 = getelementptr inbounds [40 x i8], [40 x i8]* %10, i64 0, i64 0
%67 = call i64 @SHA1_Update(i32* %65, i8* %66, i32 40)
%68 = icmp ne i64 %67, 0
br i1 %68, label %70, label %69
69: ; preds = %62
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
70: ; preds = %62
%71 = load i32*, i32** %6
%72 = trunc i64 %15 to i32
%73 = call i64 @SHA1_Update(i32* %71, i8* %17, i32 %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %76, label %75
75: ; preds = %70
store i32 0, i32* %5
store i32 1, i32* %13
br label %79
76: ; preds = %70
%77 = trunc i64 %15 to i32
%78 = call i32 @OPENSSL_cleanse(i8* %17, i32 %77)
store i32 1, i32* %5
store i32 1, i32* %13
br label %79
79: ; preds = %76, %75, %69, %61, %54, %49, %44, %36, %29, %25, %21
%80 = load i8*, i8** %11
%81 = load i32, i32* %5
ret i32 %81
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
@EVP_CTRL_SSL3_MASTER_SECRET = global i32 0
define i32 @sha1_ctrl(i32* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca [40 x i8]
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @SHA_DIGEST_LENGTH
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = load i32, i32* @EVP_CTRL_SSL3_MASTER_SECRET
%12 = icmp eq i32 %11, %1
br i1 %12, label %13, label %42
13: ; preds = %4
%14 = icmp ne i32* %0, null
%15 = icmp eq i32 %2, 48
%16 = and i1 %14, %15
br i1 %16, label %17, label %42
17: ; preds = %13
%18 = tail call i64 @SHA1_Update(i32* %0, i8* %3, i32 48)
%19 = icmp slt i64 %18, 1
br i1 %19, label %42, label %20
20: ; preds = %17
%21 = call i32 @memset(i8* %6, i32 54, i32 40)
%22 = call i64 @SHA1_Update(i32* %0, i8* %6, i32 40)
%23 = icmp eq i64 %22, 0
br i1 %23, label %42, label %24
24: ; preds = %20
%25 = call i32 @SHA1_Final(i8* %10, i32* %0)
%26 = icmp eq i32 %25, 0
br i1 %26, label %42, label %27
27: ; preds = %24
%28 = call i32 @SHA1_Init(i32* %0)
%29 = icmp eq i32 %28, 0
br i1 %29, label %42, label %30
30: ; preds = %27
%31 = call i64 @SHA1_Update(i32* %0, i8* %3, i32 48)
%32 = icmp slt i64 %31, 1
br i1 %32, label %42, label %33
33: ; preds = %30
%34 = call i32 @memset(i8* %6, i32 92, i32 40)
%35 = call i64 @SHA1_Update(i32* %0, i8* %6, i32 40)
%36 = icmp eq i64 %35, 0
br i1 %36, label %42, label %37
37: ; preds = %33
%38 = call i64 @SHA1_Update(i32* %0, i8* %10, i32 %7)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @OPENSSL_cleanse(i8* %10, i32 %7)
br label %42
42: ; preds = %37, %33, %30, %27, %24, %20, %17, %13, %4, %40
%43 = phi i32 [ 1, %40 ], [ -2, %4 ], [ 0, %13 ], [ 0, %17 ], [ 0, %20 ], [ 0, %24 ], [ 0, %27 ], [ 0, %30 ], [ 0, %33 ], [ 0, %37 ]
ret i32 %43
}