#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*/ free(char *);
char *get_hex_digit_from_bin(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void base2_to_hex(char *binstr, char *hexstr, size_t hslen) {
  int i = 0;
  int j = 0;
  memset(hexstr, 0, hslen);
  for (i = 0; i < strlen(binstr); i += 4, j++) {
    char *temp = get_hex_digit_from_bin(binstr + i);
    strcat(hexstr, temp);
    free(temp);
  }
  hexstr[j] = '\0';
}


x86 assembly (clang, -O0):
<base2_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jge    11e5 <base2_to_hex+0x95>
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
call   1070 <get_hex_digit_from_bin@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1188 <base2_to_hex+0x38>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base2_to_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %r12d,%r12d
mov    %rsi,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11b3 <base2_to_hex+0x63>
xor    %r12d,%r12d
xor    %ebx,%ebx
nopl   (%rax)
lea    (%r15,%rbx,1),%rdi
call   1070 <get_hex_digit_from_bin@plt>
mov    %rax,%r13
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <strcat@plt>
mov    %r13,%rdi
call   1030 <free@plt>
add    $0x4,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
add    $0x1,%r12
cmp    %ebx,%eax
jg     1180 <base2_to_hex+0x30>
movb   $0x0,(%r14,%r12,1)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @base2_to_hex(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i32 @memset(i8* %10, i32 0, i64 %11)
store i32 0, i32* %7
br label %13
13: ; preds = %29, %3
%14 = load i32, i32* %7
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = icmp slt i32 %14, %16
br i1 %17, label %18, label %34
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = call i8* @get_hex_digit_from_bin(i8* %22)
store i8* %23, i8** %9
%24 = load i8*, i8** %5
%25 = load i8*, i8** %9
%26 = call i32 @strcat(i8* %24, i8* %25)
%27 = load i8*, i8** %9
%28 = call i32 @free(i8* %27)
br label %29
29: ; preds = %18
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 4
store i32 %31, i32* %7
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
br label %13
34: ; preds = %13
%35 = load i8*, i8** %5
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
ret void
}

LLVM IR (-O2):
define void @base2_to_hex(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %3, %7
%8 = phi i64 [ %14, %7 ], [ 0, %3 ]
%9 = phi i32 [ %15, %7 ], [ 0, %3 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %8
%11 = tail call i8* @get_hex_digit_from_bin(i8* %10)
%12 = tail call i32 @strcat(i8* %1, i8* %11)
%13 = tail call i32 @free(i8* %11)
%14 = add nuw i64 %8, 4
%15 = add nuw nsw i32 %9, 1
%16 = tail call i32 @strlen(i8* %0)
%17 = trunc i64 %14 to i32
%18 = icmp sgt i32 %16, %17
br i1 %18, label %7, label %19
19: ; preds = %7
%20 = zext i32 %15 to i64
br label %21
21: ; preds = %19, %3
%22 = phi i64 [ 0, %3 ], [ %20, %19 ]
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 0, i8* %23
ret void
}