#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 uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ tmp;

/* Variables and functions */
char *bin2hex(int *, char *, int);
int /*<<< orphan*/ md5(char const *, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
char *strchr(char *, char);
size_t strlen(char *);
void gen_psk(const char s1[], const char s2[]) {
  char ambig[] = "B8G6I1L0OQDS5Z2";
  char unambig[] = "3479ACEFHJKMNPRTUVWXYabcdefghijklmnopqrstuvwxyz";
  size_t tbl_len = strlen(unambig);
  char md5_str[32 + 1], tmp[64];
  uint8_t dgst[16];
  uint32_t p1;
  int idx, pos;
  char *s, c;
  printf("\n  WPA2-PSK : ");
  md5(s2, dgst);
  snprintf(tmp, sizeof(tmp), "%sPSK_%s", bin2hex(dgst, md5_str, 16), s1);
  md5(tmp, dgst);
  p1 = (dgst[0] << 8) | dgst[1];
  for (idx = 0; idx < 13; idx++) {
    if ((p1 >> idx) & 1) {
      c = (dgst[idx] % 26) + 'A';
    } else {
      c = (dgst[idx] % 10) + '0';
    }
    s = strchr(ambig, c);
    pos = (s == NULL) ? -1 : (s - ambig);
    if (pos != -1) {
      c = unambig[(p1 + pos) % tbl_len];
    }
    putchar(c);
  }
}


x86 assembly (clang, -O0):
<gen_psk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe66(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe5c(%rip),%rsi
mov    $0x30,%edx
call   1080 <memcpy@plt>
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x58(%rbp)
lea    0xe6e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x100(%rbp),%rsi
call   10a0 <md5@plt>
lea    -0xc0(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x100(%rbp),%rdi
lea    -0x80(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bin2hex@plt>
mov    -0x128(%rbp),%rdi
mov    %rax,%rcx
mov    -0x8(%rbp),%r8
mov    $0x40,%esi
lea    0xe2b(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0xc0(%rbp),%rdi
lea    -0x100(%rbp),%rsi
call   10a0 <md5@plt>
mov    -0x100(%rbp),%eax
shl    $0x8,%eax
or     -0xfc(%rbp),%eax
mov    %eax,-0x104(%rbp)
movl   $0x0,-0x108(%rbp)
cmpl   $0xd,-0x108(%rbp)
jge    1370 <gen_psk+0x1f0>
mov    -0x104(%rbp),%eax
mov    -0x108(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12a8 <gen_psk+0x128>
movslq -0x108(%rbp),%rax
mov    -0x100(%rbp,%rax,4),%eax
mov    $0x1a,%ecx
cltd
idiv   %ecx
add    $0x41,%edx
mov    %dl,%al
mov    %al,-0x119(%rbp)
jmp    12c9 <gen_psk+0x149>
movslq -0x108(%rbp),%rax
mov    -0x100(%rbp,%rax,4),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%al
mov    %al,-0x119(%rbp)
lea    -0x20(%rbp),%rdi
movsbl -0x119(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    1301 <gen_psk+0x181>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x130(%rbp)
jmp    1316 <gen_psk+0x196>
mov    -0x118(%rbp),%rax
lea    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rax
mov    %eax,-0x10c(%rbp)
cmpl   $0xffffffff,-0x10c(%rbp)
je     1350 <gen_psk+0x1d0>
mov    -0x104(%rbp),%eax
add    -0x10c(%rbp),%eax
cltq
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x58(%rbp)
mov    -0x50(%rbp,%rdx,1),%al
mov    %al,-0x119(%rbp)
movsbl -0x119(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x108(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x108(%rbp)
jmp    125b <gen_psk+0xdb>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_psk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
mov    %rsi,%rbp
mov    %rdi,%r14
movaps 0xe82(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xeb7(%rip),%rdi
call   1050 <printf@plt>
lea    0x10(%rsp),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <md5@plt>
lea    0x50(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   1070 <bin2hex@plt>
lea    0xe98(%rip),%rdx
lea    0x80(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %rax,%rcx
mov    %r14,%r8
call   1060 <snprintf@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <md5@plt>
mov    0x10(%rsp),%r15d
shl    $0x8,%r15d
or     0x14(%rsp),%r15d
xor    %ebx,%ebx
mov    $0x30,%r12d
mov    %rsp,%r14
lea    0xe13(%rip),%r13
jmp    1213 <gen_psk+0xb3>
nop
movsbl %bpl,%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    $0xd,%rbx
je     1290 <gen_psk+0x130>
mov    $0x1,%eax
mov    %ebx,%ecx
shl    %cl,%eax
xor    %ecx,%ecx
test   %r15d,%eax
setne  %cl
mov    0x10(%rsp,%rbx,4),%eax
mov    $0x41,%esi
cmove  %r12d,%esi
shl    $0x4,%ecx
or     $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,%ebp
add    %sil,%bpl
movsbl %bpl,%esi
mov    %r14,%rdi
call   1040 <strchr@plt>
test   %rax,%rax
je     1200 <gen_psk+0xa0>
sub    %r14d,%eax
cmp    $0xffffffff,%eax
je     1200 <gen_psk+0xa0>
add    %r15d,%eax
movslq %eax,%rcx
mov    %rcx,%rax
movabs $0x5c9882b931057263,%rdx
82 98 5c
mul    %rdx
mov    %rcx,%rax
sub    %rdx,%rax
shr    %rax
add    %rdx,%rax
shr    $0x5,%rax
imul   $0x2f,%rax,%rax
sub    %rax,%rcx
movzbl 0x0(%r13,%rcx,1),%ebp
jmp    1200 <gen_psk+0xa0>
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.gen_psk.ambig = [16 x i8] c"B8G6I1L0OQDS5Z2\00"
@__const.gen_psk.unambig = [48 x i8] c"3479ACEFHJKMNPRTUVWXYabcdefghijklmnopqrstuvwxyz\00"
@.str = [15 x i8] c"\0A WPA2-PSK : \00"
@.str.1 = [9 x i8] c"%sPSK_%s\00"
define void @gen_psk(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [16 x i8]
%6 = alloca [48 x i8]
%7 = alloca i64
%8 = alloca [33 x i8]
%9 = alloca [64 x i8]
%10 = alloca [16 x i32]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8
store i8* %0, i8** %3
store i8* %1, i8** %4
%16 = bitcast [16 x i8]* %5 to i8*
%17 = bitcast [48 x i8]* %6 to i8*
%18 = getelementptr inbounds [48 x i8], [48 x i8]* %6, i64 0, i64 0
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %7
%20 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = getelementptr inbounds [16 x i32], [16 x i32]* %10, i64 0, i64 0
%23 = call i32 @md5(i8* %21, i32* %22)
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%25 = getelementptr inbounds [16 x i32], [16 x i32]* %10, i64 0, i64 0
%26 = getelementptr inbounds [33 x i8], [33 x i8]* %8, i64 0, i64 0
%27 = call i8* @bin2hex(i32* %25, i8* %26, i32 16)
%28 = load i8*, i8** %3
%29 = call i32 @snprintf(i8* %24, i32 64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %28)
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%31 = getelementptr inbounds [16 x i32], [16 x i32]* %10, i64 0, i64 0
%32 = call i32 @md5(i8* %30, i32* %31)
%33 = getelementptr inbounds [16 x i32], [16 x i32]* %10, i64 0, i64 0
%34 = load i32, i32* %33
%35 = shl i32 %34, 8
%36 = getelementptr inbounds [16 x i32], [16 x i32]* %10, i64 0, i64 1
%37 = load i32, i32* %36
%38 = or i32 %35, %37
store i32 %38, i32* %11
store i32 0, i32* %12
br label %39
39: ; preds = %94, %2
%40 = load i32, i32* %12
%41 = icmp slt i32 %40, 13
br i1 %41, label %42, label %97
42: ; preds = %39
%43 = load i32, i32* %11
%44 = load i32, i32* %12
%45 = ashr i32 %43, %44
%46 = and i32 %45, 1
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %42
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [16 x i32], [16 x i32]* %10, i64 0, i64 %50
%52 = load i32, i32* %51
%53 = srem i32 %52, 26
%54 = add nsw i32 %53, 65
%55 = trunc i32 %54 to i8
store i8 %55, i8* %15
br label %64
56: ; preds = %42
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [16 x i32], [16 x i32]* %10, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = srem i32 %60, 10
%62 = add nsw i32 %61, 48
%63 = trunc i32 %62 to i8
store i8 %63, i8* %15
br label %64
64: ; preds = %56, %48
%65 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%66 = load i8, i8* %15
%67 = call i8* @strchr(i8* %65, i8 signext %66)
store i8* %67, i8** %14
%68 = load i8*, i8** %14
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %71
70: ; preds = %64
br label %77
71: ; preds = %64
%72 = load i8*, i8** %14
%73 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%74 = ptrtoint i8* %72 to i64
%75 = ptrtoint i8* %73 to i64
%76 = sub i64 %74, %75
br label %77
77: ; preds = %71, %70
%78 = phi i64 [ -1, %70 ], [ %76, %71 ]
%79 = trunc i64 %78 to i32
store i32 %79, i32* %13
%80 = load i32, i32* %13
%81 = icmp ne i32 %80, -1
br i1 %81, label %82, label %91
82: ; preds = %77
%83 = load i32, i32* %11
%84 = load i32, i32* %13
%85 = add nsw i32 %83, %84
%86 = sext i32 %85 to i64
%87 = load i64, i64* %7
%88 = urem i64 %86, %87
%89 = getelementptr inbounds [48 x i8], [48 x i8]* %6, i64 0, i64 %88
%90 = load i8, i8* %89
store i8 %90, i8* %15
br label %91
91: ; preds = %82, %77
%92 = load i8, i8* %15
%93 = call i32 @putchar(i8 signext %92)
br label %94
94: ; preds = %91
%95 = load i32, i32* %12
%96 = add nsw i32 %95, 1
store i32 %96, i32* %12
br label %39
97: ; preds = %39
ret void
}

LLVM IR (-O2):
@__const.gen_psk.ambig = [16 x i8] c"B8G6I1L0OQDS5Z2\00"
@__const.gen_psk.unambig = [48 x i8] c"3479ACEFHJKMNPRTUVWXYabcdefghijklmnopqrstuvwxyz\00"
@.str = [15 x i8] c"\0A WPA2-PSK : \00"
@.str.1 = [9 x i8] c"%sPSK_%s\00"
define void @gen_psk(i8* %0, i8* %1) {
%3 = alloca [16 x i8]
%4 = alloca [33 x i8]
%5 = alloca [64 x i8]
%6 = alloca [16 x i32]
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%10 = bitcast [16 x i32]* %6 to i8*
%11 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [16 x i32], [16 x i32]* %6, i64 0, i64 0
%13 = call i32 @md5(i8* %1, i32* %12)
%14 = call i8* @bin2hex(i32* %12, i8* %8, i32 16)
%15 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %0)
%16 = call i32 @md5(i8* %9, i32* %12)
%17 = load i32, i32* %12
%18 = shl i32 %17, 8
%19 = getelementptr inbounds [16 x i32], [16 x i32]* %6, i64 0, i64 1
%20 = load i32, i32* %19
%21 = or i32 %18, %20
%22 = ptrtoint [16 x i8]* %3 to i64
br label %23
23: ; preds = %2, %49
%24 = phi i64 [ 0, %2 ], [ %52, %49 ]
%25 = trunc i64 %24 to i32
%26 = shl nuw nsw i32 1, %25
%27 = and i32 %26, %21
%28 = icmp eq i32 %27, 0
%29 = getelementptr inbounds [16 x i32], [16 x i32]* %6, i64 0, i64 %24
%30 = load i32, i32* %29
%31 = select i1 %28, i32 10, i32 26
%32 = select i1 %28, i8 48, i8 65
%33 = srem i32 %30, %31
%34 = trunc i32 %33 to i8
%35 = add nsw i8 %32, %34
%36 = call i8* @strchr(i8* %7, i8 signext %35)
%37 = icmp eq i8* %36, null
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %38, %22
%40 = trunc i64 %39 to i32
%41 = icmp eq i32 %40, -1
%42 = select i1 %37, i1 true, i1 %41
br i1 %42, label %49, label %43
43: ; preds = %23
%44 = add nsw i32 %21, %40
%45 = sext i32 %44 to i64
%46 = urem i64 %45, 47
%47 = getelementptr inbounds [48 x i8], [48 x i8]* @__const.gen_psk.unambig, i64 0, i64 %46
%48 = load i8, i8* %47
br label %49
49: ; preds = %43, %23
%50 = phi i8 [ %48, %43 ], [ %35, %23 ]
%51 = call i32 @putchar(i8 signext %50)
%52 = add nuw nsw i64 %24, 1
%53 = icmp eq i64 %52, 13
br i1 %53, label %54, label %23
54: ; preds = %49
ret void
}