#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*/ time_t;
struct tm {
  int dummy;
};

/* Variables and functions */
int EVP_EncodeBlock(unsigned char *, unsigned char *, unsigned int);
int asprintf(char **, char *, char *, char *, unsigned char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ gmtime_r(int /*<<< orphan*/ *, struct tm *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
char *ipapwd_hash_to_history(time_t hash_time, char *hash_type,
                             unsigned char *hash, unsigned int hash_len) {
  struct tm utctime;
  char timestr[15 + 1];
  unsigned char *encoded = NULL;
  char *history = NULL;
  int ret;
  if (!gmtime_r(&hash_time, &utctime))
    goto cleanup;
  strftime(timestr, 15 + 1, "%Y%m%d%H%M%SZ", &utctime);
  encoded = (unsigned char *)malloc(4 * (hash_len * sizeof(*encoded) / 3) + 1);
  if (!encoded)
    goto cleanup;
  ret = EVP_EncodeBlock(encoded, hash, hash_len);
  if (ret < 1)
    goto cleanup;
  ret = asprintf(&history, "%s%s%s", timestr, hash_type, encoded);
  if (ret == -1) {
    history = NULL;
    goto cleanup;
  }
cleanup:
  if (encoded)
    free(encoded);
  return history;
}


x86 assembly (clang, -O0):
<ipapwd_hash_to_history>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x4(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <gmtime_r@plt>
cmp    $0x0,%eax
jne    11a1 <ipapwd_hash_to_history+0x41>
jmp    124b <ipapwd_hash_to_history+0xeb>
lea    -0x30(%rbp),%rdi
mov    $0x10,%esi
lea    0xe4f(%rip),%rdx
lea    -0x20(%rbp),%rcx
call   1070 <strftime@plt>
mov    -0x1c(%rbp),%eax
shl    $0x0,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ee <ipapwd_hash_to_history+0x8e>
jmp    124b <ipapwd_hash_to_history+0xeb>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <EVP_EncodeBlock@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x44(%rbp)
jge    1210 <ipapwd_hash_to_history+0xb0>
jmp    124b <ipapwd_hash_to_history+0xeb>
lea    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%r8
lea    -0x40(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   1060 <asprintf@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1246 <ipapwd_hash_to_history+0xe6>
movq   $0x0,-0x40(%rbp)
jmp    124b <ipapwd_hash_to_history+0xeb>
jmp    124b <ipapwd_hash_to_history+0xeb>
cmpq   $0x0,-0x38(%rbp)
je     125f <ipapwd_hash_to_history+0xff>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipapwd_hash_to_history>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,0xc(%rsp)
movq   $0x0,(%rsp)
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1040 <gmtime_r@plt>
test   %eax,%eax
je     121b <ipapwd_hash_to_history+0xbb>
lea    0xe64(%rip),%rdx
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rcx
mov    $0x10,%esi
call   1070 <strftime@plt>
mov    %ebp,%eax
mov    $0xaaaaaaab,%ecx
imul   %rax,%rcx
shr    $0x20,%rcx
and    $0xfffffffe,%ecx
lea    (%rcx,%rcx,1),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     121b <ipapwd_hash_to_history+0xbb>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1080 <EVP_EncodeBlock@plt>
test   %eax,%eax
jle    120d <ipapwd_hash_to_history+0xad>
lea    0xe21(%rip),%rsi
mov    %rsp,%rdi
lea    0x10(%rsp),%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    120d <ipapwd_hash_to_history+0xad>
movq   $0x0,(%rsp)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
jmp    121d <ipapwd_hash_to_history+0xbd>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [14 x i8] c"%Y%m%d%H%M%SZ\00"
@.str.1 = [7 x i8] c"%s%s%s\00"
define i8* @ipapwd_hash_to_history(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.tm
%10 = alloca [16 x i8]
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i8* null, i8** %11
store i8* null, i8** %12
%14 = call i32 @gmtime_r(i32* %5, %struct.tm* %9)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %4
br label %49
17: ; preds = %4
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%19 = call i32 @strftime(i8* %18, i32 16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), %struct.tm* %9)
%20 = load i32, i32* %8
%21 = zext i32 %20 to i64
%22 = mul i64 %21, 1
%23 = udiv i64 %22, 3
%24 = mul i64 4, %23
%25 = add i64 %24, 1
%26 = trunc i64 %25 to i32
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %11
%29 = load i8*, i8** %11
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %17
br label %49
32: ; preds = %17
%33 = load i8*, i8** %11
%34 = load i8*, i8** %7
%35 = load i32, i32* %8
%36 = call i32 @EVP_EncodeBlock(i8* %33, i8* %34, i32 %35)
store i32 %36, i32* %13
%37 = load i32, i32* %13
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %40
39: ; preds = %32
br label %49
40: ; preds = %32
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%42 = load i8*, i8** %6
%43 = load i8*, i8** %11
%44 = call i32 @asprintf(i8** %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %41, i8* %42, i8* %43)
store i32 %44, i32* %13
%45 = load i32, i32* %13
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %40
store i8* null, i8** %12
br label %49
48: ; preds = %40
br label %49
49: ; preds = %48, %47, %39, %31, %16
%50 = load i8*, i8** %11
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i8*, i8** %11
%54 = call i32 @free(i8* %53)
br label %55
55: ; preds = %52, %49
%56 = load i8*, i8** %12
ret i8* %56
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [14 x i8] c"%Y%m%d%H%M%SZ\00"
@.str.1 = [7 x i8] c"%s%s%s\00"
define i8* @ipapwd_hash_to_history(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.tm
%7 = alloca [16 x i8]
%8 = alloca i8*
store i32 %0, i32* %5
%9 = bitcast %struct.tm* %6 to i8*
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%11 = bitcast i8** %8 to i8*
store i8* null, i8** %8
%12 = call i32 @gmtime_r(i32* %5, %struct.tm* %6)
%13 = icmp eq i32 %12, 0
br i1 %13, label %32, label %14
14: ; preds = %4
%15 = call i32 @strftime(i8* %10, i32 16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), %struct.tm* %6)
%16 = udiv i32 %3, 3
%17 = shl i32 %16, 2
%18 = or i32 %17, 1
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %32, label %22
22: ; preds = %14
%23 = call i32 @EVP_EncodeBlock(i8* %20, i8* %2, i32 %3)
%24 = icmp slt i32 %23, 1
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %1, i8* %20)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %25
store i8* null, i8** %8
br label %29
29: ; preds = %28, %22, %25
%30 = call i32 @free(i8* %20)
%31 = load i8*, i8** %8
br label %32
32: ; preds = %4, %14, %29
%33 = phi i8* [ null, %4 ], [ null, %14 ], [ %31, %29 ]
ret i8* %33
}