#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 */
unsigned char *calloc(int, int);
int hex2dec(unsigned char);
int strlen(unsigned char *);
unsigned char *packdigit(unsigned char *number) {
  unsigned char *area;
  unsigned char *scan;
  area = calloc(strlen(number) / 2 + 2, sizeof area[0]);
  if (area != NULL) {
    for (scan = area; *number; number += 2, scan++)
      *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
  }
  return area;
}


x86 assembly (clang, -O0):
<packdigit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
add    $0x2,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d0 <packdigit+0xa0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11cb <packdigit+0x9b>
mov    -0x8(%rbp),%rax
movzbl (%rax),%edi
call   1030 <hex2dec@plt>
shl    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movzbl 0x1(%rax),%edi
call   1030 <hex2dec@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
or     %ecx,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1173 <packdigit+0x43>
jmp    11d0 <packdigit+0xa0>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<packdigit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%edi
shr    $0x1f,%edi
add    %eax,%edi
sar    %edi
add    $0x2,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     119c <packdigit+0x6c>
mov    (%r15),%al
test   %al,%al
je     119c <packdigit+0x6c>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movzbl %al,%edi
call   1030 <hex2dec@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movzbl 0x1(%r15,%rbx,2),%edi
call   1030 <hex2dec@plt>
or     %ebp,%eax
mov    %al,(%r14,%rbx,1)
movzbl 0x2(%r15,%rbx,2),%eax
add    $0x1,%rbx
test   %al,%al
jne    1170 <packdigit+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define noalias i8* @packdigit(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = sdiv i32 %2, 2
%4 = add nsw i32 %3, 2
%5 = tail call i8* @calloc(i32 %4, i32 1)
%6 = icmp eq i8* %5, null
br i1 %6, label %25, label %7
7: ; preds = %1
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %7, %10
%11 = phi i8 [ %23, %10 ], [ %8, %7 ]
%12 = phi i8* [ %22, %10 ], [ %5, %7 ]
%13 = phi i8* [ %21, %10 ], [ %0, %7 ]
%14 = tail call i32 @hex2dec(i8 zeroext %11)
%15 = shl i32 %14, 4
%16 = getelementptr inbounds i8, i8* %13, i64 1
%17 = load i8, i8* %16
%18 = tail call i32 @hex2dec(i8 zeroext %17)
%19 = or i32 %18, %15
%20 = trunc i32 %19 to i8
store i8 %20, i8* %12
%21 = getelementptr inbounds i8, i8* %13, i64 2
%22 = getelementptr inbounds i8, i8* %12, i64 1
%23 = load i8, i8* %21
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %10
25: ; preds = %10, %7, %1
ret i8* %5
}