#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 */
scalar_t__ isspace(int);
char strtol(char *, char **, int);
int hex(char *text, char *buffer) {
  char *s;
  char byte[3];
  int nibble = 0;
  int len = 0;
  byte[2] = '\0';
  for (s = text; *s; s++) {
    if (isspace((int)*s))
      continue;
    byte[nibble++] = *s;
    if (nibble == 2) {
      nibble = 0;
      buffer[len++] = strtol(byte, (char **)NULL, 16);
    }
  }
  return len;
}


x86 assembly (clang, -O0):
<hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11d9 <hex+0xb9>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
je     1172 <hex+0x52>
jmp    11c8 <hex+0xa8>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x20(%rbp)
cltq
mov    %cl,-0x1b(%rbp,%rax,1)
cmpl   $0x2,-0x20(%rbp)
jne    11c3 <hex+0xa3>
movl   $0x0,-0x20(%rbp)
lea    -0x1b(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11c8 <hex+0xa8>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    114a <hex+0x2a>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
mov    (%rdi),%al
test   %al,%al
je     119e <hex+0x7e>
mov    %rsi,%r14
mov    %rdi,%rbx
add    $0x1,%rbx
xor    %r12d,%r12d
lea    0xd(%rsp),%r15
xor    %ebp,%ebp
jmp    117b <hex+0x5b>
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
movslq %r12d,%rcx
add    $0x1,%r12d
mov    %al,(%r14,%rcx,1)
nopl   0x0(%rax)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11a1 <hex+0x81>
movsbl %al,%edi
call   1030 <isspace@plt>
test   %rax,%rax
jne    1170 <hex+0x50>
movzbl -0x1(%rbx),%eax
mov    %ebp,%ecx
mov    %al,0xd(%rsp,%rcx,1)
cmp    $0x1,%ebp
je     114d <hex+0x2d>
mov    $0x1,%ebp
jmp    1170 <hex+0x50>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hex(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [3 x i8]
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %7
store i32 0, i32* %8
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 2
store i8 0, i8* %9
%10 = load i8*, i8** %3
store i8* %10, i8** %5
br label %11
11: ; preds = %40, %2
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %43
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = call i64 @isspace(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %40
22: ; preds = %15
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 %27
store i8 %24, i8* %28
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 2
br i1 %30, label %31, label %39
31: ; preds = %22
store i32 0, i32* %7
%32 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%33 = call signext i8 @strtol(i8* %32, i8** null, i32 16)
%34 = load i8*, i8** %4
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 %33, i8* %38
br label %39
39: ; preds = %31, %22
br label %40
40: ; preds = %39, %21
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
br label %11
43: ; preds = %11
%44 = load i32, i32* %8
ret i32 %44
}

LLVM IR (-O2):
define i32 @hex(i8* nocapture %0, i8* nocapture writeonly %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
store i8 0, i8* %5
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %32, label %8
8: ; preds = %2, %26
%9 = phi i8 [ %30, %26 ], [ %6, %2 ]
%10 = phi i32 [ %28, %26 ], [ 0, %2 ]
%11 = phi i32 [ %27, %26 ], [ 0, %2 ]
%12 = phi i8* [ %29, %26 ], [ %0, %2 ]
%13 = sext i8 %9 to i32
%14 = tail call i64 @isspace(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %8
%17 = load i8, i8* %12
%18 = zext i32 %11 to i64
%19 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 %18
store i8 %17, i8* %19
%20 = icmp eq i32 %11, 1
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = call signext i8 @strtol(i8* nocapture %4, i8** null, i32 16)
%23 = add nsw i32 %10, 1
%24 = sext i32 %10 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 %22, i8* %25
br label %26
26: ; preds = %16, %21, %8
%27 = phi i32 [ %11, %8 ], [ 0, %21 ], [ 1, %16 ]
%28 = phi i32 [ %10, %8 ], [ %23, %21 ], [ %10, %16 ]
%29 = getelementptr inbounds i8, i8* %12, i64 1
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %8
32: ; preds = %26, %2
%33 = phi i32 [ 0, %2 ], [ %28, %26 ]
ret i32 %33
}