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

/* Variables and functions */
int /*<<< orphan*/ hex_encode(char *, char *);
int /*<<< orphan*/ md5_hash(int /*<<< orphan*/ const *, scalar_t__, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int ha1_encode(char *username, char *realm, char *password) {
  char ibuf[1024];
  char obuf[40];
  uint32_t ha1bin[4];
  if (strlen(username) + strlen(realm) + strlen(password) + 3 >= 1024) {
    printf("input values are too big\n");
    return -2;
  }
  strcpy(ibuf, username);
  strcat(ibuf, ":");
  strcat(ibuf, realm);
  strcat(ibuf, ":");
  strcat(ibuf, password);
  printf("computing md5 for: %s\n", ibuf);
  md5_hash((const uint8_t *)ibuf, strlen(ibuf), ha1bin);
  hex_encode((char *)ha1bin, obuf);
  printf("encoded ha1 [%s] bin: %u %u %u %u\n", obuf, ha1bin[0], ha1bin[1],
         ha1bin[2], ha1bin[3]);
  printf("\n%s\n", obuf);
  return 0;
}


x86 assembly (clang, -O0):
<ha1_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x470(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x470(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x468(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x468(%rbp),%rax
add    %rcx,%rax
add    $0x3,%rax
cmp    $0x400,%rax
jl     11e4 <ha1_encode+0x84>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12dd <ha1_encode+0x17d>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x420(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x420(%rbp),%rsi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x478(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x478(%rbp),%rdi
mov    %rax,%rsi
lea    -0x460(%rbp),%rdx
call   1060 <md5_hash@plt>
lea    -0x460(%rbp),%rdi
lea    -0x450(%rbp),%rsi
call   1070 <hex_encode@plt>
lea    -0x450(%rbp),%rsi
mov    -0x460(%rbp),%edx
mov    -0x45c(%rbp),%ecx
mov    -0x458(%rbp),%r8d
mov    -0x454(%rbp),%r9d
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x450(%rbp),%rsi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ha1_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
add    %rbp,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %rbx,%rax
cmp    $0x3fd,%rax
jl     11c9 <ha1_encode+0x59>
lea    0xe87(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfffffffe,%ebp
jmp    127b <ha1_encode+0x10b>
lea    0x40(%rsp),%r15
mov    %r15,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
lea    0xe20(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
lea    0xdef(%rip),%rdi
xor    %ebp,%ebp
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rsp,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1070 <md5_hash@plt>
lea    0x10(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <hex_encode@plt>
mov    (%rsp),%edx
mov    0x4(%rsp),%ecx
mov    0x8(%rsp),%r8d
mov    0xc(%rsp),%r9d
lea    0xdb9(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdcb(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x448,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"input values are too big\0A\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [23 x i8] c"computing md5 for: %s\0A\00"
@.str.3 = [35 x i8] c"encoded ha1 [%s] bin: %u %u %u %u\0A\00"
@.str.4 = [5 x i8] c"\0A%s\0A\00"
define i32 @ha1_encode(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [1024 x i8]
%9 = alloca [40 x i8]
%10 = alloca [4 x i32]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
%13 = load i8*, i8** %6
%14 = call i64 @strlen(i8* %13)
%15 = add nsw i64 %12, %14
%16 = load i8*, i8** %7
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %15, %17
%19 = add nsw i64 %18, 3
%20 = icmp sge i64 %19, 1024
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %4
br label %61
23: ; preds = %3
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%25 = load i8*, i8** %5
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%30 = load i8*, i8** %6
%31 = call i32 @strcat(i8* %29, i8* %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%33 = call i32 @strcat(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%35 = load i8*, i8** %7
%36 = call i32 @strcat(i8* %34, i8* %35)
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%40 = bitcast i8* %39 to i32*
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%42 = call i64 @strlen(i8* %41)
%43 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 0
%44 = call i32 @md5_hash(i32* %40, i64 %42, i32* %43)
%45 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 0
%46 = bitcast i32* %45 to i8*
%47 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%48 = call i32 @hex_encode(i8* %46, i8* %47)
%49 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%50 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 1
%53 = load i32, i32* %52
%54 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 2
%55 = load i32, i32* %54
%56 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 3
%57 = load i32, i32* %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %49, i32 %51, i32 %53, i32 %55, i32 %57)
%59 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %59)
store i32 0, i32* %4
br label %61
61: ; preds = %23, %21
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c":\00"
@.str.2 = [23 x i8] c"computing md5 for: %s\0A\00"
@.str.3 = [35 x i8] c"encoded ha1 [%s] bin: %u %u %u %u\0A\00"
@.str.4 = [5 x i8] c"\0A%s\0A\00"
@str = [25 x i8] c"input values are too big\00"
define i32 @ha1_encode(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca [40 x i8]
%6 = alloca [4 x i32]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%9 = bitcast [4 x i32]* %6 to i8*
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%12 = add nsw i64 %11, %10
%13 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%14 = add nsw i64 %12, %13
%15 = icmp sgt i64 %14, 1020
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %39
18: ; preds = %3
%19 = call i32 @strcpy(i8* %7, i8* %0)
%20 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @strcat(i8* %7, i8* %1)
%22 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @strcat(i8* %7, i8* %2)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%25 = bitcast [1024 x i8]* %4 to i32*
%26 = call i64 @strlen(i8* %7)
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%28 = call i32 @md5_hash(i32* %25, i64 %26, i32* %27)
%29 = call i32 @hex_encode(i8* %9, i8* %8)
%30 = load i32, i32* %27
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 2
%34 = load i32, i32* %33
%35 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 3
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %8, i32 %30, i32 %32, i32 %34, i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %8)
br label %39
39: ; preds = %18, %16
%40 = phi i32 [ -2, %16 ], [ 0, %18 ]
ret i32 %40
}