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

/* Variables and functions */
int /*<<< orphan*/ ChallengeResponse(unsigned char *, unsigned char *,
                                     unsigned char *);
int MD4_DIGEST_LENGTH;
int /*<<< orphan*/ NtPasswordHash(unsigned char *, int, unsigned char *);
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, ...);
int SHA_DIGEST_LENGTH;
int /*<<< orphan*/ SHA_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int strlen(char *);
int ChapMS_v2(unsigned char *resp, unsigned char *chl, unsigned char *secret,
              int secret_len, char *username) {
  unsigned char pwdhash[MD4_DIGEST_LENGTH];
  SHA_CTX ctx;
  unsigned char digest[SHA_DIGEST_LENGTH];
  memset(resp, 0, 49);
  memset(resp, 0x66, 16);
  SHA_Init(&ctx);
  SHA1_Update(&ctx, resp, 16);
  SHA1_Update(&ctx, chl, 16);
  SHA1_Update(&ctx, username, strlen(username));
  SHA1_Final(digest, &ctx);
  NtPasswordHash(secret, secret_len, pwdhash);
  ChallengeResponse(digest, pwdhash, resp + 16 + 8);
  return 0;
}


x86 assembly (clang, -O0):
<ChapMS_v2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x31,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    $0x66,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x3c(%rbp),%rdi
call   1060 <SHA_Init@plt>
mov    -0x8(%rbp),%rsi
lea    -0x3c(%rbp),%rdi
mov    $0x10,%edx
mov    $0x0,%al
call   1070 <SHA1_Update@plt>
mov    -0x10(%rbp),%rsi
lea    -0x3c(%rbp),%rdi
mov    $0x10,%edx
mov    $0x0,%al
call   1070 <SHA1_Update@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
lea    -0x3c(%rbp),%rdi
mov    $0x0,%al
call   1070 <SHA1_Update@plt>
mov    -0x58(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
call   1090 <SHA1_Final@plt>
mov    -0x50(%rbp),%rdx
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <NtPasswordHash@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x8(%rbp),%rdx
add    $0x10,%rdx
add    $0x8,%rdx
call   1030 <ChallengeResponse@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ChapMS_v2>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r13
mov    %ecx,-0x2c(%rbp)
mov    %rdx,-0x40(%rbp)
mov    %rsi,-0x38(%rbp)
mov    %rdi,%rbx
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
xor    %esi,%esi
mov    $0x31,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    $0x66,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x44(%rbp),%r15
mov    %r15,%rdi
call   1060 <SHA_Init@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
xor    %eax,%eax
call   1070 <SHA1_Update@plt>
mov    %r15,%rdi
mov    -0x38(%rbp),%rsi
mov    $0x10,%edx
xor    %eax,%eax
call   1070 <SHA1_Update@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <SHA1_Update@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <SHA1_Final@plt>
mov    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    %r12,%rdx
call   1080 <NtPasswordHash@plt>
add    $0x18,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <ChallengeResponse@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MD4_DIGEST_LENGTH = global i32 0
@SHA_DIGEST_LENGTH = global i32 0
define i32 @ChapMS_v2(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%15 = load i32, i32* @MD4_DIGEST_LENGTH
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %11
%18 = alloca i8, i64 %16
store i64 %16, i64* %12
%19 = load i32, i32* @SHA_DIGEST_LENGTH
%20 = zext i32 %19 to i64
%21 = alloca i8, i64 %20
store i64 %20, i64* %14
%22 = load i8*, i8** %6
%23 = call i32 @memset(i8* %22, i32 0, i32 49)
%24 = load i8*, i8** %6
%25 = call i32 @memset(i8* %24, i32 102, i32 16)
%26 = call i32 @SHA_Init(i32* %13)
%27 = load i8*, i8** %6
%28 = call i32 (i32*, ...) @SHA1_Update(i32* %13, i8* %27, i32 16)
%29 = load i8*, i8** %7
%30 = call i32 (i32*, ...) @SHA1_Update(i32* %13, i8* %29, i32 16)
%31 = load i8*, i8** %10
%32 = load i8*, i8** %10
%33 = call i32 @strlen(i8* %32)
%34 = call i32 (i32*, ...) @SHA1_Update(i32* %13, i8* %31, i32 %33)
%35 = call i32 @SHA1_Final(i8* %21, i32* %13)
%36 = load i8*, i8** %8
%37 = load i32, i32* %9
%38 = call i32 @NtPasswordHash(i8* %36, i32 %37, i8* %18)
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 16
%41 = getelementptr inbounds i8, i8* %40, i64 8
%42 = call i32 @ChallengeResponse(i8* %21, i8* %18, i8* %41)
%43 = load i8*, i8** %11
ret i32 0
}

LLVM IR (-O2):
@MD4_DIGEST_LENGTH = global i32 0
@SHA_DIGEST_LENGTH = global i32 0
define i32 @ChapMS_v2(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = load i32, i32* @MD4_DIGEST_LENGTH
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast i32* %6 to i8*
%11 = load i32, i32* @SHA_DIGEST_LENGTH
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
%14 = tail call i32 @memset(i8* %0, i32 0, i32 49)
%15 = tail call i32 @memset(i8* %0, i32 102, i32 16)
%16 = call i32 @SHA_Init(i32* %6)
%17 = call i32 (i32*, ...) @SHA1_Update(i32* %6, i8* %0, i32 16)
%18 = call i32 (i32*, ...) @SHA1_Update(i32* %6, i8* %1, i32 16)
%19 = call i32 @strlen(i8* %4)
%20 = call i32 (i32*, ...) @SHA1_Update(i32* %6, i8* %4, i32 %19)
%21 = call i32 @SHA1_Final(i8* %13, i32* %6)
%22 = call i32 @NtPasswordHash(i8* %2, i32 %3, i8* %9)
%23 = getelementptr inbounds i8, i8* %0, i64 24
%24 = call i32 @ChallengeResponse(i8* %13, i8* %9, i8* %23)
ret i32 0
}