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

/* Variables and functions */
int atoi(char *);
char *crypt(char *, char *);
int /*<<< orphan*/ exit(int);
int getopt(int, char **, char *);
char *getpass(char *);
char *make_des_salt();
char *make_md5_salt(int);
char *make_md5_salt_para(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ srandom(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  char *plaintext = NULL;
  extern char *optarg;
  extern int optind;
  int c;
  char *saltpara = NULL;
  char *salt;
  int flag = 0;
  int length = 8;
  srandom(time(NULL));
  while ((c = getopt(argc, argv, "mdh?l:s:p:")) != -1) {
    switch (c) {
    case 'm':
      flag |= 0x00000001;
      break;
    case 'd':
      flag |= 0x00000002;
      break;
    case 'l':
      flag |= 0x00000010;
      length = atoi(optarg);
      break;
    case 's':
      flag |= 0x00000004;
      saltpara = optarg;
      break;
    case 'p':
      flag |= 0x00000008;
      plaintext = optarg;
      break;
    case 'h':
    case '?':
      usage();
      break;
    default:
      printf("Invalid Option: -%c\n", c);
      break;
    }
  }
  if (flag & 0x00000001) {
    if (flag & 0x00000004)
      salt = make_md5_salt_para(saltpara);
    else
      salt = make_md5_salt(length);
  } else {
    if (flag & 0x00000004) {
      if ((strlen(saltpara) == 2)) {
        salt = saltpara;
      } else {
        printf("Invalid salt, please enter 2 alphanumeric characters\n");
        exit(1);
      }
    } else {
      salt = make_des_salt();
    }
  }
  if (flag & 0x00000008) {
    if (!plaintext)
      printf("Please enter a valid password\n");
  } else {
    plaintext = getpass("plaintext: ");
  }
  printf("%s\n", crypt(plaintext, salt));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x8,-0x38(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xeb2(%rip),%rdx
call   10d0 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     12f2 <main+0x122>
mov    -0x1c(%rbp),%eax
add    $0xffffffc1,%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
sub    $0x34,%eax
ja     12dc <main+0x10c>
mov    -0x40(%rbp),%rax
lea    0xdad(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x34(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12ed <main+0x11d>
mov    -0x34(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x34(%rbp)
jmp    12ed <main+0x11d>
mov    -0x34(%rbp),%eax
or     $0x10,%eax
mov    %eax,-0x34(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x38(%rbp)
jmp    12ed <main+0x11d>
mov    -0x34(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0x34(%rbp)
mov    0x2d38(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    12ed <main+0x11d>
mov    -0x34(%rbp),%eax
or     $0x8,%eax
mov    %eax,-0x34(%rbp)
mov    0x2d1c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    12ed <main+0x11d>
mov    $0x0,%al
call   10a0 <usage@plt>
jmp    12ed <main+0x11d>
mov    -0x1c(%rbp),%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1214 <main+0x44>
mov    -0x34(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1333 <main+0x163>
mov    -0x34(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1322 <main+0x152>
mov    -0x28(%rbp),%rdi
call   1090 <make_md5_salt_para@plt>
mov    %rax,-0x30(%rbp)
jmp    132e <main+0x15e>
mov    -0x38(%rbp),%edi
call   10b0 <make_md5_salt@plt>
mov    %rax,-0x30(%rbp)
jmp    138e <main+0x1be>
mov    -0x34(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     137e <main+0x1ae>
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x2,%eax
jne    1361 <main+0x191>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1379 <main+0x1a9>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    1389 <main+0x1b9>
mov    $0x0,%al
call   1050 <make_des_salt@plt>
mov    %rax,-0x30(%rbp)
jmp    138e <main+0x1be>
mov    -0x34(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     13bb <main+0x1eb>
cmpq   $0x0,-0x18(%rbp)
jne    13b6 <main+0x1e6>
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13cb <main+0x1fb>
lea    0xd87(%rip),%rdi
call   10c0 <getpass@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <crypt@plt>
mov    %rax,%rsi
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
movl   $0x8,0x4(%rsp)
lea    0xec4(%rip),%r13
lea    0xec8(%rip),%r12
lea    0xde2(%rip),%r15
xor    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %eax,%eax
mov    %rax,0x8(%rsp)
xor    %r14d,%r14d
jmp    1246 <main+0x66>
cmp    $0xffffffff,%eax
je     12bc <main+0xdc>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   10e0 <getopt@plt>
lea    -0x3f(%rax),%ecx
cmp    $0x34,%ecx
ja     1231 <main+0x51>
movslq (%r15,%rcx,4),%rcx
add    %r15,%rcx
jmp    *%rcx
xor    %eax,%eax
call   10b0 <usage@plt>
jmp    1246 <main+0x66>
or     $0x10,%r14d
mov    0x2d68(%rip),%rax
mov    (%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,0x4(%rsp)
jmp    1246 <main+0x66>
or     $0x1,%r14d
jmp    1246 <main+0x66>
or     $0x8,%r14d
mov    0x2d49(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
jmp    1246 <main+0x66>
or     $0x4,%r14d
mov    0x2d34(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
jmp    1246 <main+0x66>
or     $0x2,%r14d
jmp    1246 <main+0x66>
test   $0x1,%r14b
jne    12d6 <main+0xf6>
test   $0x4,%r14b
mov    0x8(%rsp),%rbx
jne    12ec <main+0x10c>
xor    %eax,%eax
call   1060 <make_des_salt@plt>
jmp    1315 <main+0x135>
test   $0x4,%r14b
mov    0x8(%rsp),%rdi
jne    1310 <main+0x130>
mov    0x4(%rsp),%edi
call   10c0 <make_md5_salt@plt>
jmp    1315 <main+0x135>
mov    %rbx,%rdi
call   1070 <strlen@plt>
cmp    $0x2,%eax
jne    1357 <main+0x177>
test   $0x8,%r14b
jne    131e <main+0x13e>
lea    0xdee(%rip),%rdi
call   10d0 <getpass@plt>
mov    %rax,%rdi
jmp    1336 <main+0x156>
call   10a0 <make_md5_salt_para@plt>
mov    %rax,%rbx
test   $0x8,%r14b
je     12ff <main+0x11f>
mov    0x10(%rsp),%rdi
test   %rdi,%rdi
jne    1336 <main+0x156>
lea    0xdd1(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <crypt@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"mdh?l:s:p:\00"
@optarg = external global i8*
@.str.1 = [21 x i8] c"Invalid Option: -%c\0A\00"
@.str.2 = [54 x i8] c"Invalid salt, please enter 2 alphanumeric characters\0A\00"
@.str.3 = [31 x i8] c"Please enter a valid password\0A\00"
@.str.4 = [12 x i8] c"plaintext: \00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %8
store i32 0, i32* %10
store i32 8, i32* %11
%12 = call i32 @time(i32* null)
%13 = call i32 @srandom(i32 %12)
br label %14
14: ; preds = %45, %2
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = call i32 @getopt(i32 %15, i8** %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %7
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %46
19: ; preds = %14
%20 = load i32, i32* %7
switch i32 %20, label %42 [
i32 109, label %21
i32 100, label %24
i32 108, label %27
i32 115, label %32
i32 112, label %36
i32 104, label %40
i32 63, label %40
]
21: ; preds = %19
%22 = load i32, i32* %10
%23 = or i32 %22, 1
store i32 %23, i32* %10
br label %45
24: ; preds = %19
%25 = load i32, i32* %10
%26 = or i32 %25, 2
store i32 %26, i32* %10
br label %45
27: ; preds = %19
%28 = load i32, i32* %10
%29 = or i32 %28, 16
store i32 %29, i32* %10
%30 = load i8*, i8** @optarg
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %11
br label %45
32: ; preds = %19
%33 = load i32, i32* %10
%34 = or i32 %33, 4
store i32 %34, i32* %10
%35 = load i8*, i8** @optarg
store i8* %35, i8** %8
br label %45
36: ; preds = %19
%37 = load i32, i32* %10
%38 = or i32 %37, 8
store i32 %38, i32* %10
%39 = load i8*, i8** @optarg
store i8* %39, i8** %6
br label %45
40: ; preds = %19, %19
%41 = call i32 (...) @usage()
br label %45
42: ; preds = %19
%43 = load i32, i32* %7
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %42, %40, %36, %32, %27, %24, %21
br label %14
46: ; preds = %14
%47 = load i32, i32* %10
%48 = and i32 %47, 1
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %61
50: ; preds = %46
%51 = load i32, i32* %10
%52 = and i32 %51, 4
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i8*, i8** %8
%56 = call i8* @make_md5_salt_para(i8* %55)
store i8* %56, i8** %9
br label %60
57: ; preds = %50
%58 = load i32, i32* %11
%59 = call i8* @make_md5_salt(i32 %58)
store i8* %59, i8** %9
br label %60
60: ; preds = %57, %54
br label %78
61: ; preds = %46
%62 = load i32, i32* %10
%63 = and i32 %62, 4
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %75
65: ; preds = %61
%66 = load i8*, i8** %8
%67 = call i32 @strlen(i8* %66)
%68 = icmp eq i32 %67, 2
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = load i8*, i8** %8
store i8* %70, i8** %9
br label %74
71: ; preds = %65
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %69
br label %77
75: ; preds = %61
%76 = call i8* (...) @make_des_salt()
store i8* %76, i8** %9
br label %77
77: ; preds = %75, %74
br label %78
78: ; preds = %77, %60
%79 = load i32, i32* %10
%80 = and i32 %79, 8
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %88
82: ; preds = %78
%83 = load i8*, i8** %6
%84 = icmp ne i8* %83, null
br i1 %84, label %87, label %85
85: ; preds = %82
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %87
87: ; preds = %85, %82
br label %90
88: ; preds = %78
%89 = call i8* @getpass(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i8* %89, i8** %6
br label %90
90: ; preds = %88, %87
%91 = load i8*, i8** %6
%92 = load i8*, i8** %9
%93 = call i8* @crypt(i8* %91, i8* %92)
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %93)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"mdh?l:s:p:\00"
@optarg = external global i8*
@.str.1 = [21 x i8] c"Invalid Option: -%c\0A\00"
@.str.4 = [12 x i8] c"plaintext: \00"
@str = [30 x i8] c"Please enter a valid password\00"
@str.6 = [53 x i8] c"Invalid salt, please enter 2 alphanumeric characters\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 @time(i32* null)
%4 = tail call i32 @srandom(i32 %3)
br label %5
5: ; preds = %34, %2
%6 = phi i8* [ %36, %34 ], [ null, %2 ]
%7 = phi i8* [ %11, %34 ], [ null, %2 ]
%8 = phi i32 [ %35, %34 ], [ 0, %2 ]
%9 = phi i32 [ %16, %34 ], [ 8, %2 ]
br label %10
10: ; preds = %5, %31
%11 = phi i8* [ %7, %5 ], [ %33, %31 ]
%12 = phi i32 [ %8, %5 ], [ %32, %31 ]
%13 = phi i32 [ %9, %5 ], [ %16, %31 ]
br label %14
14: ; preds = %27, %10
%15 = phi i32 [ %28, %27 ], [ %12, %10 ]
%16 = phi i32 [ %30, %27 ], [ %13, %10 ]
br label %17
17: ; preds = %25, %14
%18 = phi i32 [ %15, %14 ], [ %26, %25 ]
br label %19
19: ; preds = %41, %17
%20 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
switch i32 %20, label %39 [
i32 -1, label %42
i32 109, label %21
i32 100, label %23
i32 108, label %27
i32 115, label %31
i32 112, label %34
i32 104, label %37
i32 63, label %37
]
21: ; preds = %19
%22 = or i32 %18, 1
br label %25
23: ; preds = %19
%24 = or i32 %18, 2
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %22, %21 ], [ %24, %23 ]
br label %17
27: ; preds = %19
%28 = or i32 %18, 16
%29 = load i8*, i8** @optarg
%30 = tail call i32 @atoi(i8* %29)
br label %14
31: ; preds = %19
%32 = or i32 %18, 4
%33 = load i8*, i8** @optarg
br label %10
34: ; preds = %19
%35 = or i32 %18, 8
%36 = load i8*, i8** @optarg
br label %5
37: ; preds = %19, %19
%38 = tail call i32 (...) @usage()
br label %41
39: ; preds = %19
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %41
41: ; preds = %39, %37
br label %19
42: ; preds = %19
%43 = and i32 %18, 1
%44 = icmp eq i32 %43, 0
%45 = and i32 %18, 4
%46 = icmp eq i32 %45, 0
br i1 %44, label %52, label %47
47: ; preds = %42
br i1 %46, label %50, label %48
48: ; preds = %47
%49 = tail call i8* @make_md5_salt_para(i8* %11)
br label %61
50: ; preds = %47
%51 = tail call i8* @make_md5_salt(i32 %16)
br label %61
52: ; preds = %42
br i1 %46, label %59, label %53
53: ; preds = %52
%54 = tail call i32 @strlen(i8* %11)
%55 = icmp eq i32 %54, 2
br i1 %55, label %61, label %56
56: ; preds = %53
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.6, i64 0, i64 0))
%58 = tail call i32 @exit(i32 1)
unreachable
59: ; preds = %52
%60 = tail call i8* (...) @make_des_salt()
br label %61
61: ; preds = %53, %59, %48, %50
%62 = phi i8* [ %49, %48 ], [ %51, %50 ], [ %60, %59 ], [ %11, %53 ]
%63 = and i32 %18, 8
%64 = icmp eq i32 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = icmp eq i8* %6, null
br i1 %66, label %67, label %71
67: ; preds = %65
%68 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %71
69: ; preds = %61
%70 = tail call i8* @getpass(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %71
71: ; preds = %65, %67, %69
%72 = phi i8* [ %6, %65 ], [ null, %67 ], [ %70, %69 ]
%73 = tail call i8* @crypt(i8* %72, i8* %62)
%74 = tail call i32 @puts(i8* dereferenceable(1) %73)
ret i32 0
}