#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 /*<<< orphan*/ b64_decode_string(char *, char *, int);
int b64length(char *);
int /*<<< orphan*/ hexstring_encode_b64(char *, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ separate();
int strlen(char *);
char *xcalloc(int, int);
int /*<<< orphan*/ xfree(char *);
void s1c1() {
  separate();
  char *HEXSTRING = "49276d206b696c6c696e6720796f757220627261696e206c696b652061"
                    "20706f69736f6e6f7573206d757368726f6f6d";
  printf("\nChallenge 1\nHexadecimal to convert to Base64:\n");
  puts(HEXSTRING);
  printf("\nSolution:\n");
  char *b64HS = (char *)xcalloc(b64length(HEXSTRING), 1);
  hexstring_encode_b64(b64HS, HEXSTRING, strlen(HEXSTRING));
  puts(b64HS);
  puts("Convert to ASCII:");
  char *decoded_string = xcalloc(strlen(b64HS) / 4 * 3 + 1, 1);
  b64_decode_string(decoded_string, b64HS, strlen(b64HS));
  puts(decoded_string);
  xfree(decoded_string);
  xfree(b64HS);
}


x86 assembly (clang, -O0):
<s1c1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    $0x0,%al
call   1090 <separate@plt>
lea    0xe5a(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    0xeb0(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <puts@plt>
lea    0xecb(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1080 <b64length@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   10b0 <xcalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <hexstring_encode_b64@plt>
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
imul   $0x3,%eax,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   10b0 <xcalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   10a0 <b64_decode_string@plt>
mov    -0x18(%rbp),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%rdi
call   1070 <xfree@plt>
mov    -0x10(%rbp),%rdi
call   1070 <xfree@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s1c1>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <separate@plt>
lea    0xee0(%rip),%rdi
call   1030 <puts@plt>
lea    0xe61(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
lea    0xef4(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1070 <b64length@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   10a0 <xcalloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <hexstring_encode_b64@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x2,%ecx
lea    (%rcx,%rcx,2),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   10a0 <xcalloc@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1090 <b64_decode_string@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <xfree@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <xfree@plt>

LLVM IR (-O0):
@.str = [97 x i8] c"49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d\00"
@.str.1 = [48 x i8] c"\0AChallenge 1\0AHexadecimal to convert to Base64:\0A\00"
@.str.2 = [12 x i8] c"\0ASolution:\0A\00"
@.str.3 = [18 x i8] c"Convert to ASCII:\00"
define void @s1c1() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = call i32 (...) @separate()
store i8* getelementptr inbounds ([97 x i8], [97 x i8]* @.str, i64 0, i64 0), i8** %1
%5 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%6 = load i8*, i8** %1
%7 = call i32 @puts(i8* %6)
%8 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%9 = load i8*, i8** %1
%10 = call i32 @b64length(i8* %9)
%11 = call i8* @xcalloc(i32 %10, i32 1)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = load i8*, i8** %1
%14 = load i8*, i8** %1
%15 = call i32 @strlen(i8* %14)
%16 = call i32 @hexstring_encode_b64(i8* %12, i8* %13, i32 %15)
%17 = load i8*, i8** %2
%18 = call i32 @puts(i8* %17)
%19 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%20 = load i8*, i8** %2
%21 = call i32 @strlen(i8* %20)
%22 = sdiv i32 %21, 4
%23 = mul nsw i32 %22, 3
%24 = add nsw i32 %23, 1
%25 = call i8* @xcalloc(i32 %24, i32 1)
store i8* %25, i8** %3
%26 = load i8*, i8** %3
%27 = load i8*, i8** %2
%28 = load i8*, i8** %2
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @b64_decode_string(i8* %26, i8* %27, i32 %29)
%31 = load i8*, i8** %3
%32 = call i32 @puts(i8* %31)
%33 = load i8*, i8** %3
%34 = call i32 @xfree(i8* %33)
%35 = load i8*, i8** %2
%36 = call i32 @xfree(i8* %35)
ret void
}

LLVM IR (-O2):
@.str = [97 x i8] c"49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d\00"
@.str.3 = [18 x i8] c"Convert to ASCII:\00"
@str = [47 x i8] c"\0AChallenge 1\0AHexadecimal to convert to Base64:\00"
@str.4 = [11 x i8] c"\0ASolution:\00"
define void @s1c1() {
%1 = tail call i32 (...) @separate()
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([97 x i8], [97 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.4, i64 0, i64 0))
%5 = tail call i32 @b64length(i8* getelementptr inbounds ([97 x i8], [97 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @xcalloc(i32 %5, i32 1)
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([97 x i8], [97 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @hexstring_encode_b64(i8* %6, i8* getelementptr inbounds ([97 x i8], [97 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) %6)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%11 = tail call i32 @strlen(i8* %6)
%12 = sdiv i32 %11, 4
%13 = mul nsw i32 %12, 3
%14 = add nsw i32 %13, 1
%15 = tail call i8* @xcalloc(i32 %14, i32 1)
%16 = tail call i32 @strlen(i8* %6)
%17 = tail call i32 @b64_decode_string(i8* %15, i8* %6, i32 %16)
%18 = tail call i32 @puts(i8* dereferenceable(1) %15)
%19 = tail call i32 @xfree(i8* %15)
%20 = tail call i32 @xfree(i8* %6)
ret void
}