#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 */
struct timespec {
  int tv_sec;
  int tv_nsec;
};
typedef int /*<<< orphan*/ salt;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
char *crypt(char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int random();
int /*<<< orphan*/ srandom(int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  static const char salt_chars[64] =
      "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  char salt[21];
  int i;
  if (argc < 4) {
    printf("Usage: passcrypt -l <256/512> <password>\n");
    exit(EXIT_FAILURE);
  }
  memset(salt, 0, sizeof(salt));
  if (strcmp(argv[2], "256") == 0) {
    strcpy(salt, "$5$");
  } else if (strcmp(argv[2], "512") == 0) {
    strcpy(salt, "$6$");
  } else {
    printf("Unknown SHA type.\n");
    exit(EXIT_FAILURE);
  }
  for (i = 3; i < 19; i++) {
    long r;
    struct timespec tp;
    clock_gettime(CLOCK_REALTIME, &tp);
    srandom(tp.tv_sec * tp.tv_nsec);
    r = random() % 64;
    salt[i] = salt_chars[r];
  }
  strcat(salt, "$");
  printf("%s\n", crypt(argv[3], salt));
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11dc <main+0x3c>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x15,%edx
call   1080 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe6f(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    121f <main+0x7f>
lea    -0x30(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1273 <main+0xd3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe44(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1252 <main+0xb2>
lea    -0x30(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1040 <strcpy@plt>
jmp    126e <main+0xce>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    1273 <main+0xd3>
movl   $0x3,-0x34(%rbp)
cmpl   $0x13,-0x34(%rbp)
jge    12dc <main+0x13c>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    -0x48(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x48(%rbp),%edi
imul   -0x44(%rbp),%edi
call   1030 <srandom@plt>
mov    $0x0,%al
call   10a0 <random@plt>
mov    $0x40,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rcx
lea    0xd3d(%rip),%rax
mov    (%rax,%rcx,1),%cl
movslq -0x34(%rbp),%rax
mov    %cl,-0x30(%rbp,%rax,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    127a <main+0xda>
lea    -0x30(%rbp),%rdi
lea    0xd89(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <crypt@plt>
mov    %rax,%rsi
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
cmp    $0x3,%edi
jg     11be <main+0x1e>
lea    0xea9(%rip),%rdi
jmp    12af <main+0x10f>
mov    %rsi,%r14
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x15,%edx
call   1080 <memset@plt>
mov    0x10(%r14),%rdi
lea    0xe63(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    11f0 <main+0x50>
lea    0xe56(%rip),%rsi
jmp    1210 <main+0x70>
mov    0x10(%r14),%rdi
lea    0xe4d(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    12a8 <main+0x108>
lea    0xe3c(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1040 <strcpy@plt>
mov    0x2db7(%rip),%r15
lea    0x8(%rsp),%r12
lea    0xdd3(%rip),%r13
xor    %ebx,%ebx
nop
mov    (%r15),%edi
mov    %r12,%rsi
call   1060 <clock_gettime@plt>
mov    0xc(%rsp),%edi
imul   0x8(%rsp),%edi
call   1030 <srandom@plt>
xor    %eax,%eax
call   10a0 <random@plt>
lea    0x3f(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffffffc0,%ecx
sub    %ecx,%eax
cltq
movzbl (%rax,%r13,1),%eax
mov    %al,0x13(%rsp,%rbx,1)
add    $0x1,%rbx
cmp    $0x10,%rbx
jne    1230 <main+0x90>
lea    0xdcd(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    0x18(%r14),%rdi
mov    %rbx,%rsi
call   1070 <crypt@plt>
mov    %rax,%rdi
call   1050 <puts@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xda1(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@main.salt_chars = internal constant [64 x i8] c"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
@.str = [42 x i8] c"Usage: passcrypt -l <256/512> <password>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"256\00"
@.str.2 = [4 x i8] c"$5$\00"
@.str.3 = [4 x i8] c"512\00"
@.str.4 = [4 x i8] c"$6$\00"
@.str.5 = [19 x i8] c"Unknown SHA type.\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.6 = [2 x i8] c"$\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [21 x i8]
%7 = alloca i32
%8 = alloca i64
%9 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 4
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 21)
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 0
%26 = call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %41
27: ; preds = %16
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 0
%35 = call i32 @strcpy(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %40
36: ; preds = %27
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %33
br label %41
41: ; preds = %40, %24
store i32 3, i32* %7
br label %42
42: ; preds = %63, %41
%43 = load i32, i32* %7
%44 = icmp slt i32 %43, 19
br i1 %44, label %45, label %66
45: ; preds = %42
%46 = load i32, i32* @CLOCK_REALTIME
%47 = call i32 @clock_gettime(i32 %46, %struct.timespec* %9)
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%49 = load i32, i32* %48
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%51 = load i32, i32* %50
%52 = mul nsw i32 %49, %51
%53 = call i32 @srandom(i32 %52)
%54 = call i32 (...) @random()
%55 = srem i32 %54, 64
%56 = sext i32 %55 to i64
store i64 %56, i64* %8
%57 = load i64, i64* %8
%58 = getelementptr inbounds [64 x i8], [64 x i8]* @main.salt_chars, i64 0, i64 %57
%59 = load i8, i8* %58
%60 = load i32, i32* %7
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 %61
store i8 %59, i8* %62
br label %63
63: ; preds = %45
%64 = load i32, i32* %7
%65 = add nsw i32 %64, 1
store i32 %65, i32* %7
br label %42
66: ; preds = %42
%67 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 0
%68 = call i32 @strcat(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 3
%71 = load i8*, i8** %70
%72 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 0
%73 = call i8* @crypt(i8* %71, i8* %72)
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %73)
%75 = load i32, i32* @EXIT_SUCCESS
%76 = call i32 @exit(i32 %75)
unreachable
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@main.salt_chars = internal unnamed_addr constant [64 x i8] c"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"256\00"
@.str.2 = [4 x i8] c"$5$\00"
@.str.3 = [4 x i8] c"512\00"
@.str.4 = [4 x i8] c"$6$\00"
@CLOCK_REALTIME = global i32 0
@.str.6 = [2 x i8] c"$\00"
@EXIT_SUCCESS = global i32 0
@str = [18 x i8] c"Unknown SHA type.\00"
@str.8 = [41 x i8] c"Usage: passcrypt -l <256/512> <password>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [21 x i8]
%4 = alloca %struct.timespec
%5 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%6 = icmp slt i32 %0, 4
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = call i32 @memset(i8* %5, i32 0, i32 21)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %11
%18 = load i8*, i8** %13
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %17, %11
%26 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), %17 ]
%27 = call i32 @strcpy(i8* %5, i8* %26)
%28 = bitcast %struct.timespec* %4 to i8*
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
br label %31
31: ; preds = %25, %31
%32 = phi i64 [ 3, %25 ], [ %45, %31 ]
%33 = load i32, i32* @CLOCK_REALTIME
%34 = call i32 @clock_gettime(i32 %33, %struct.timespec* %4)
%35 = load i32, i32* %29
%36 = load i32, i32* %30
%37 = mul nsw i32 %36, %35
%38 = call i32 @srandom(i32 %37)
%39 = call i32 (...) @random()
%40 = srem i32 %39, 64
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [64 x i8], [64 x i8]* @main.salt_chars, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 %32
store i8 %43, i8* %44
%45 = add nuw nsw i64 %32, 1
%46 = icmp eq i64 %45, 19
br i1 %46, label %47, label %31
47: ; preds = %31
%48 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%49 = getelementptr inbounds i8*, i8** %1, i64 3
%50 = load i8*, i8** %49
%51 = call i8* @crypt(i8* %50, i8* %5)
%52 = call i32 @puts(i8* dereferenceable(1) %51)
%53 = load i32, i32* @EXIT_SUCCESS
%54 = call i32 @exit(i32 %53)
unreachable
}