#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*/ uint8_t;
typedef scalar_t__ iconv_t;
typedef int /*<<< orphan*/ MD4_CTX;

/* Variables and functions */
int MD4_Final(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int MD4_Init(int /*<<< orphan*/ *);
int MD4_Update(int /*<<< orphan*/ *, char *, size_t);
char *calloc(size_t, int);
int /*<<< orphan*/ free(char *);
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char *);
size_t strlen(char *);
int encode_nt_key(char *newPasswd, uint8_t *nt_key) {
  int ret = 0;
  iconv_t cd;
  size_t cs, il, ol, sl;
  char *inc, *outc;
  char *ucs2Passwd;
  MD4_CTX md4ctx;
  cd = iconv_open("UCS-2LE", "UTF-8");
  if (cd == (iconv_t)(-1)) {
    ret = -1;
    goto done;
  }
  il = strlen(newPasswd);
  sl = ol = (il + 1) * 2;
  ucs2Passwd = calloc(ol, 1);
  if (!ucs2Passwd) {
    ret = -1;
    iconv_close(cd);
    goto done;
  }
  inc = newPasswd;
  outc = ucs2Passwd;
  cs = iconv(cd, &inc, &il, &outc, &ol);
  if (cs == -1) {
    ret = -1;
    free(ucs2Passwd);
    iconv_close(cd);
    goto done;
  }
  iconv_close(cd);
  sl -= ol;
  ret = MD4_Init(&md4ctx);
  if (ret == 0) {
    ret = -1;
    free(ucs2Passwd);
    goto done;
  }
  ret = MD4_Update(&md4ctx, ucs2Passwd, sl);
  if (ret == 0) {
    ret = -1;
    free(ucs2Passwd);
    goto done;
  }
  ret = MD4_Final(nt_key, &md4ctx);
  if (ret == 0) {
    ret = -1;
    free(ucs2Passwd);
    goto done;
  }
  ret = 0;
done:
  return ret;
}


x86 assembly (clang, -O0):
<encode_nt_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe52(%rip),%rdi
lea    0xe53(%rip),%rsi
call   10b0 <iconv_open@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11d5 <encode_nt_key+0x45>
movl   $0xffffffff,-0x14(%rbp)
jmp    132a <encode_nt_key+0x19a>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
shl    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1228 <encode_nt_key+0x98>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <iconv_close@plt>
jmp    132a <encode_nt_key+0x19a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x48(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x50(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    127e <encode_nt_key+0xee>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <iconv_close@plt>
jmp    132a <encode_nt_key+0x19a>
mov    -0x20(%rbp),%rdi
call   10a0 <iconv_close@plt>
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
lea    -0x5c(%rbp),%rdi
call   1070 <MD4_Init@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    12c1 <encode_nt_key+0x131>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
jmp    132a <encode_nt_key+0x19a>
mov    -0x58(%rbp),%rsi
mov    -0x40(%rbp),%rdx
lea    -0x5c(%rbp),%rdi
call   1060 <MD4_Update@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    12f4 <encode_nt_key+0x164>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
jmp    132a <encode_nt_key+0x19a>
mov    -0x10(%rbp),%rdi
lea    -0x5c(%rbp),%rsi
call   1090 <MD4_Final@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1323 <encode_nt_key+0x193>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
jmp    132a <encode_nt_key+0x19a>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_nt_key>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xe56(%rip),%rdi
lea    0xe57(%rip),%rsi
call   10b0 <iconv_open@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
cmp    $0xffffffffffffffff,%rbx
je     128d <encode_nt_key+0xfd>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,0x28(%rsp)
lea    (%rax,%rax,1),%r12
add    $0x2,%r12
mov    %r12,0x10(%rsp)
mov    %r12,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     1280 <encode_nt_key+0xf0>
mov    %rax,%r15
mov    %r13,0x20(%rsp)
mov    %rax,0x18(%rsp)
lea    0x20(%rsp),%rsi
lea    0x28(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
call   1040 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     1278 <encode_nt_key+0xe8>
mov    %rbx,%rdi
call   10a0 <iconv_close@plt>
mov    0x10(%rsp),%rbx
lea    0xc(%rsp),%rdi
call   1070 <MD4_Init@plt>
test   %eax,%eax
je     126e <encode_nt_key+0xde>
sub    %rbx,%r12
lea    0xc(%rsp),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <MD4_Update@plt>
test   %eax,%eax
je     126e <encode_nt_key+0xde>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
call   1090 <MD4_Final@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    128d <encode_nt_key+0xfd>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1288 <encode_nt_key+0xf8>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   10a0 <iconv_close@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"UCS-2LE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
define i32 @encode_nt_key(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %5
%15 = call i64 @iconv_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %5
br label %74
19: ; preds = %2
%20 = load i8*, i8** %3
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = add i64 %22, 1
%24 = mul i64 %23, 2
store i64 %24, i64* %9
store i64 %24, i64* %10
%25 = load i64, i64* %9
%26 = call i8* @calloc(i64 %25, i32 1)
store i8* %26, i8** %13
%27 = load i8*, i8** %13
%28 = icmp ne i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %19
store i32 -1, i32* %5
%30 = load i64, i64* %6
%31 = call i32 @iconv_close(i64 %30)
br label %74
32: ; preds = %19
%33 = load i8*, i8** %3
store i8* %33, i8** %11
%34 = load i8*, i8** %13
store i8* %34, i8** %12
%35 = load i64, i64* %6
%36 = call i64 @iconv(i64 %35, i8** %11, i64* %8, i8** %12, i64* %9)
store i64 %36, i64* %7
%37 = load i64, i64* %7
%38 = icmp eq i64 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %32
store i32 -1, i32* %5
%40 = load i8*, i8** %13
%41 = call i32 @free(i8* %40)
%42 = load i64, i64* %6
%43 = call i32 @iconv_close(i64 %42)
br label %74
44: ; preds = %32
%45 = load i64, i64* %6
%46 = call i32 @iconv_close(i64 %45)
%47 = load i64, i64* %9
%48 = load i64, i64* %10
%49 = sub i64 %48, %47
store i64 %49, i64* %10
%50 = call i32 @MD4_Init(i32* %14)
store i32 %50, i32* %5
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %44
store i32 -1, i32* %5
%54 = load i8*, i8** %13
%55 = call i32 @free(i8* %54)
br label %74
56: ; preds = %44
%57 = load i8*, i8** %13
%58 = load i64, i64* %10
%59 = call i32 @MD4_Update(i32* %14, i8* %57, i64 %58)
store i32 %59, i32* %5
%60 = load i32, i32* %5
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %56
store i32 -1, i32* %5
%63 = load i8*, i8** %13
%64 = call i32 @free(i8* %63)
br label %74
65: ; preds = %56
%66 = load i32*, i32** %4
%67 = call i32 @MD4_Final(i32* %66, i32* %14)
store i32 %67, i32* %5
%68 = load i32, i32* %5
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %65
store i32 -1, i32* %5
%71 = load i8*, i8** %13
%72 = call i32 @free(i8* %71)
br label %74
73: ; preds = %65
store i32 0, i32* %5
br label %74
74: ; preds = %73, %70, %62, %53, %39, %29, %18
%75 = load i32, i32* %5
ret i32 %75
}

LLVM IR (-O2):
@.str = [8 x i8] c"UCS-2LE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
define i32 @encode_nt_key(i8* %0, i32* %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = bitcast i64* %3 to i8*
%9 = bitcast i64* %4 to i8*
%10 = bitcast i8** %5 to i8*
%11 = bitcast i8** %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = tail call i64 @iconv_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, -1
br i1 %14, label %47, label %15
15: ; preds = %2
%16 = tail call i64 @strlen(i8* dereferenceable(1) %0)
store i64 %16, i64* %3
%17 = shl i64 %16, 1
%18 = add i64 %17, 2
store i64 %18, i64* %4
%19 = tail call i8* @calloc(i64 %18, i32 1)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = tail call i32 @iconv_close(i64 %13)
br label %47
23: ; preds = %15
store i8* %0, i8** %5
store i8* %19, i8** %6
%24 = call i64 @iconv(i64 %13, i8** %5, i64* %3, i8** %6, i64* %4)
%25 = icmp eq i64 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @free(i8* %19)
%28 = call i32 @iconv_close(i64 %13)
br label %47
29: ; preds = %23
%30 = call i32 @iconv_close(i64 %13)
%31 = load i64, i64* %4
%32 = call i32 @MD4_Init(i32* %7)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @free(i8* %19)
br label %47
36: ; preds = %29
%37 = sub i64 %18, %31
%38 = call i32 @MD4_Update(i32* %7, i8* %19, i64 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @free(i8* %19)
br label %47
42: ; preds = %36
%43 = call i32 @MD4_Final(i32* %1, i32* %7)
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @free(i8* %19)
br label %47
47: ; preds = %42, %2, %45, %40, %34, %26, %21
%48 = phi i32 [ -1, %26 ], [ -1, %34 ], [ -1, %40 ], [ -1, %45 ], [ -1, %21 ], [ -1, %2 ], [ 0, %42 ]
ret i32 %48
}