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

/* Variables and functions */
scalar_t__ crypt(char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, char *,
                           char *);
char *getpass(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  char password_first[1024], password_repeat[1024];
  char *input, *passwordfile, *username, *realm, *encrypted;
  FILE *htpasswd_file;
  if (argc < 4) {
    printf("Usage:\n\n");
    printf("htdigest passwordfile realm username\n");
    printf("htdigest passwordfile realm username password\n\n");
    printf("Creates or updates an Apache/nginx compatible htdigest file\n");
    return 1;
  }
  passwordfile = argv[1];
  realm = argv[2];
  username = argv[3];
  if (argc != 5) {
    input = getpass("Password: ");
    strcpy(password_first, input);
    input = getpass("Password (again): ");
    strcpy(password_repeat, input);
    memset(input, 0, strlen(input));
    int password_compare = strcmp(password_first, password_repeat);
    if (password_compare != 0) {
      printf("Passwords did not match\n");
      return 2;
    }
  } else {
    strcpy(password_first, argv[4]);
  }
  encrypted = (char *)crypt(password_first, password_first);
  htpasswd_file = fopen(passwordfile, "a");
  fprintf(htpasswd_file, "%s:%s:%s\n", username, realm, encrypted);
  fclose(htpasswd_file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11ff <main+0x5f>
lea    0xe36(%rip),%rdi
call   1070 <printf@plt>
lea    0xe33(%rip),%rdi
call   1070 <printf@plt>
lea    0xe4d(%rip),%rdi
call   1070 <printf@plt>
lea    0xe71(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1372 <main+0x1d2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x820(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x830(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x828(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     12ef <main+0x14f>
lea    0xe5f(%rip),%rdi
call   10b0 <getpass@plt>
mov    %rax,-0x818(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x818(%rbp),%rsi
call   1030 <strcpy@plt>
lea    0xe44(%rip),%rdi
call   10b0 <getpass@plt>
mov    %rax,-0x818(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x818(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x818(%rbp),%rax
mov    %rax,-0x850(%rbp)
mov    -0x818(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x850(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
call   1080 <memset@plt>
lea    -0x410(%rbp),%rdi
lea    -0x810(%rbp),%rsi
call   1090 <strcmp@plt>
mov    %eax,-0x844(%rbp)
cmpl   $0x0,-0x844(%rbp)
je     12ea <main+0x14a>
lea    0xde1(%rip),%rdi
call   1070 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1372 <main+0x1d2>
jmp    1303 <main+0x163>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1040 <crypt@plt>
mov    %rax,-0x838(%rbp)
mov    -0x820(%rbp),%rdi
lea    0xda8(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x840(%rbp)
mov    -0x840(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    -0x830(%rbp),%rcx
mov    -0x838(%rbp),%r8
lea    0xd7b(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x840(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
cmp    $0x3,%edi
jg     11f0 <main+0x50>
lea    0xe85(%rip),%rdi
call   1040 <puts@plt>
lea    0xe81(%rip),%rdi
call   1040 <puts@plt>
lea    0xe9a(%rip),%rdi
call   1040 <puts@plt>
lea    0xebd(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12c3 <main+0x123>
mov    0x8(%rsi),%r13
mov    0x10(%rsi),%r14
mov    0x18(%rsi),%r15
cmp    $0x5,%edi
jne    120f <main+0x6f>
mov    0x20(%rsi),%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
jmp    1281 <main+0xe1>
lea    0xdea(%rip),%rdi
call   10b0 <getpass@plt>
mov    %rsp,%r12
mov    %r12,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
lea    0xddb(%rip),%rdi
call   10b0 <getpass@plt>
mov    %rax,%rbx
lea    0x400(%rsp),%rbp
mov    %rbp,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1080 <memset@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1281 <main+0xe1>
lea    0xdb5(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%eax
jmp    12c3 <main+0x123>
mov    %rsp,%rdi
mov    %rdi,%rsi
call   1050 <crypt@plt>
mov    %rax,%rbx
lea    0xd88(%rip),%rsi
mov    %r13,%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbp
lea    0xd78(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   10a0 <fprintf@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"Usage:\0A\0A\00"
@.str.1 = [38 x i8] c"htdigest passwordfile realm username\0A\00"
@.str.2 = [48 x i8] c"htdigest passwordfile realm username password\0A\0A\00"
@.str.3 = [61 x i8] c"Creates or updates an Apache/nginx compatible htdigest file\0A\00"
@.str.4 = [11 x i8] c"Password: \00"
@.str.5 = [19 x i8] c"Password (again): \00"
@.str.6 = [25 x i8] c"Passwords did not match\0A\00"
@.str.7 = [2 x i8] c"a\00"
@.str.8 = [10 x i8] c"%s:%s:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 4
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %75
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %9
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
store i8* %28, i8** %11
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 3
%31 = load i8*, i8** %30
store i8* %31, i8** %10
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, 5
br i1 %33, label %34, label %55
34: ; preds = %22
%35 = call i8* @getpass(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i8* %35, i8** %8
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = load i8*, i8** %8
%38 = call i32 @strcpy(i8* %36, i8* %37)
%39 = call i8* @getpass(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
store i8* %39, i8** %8
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = load i8*, i8** %8
%42 = call i32 @strcpy(i8* %40, i8* %41)
%43 = load i8*, i8** %8
%44 = load i8*, i8** %8
%45 = call i32 @strlen(i8* %44)
%46 = call i32 @memset(i8* %43, i32 0, i32 %45)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%49 = call i32 @strcmp(i8* %47, i8* %48)
store i32 %49, i32* %14
%50 = load i32, i32* %14
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %34
%53 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
store i32 2, i32* %3
br label %75
54: ; preds = %34
br label %61
55: ; preds = %22
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 4
%59 = load i8*, i8** %58
%60 = call i32 @strcpy(i8* %56, i8* %59)
br label %61
61: ; preds = %55, %54
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%64 = call i64 @crypt(i8* %62, i8* %63)
%65 = inttoptr i64 %64 to i8*
store i8* %65, i8** %12
%66 = load i8*, i8** %9
%67 = call i32* @fopen(i8* %66, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %67, i32** %13
%68 = load i32*, i32** %13
%69 = load i8*, i8** %10
%70 = load i8*, i8** %11
%71 = load i8*, i8** %12
%72 = call i32 @fprintf(i32* %68, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* %69, i8* %70, i8* %71)
%73 = load i32*, i32** %13
%74 = call i32 @fclose(i32* %73)
store i32 0, i32* %3
br label %75
75: ; preds = %61, %52, %17
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str.4 = [11 x i8] c"Password: \00"
@.str.5 = [19 x i8] c"Password (again): \00"
@.str.7 = [2 x i8] c"a\00"
@.str.8 = [10 x i8] c"%s:%s:%s\0A\00"
@str = [24 x i8] c"Passwords did not match\00"
@str.9 = [8 x i8] c"Usage:\0A\00"
@str.10 = [37 x i8] c"htdigest passwordfile realm username\00"
@str.11 = [47 x i8] c"htdigest passwordfile realm username password\0A\00"
@str.12 = [60 x i8] c"Creates or updates an Apache/nginx compatible htdigest file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 4
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.10, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.11, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.12, i64 0, i64 0))
br label %42
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = icmp eq i32 %0, 5
br i1 %20, label %32, label %21
21: ; preds = %13
%22 = tail call i8* @getpass(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%23 = call i32 @strcpy(i8* %5, i8* %22)
%24 = call i8* @getpass(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 @strcpy(i8* %6, i8* %24)
%26 = call i32 @strlen(i8* %24)
%27 = call i32 @memset(i8* %24, i32 0, i32 %26)
%28 = call i32 @strcmp(i8* %5, i8* %6)
%29 = icmp eq i32 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %21
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %42
32: ; preds = %13
%33 = getelementptr inbounds i8*, i8** %1, i64 4
%34 = load i8*, i8** %33
%35 = call i32 @strcpy(i8* %5, i8* %34)
br label %36
36: ; preds = %21, %32
%37 = call i64 @crypt(i8* %5, i8* %5)
%38 = inttoptr i64 %37 to i8*
%39 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%40 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* %19, i8* %17, i8* %38)
%41 = call i32 @fclose(i32* %39)
br label %42
42: ; preds = %30, %36, %8
%43 = phi i32 [ 1, %8 ], [ 0, %36 ], [ 2, %30 ]
ret i32 %43
}