#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 EINVAL;
int sscanf(char *, char *, unsigned int *);
int convert(unsigned char *dst, const unsigned char *src, size_t len) {
  unsigned int n;
  for (; len != 0; src += 2, len--) {
    if (sscanf((char *)src, "%2x", &n) != 1)
      return EINVAL;
    *dst++ = (unsigned char)n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<convert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1190 <convert+0x80>
mov    -0x18(%rbp),%rdi
lea    0xec6(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
je     115d <convert+0x4d>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1197 <convert+0x87>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1124 <convert+0x14>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1169 <convert+0x59>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %ebp,%ebp
lea    0xece(%rip),%r12
lea    0x4(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    116d <convert+0x5d>
movzbl 0x4(%rsp),%eax
mov    %al,(%r15,%rbp,1)
add    $0x2,%rbx
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1140 <convert+0x30>
xor    %eax,%eax
jmp    1176 <convert+0x66>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%2x\00"
@EINVAL = global i32 0
define i32 @convert(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %23, %3
%10 = load i64, i64* %7
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %9
%13 = load i8*, i8** %6
%14 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %8)
%15 = icmp ne i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* @EINVAL
store i32 %17, i32* %4
br label %29
18: ; preds = %12
%19 = load i32, i32* %8
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %5
store i8 %20, i8* %21
br label %23
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 2
store i8* %25, i8** %6
%26 = load i64, i64* %7
%27 = add i64 %26, -1
store i64 %27, i64* %7
br label %9
28: ; preds = %9
store i32 0, i32* %4
br label %29
29: ; preds = %28, %16
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@.str = [4 x i8] c"%2x\00"
@EINVAL = global i32 0
define i32 @convert(i8* nocapture writeonly %0, i8* nocapture %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i64 %2, 0
br i1 %6, label %22, label %7
7: ; preds = %3, %15
%8 = phi i64 [ %20, %15 ], [ %2, %3 ]
%9 = phi i8* [ %19, %15 ], [ %1, %3 ]
%10 = phi i8* [ %18, %15 ], [ %0, %3 ]
%11 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%12 = icmp eq i32 %11, 1
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = load i32, i32* @EINVAL
br label %22
15: ; preds = %7
%16 = load i32, i32* %4
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %17, i8* %10
%19 = getelementptr inbounds i8, i8* %9, i64 2
%20 = add i64 %8, -1
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %7
22: ; preds = %15, %3, %13
%23 = phi i32 [ %14, %13 ], [ 0, %3 ], [ 0, %15 ]
ret i32 %23
}