#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 *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, *encrypted;
  FILE *htpasswd_file;
  if (argc < 3) {
    printf("Usage:\n\n");
    printf("htpasswd passwordfile username\n");
    printf("htpasswd passwordfile username password\n\n");
    printf("Creates or updates an Apache/nginx compatible htpasswd file\n");
    return 1;
  }
  passwordfile = argv[1];
  username = argv[2];
  if (argc != 4) {
    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[3]);
  }
  encrypted = (char *)crypt(password_first, password_first);
  htpasswd_file = fopen(passwordfile, "a");
  fprintf(htpasswd_file, "%s:%s\n", username, 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   $0x3,-0x8(%rbp)
jge    11ff <main+0x5f>
lea    0xe36(%rip),%rdi
call   1070 <printf@plt>
lea    0xe33(%rip),%rdi
call   1070 <printf@plt>
lea    0xe47(%rip),%rdi
call   1070 <printf@plt>
lea    0xe65(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135c <main+0x1bc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x820(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x828(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     12e0 <main+0x140>
lea    0xe62(%rip),%rdi
call   10b0 <getpass@plt>
mov    %rax,-0x818(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x818(%rbp),%rsi
call   1030 <strcpy@plt>
lea    0xe47(%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,-0x848(%rbp)
mov    -0x818(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x848(%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,-0x83c(%rbp)
cmpl   $0x0,-0x83c(%rbp)
je     12db <main+0x13b>
lea    0xde4(%rip),%rdi
call   1070 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    135c <main+0x1bc>
jmp    12f4 <main+0x154>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1040 <crypt@plt>
mov    %rax,-0x830(%rbp)
mov    -0x820(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x838(%rbp)
mov    -0x838(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    -0x830(%rbp),%rcx
lea    0xd85(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x838(%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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x800,%rsp
cmp    $0x2,%edi
jg     11ef <main+0x4f>
lea    0xe83(%rip),%rdi
call   1040 <puts@plt>
lea    0xe7f(%rip),%rdi
call   1040 <puts@plt>
lea    0xe92(%rip),%rdi
call   1040 <puts@plt>
lea    0xeaf(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12bb <main+0x11b>
mov    0x8(%rsi),%r12
mov    0x10(%rsi),%r14
cmp    $0x4,%edi
jne    120a <main+0x6a>
mov    0x18(%rsi),%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
jmp    127c <main+0xdc>
lea    0xdef(%rip),%rdi
call   10b0 <getpass@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
lea    0xde0(%rip),%rdi
call   10b0 <getpass@plt>
mov    %rax,%rbx
lea    0x400(%rsp),%r13
mov    %r13,%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    %r15,%rdi
mov    %r13,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     127c <main+0xdc>
lea    0xdb7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%eax
jmp    12bb <main+0x11b>
mov    %rsp,%rdi
mov    %rdi,%rsi
call   1050 <crypt@plt>
mov    %rax,%r15
lea    0xd8d(%rip),%rsi
mov    %r12,%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbx
lea    0xd7d(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %r15,%rcx
call   10a0 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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 = [7 x i8] c"%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 = [31 x i8] c"htpasswd passwordfile username\00"
@str.11 = [41 x i8] c"htpasswd passwordfile username password\0A\00"
@str.12 = [60 x i8] c"Creates or updates an Apache/nginx compatible htpasswd 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, 3
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 ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 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 %40
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 = icmp eq i32 %0, 4
br i1 %18, label %30, label %19
19: ; preds = %13
%20 = tail call i8* @getpass(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 @strcpy(i8* %5, i8* %20)
%22 = call i8* @getpass(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%23 = call i32 @strcpy(i8* %6, i8* %22)
%24 = call i32 @strlen(i8* %22)
%25 = call i32 @memset(i8* %22, i32 0, i32 %24)
%26 = call i32 @strcmp(i8* %5, i8* %6)
%27 = icmp eq i32 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %19
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %40
30: ; preds = %13
%31 = getelementptr inbounds i8*, i8** %1, i64 3
%32 = load i8*, i8** %31
%33 = call i32 @strcpy(i8* %5, i8* %32)
br label %34
34: ; preds = %19, %30
%35 = call i64 @crypt(i8* %5, i8* %5)
%36 = inttoptr i64 %35 to i8*
%37 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%38 = call i32 @fprintf(i32* %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* %17, i8* %36)
%39 = call i32 @fclose(i32* %37)
br label %40
40: ; preds = %28, %34, %8
%41 = phi i32 [ 1, %8 ], [ 0, %34 ], [ 2, %28 ]
ret i32 %41
}