#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*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int charhexa_to_char(char *chaine, unsigned char *resultat) {
  int i, j, n, l;
  char ch[5];
  i = 0;
  l = strlen(chaine);
  if (l > 3000)
    l = 3000;
  for (n = 0; n < l; n = n + 2) {
    strcpy(ch, "0x");
    strncat(ch, &chaine[n], 2);
    sscanf(ch, "%x", &j);
    resultat[i] = j;
    i++;
  }
  return i;
}


x86 assembly (clang, -O0):
<charhexa_to_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xbb8,-0x20(%rbp)
jle    1177 <charhexa_to_char+0x37>
movl   $0xbb8,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11ee <charhexa_to_char+0xae>
lea    -0x25(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x25(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x2,%edx
call   1050 <strncat@plt>
lea    -0x25(%rbp),%rdi
lea    0xe45(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x18(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    117e <charhexa_to_char+0x3e>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<charhexa_to_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %eax,%eax
jle    11d9 <charhexa_to_char+0x99>
cmp    $0xbb8,%eax
mov    $0xbb8,%r15d
cmovb  %eax,%r15d
cmp    $0x3,%r15d
mov    $0x2,%eax
cmovl  %eax,%r15d
add    $0xffffffff,%r15d
shr    %r15d
add    $0x1,%r15d
xor    %ebp,%ebp
lea    0x13(%rsp),%r13
lea    0xc(%rsp),%r12
mov    %r13,%rdi
lea    0xe66(%rip),%rsi
call   1030 <strcpy@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1050 <strncat@plt>
mov    %r13,%rdi
lea    0xe4a(%rip),%rsi
mov    %r12,%rdx
call   1060 <sscanf@plt>
movzbl 0xc(%rsp),%eax
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r15
jne    1190 <charhexa_to_char+0x50>
jmp    11dc <charhexa_to_char+0x9c>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"%x\00"
define i32 @charhexa_to_char(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [5 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp sgt i32 %12, 3000
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 3000, i32* %8
br label %15
15: ; preds = %14, %2
store i32 0, i32* %7
br label %16
16: ; preds = %39, %15
%17 = load i32, i32* %7
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %42
20: ; preds = %16
%21 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%23 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%24 = load i8*, i8** %3
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = call i32 @strncat(i8* %23, i8* %27, i32 2)
%29 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%30 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%31 = load i32, i32* %6
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %4
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 %32, i8* %36
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %39
39: ; preds = %20
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 2
store i32 %41, i32* %7
br label %16
42: ; preds = %16
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"%x\00"
define i32 @charhexa_to_char(i8* %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca [5 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %2
%10 = icmp ult i32 %7, 3000
%11 = select i1 %10, i32 %7, i32 3000
%12 = call i32 @llvm.smax.i32(i32 %11, i32 2)
%13 = add nsw i32 %12, -1
%14 = lshr i32 %13, 1
%15 = add nuw nsw i32 %14, 1
%16 = zext i32 %15 to i64
br label %17
17: ; preds = %9, %17
%18 = phi i64 [ 0, %9 ], [ %27, %17 ]
%19 = phi i64 [ 0, %9 ], [ %28, %17 ]
%20 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%21 = getelementptr inbounds i8, i8* %0, i64 %19
%22 = call i32 @strncat(i8* %6, i8* %21, i32 2)
%23 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%24 = load i32, i32* %3
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds i8, i8* %1, i64 %18
store i8 %25, i8* %26
%27 = add nuw nsw i64 %18, 1
%28 = add nuw nsw i64 %19, 2
%29 = icmp eq i64 %27, %16
br i1 %29, label %30, label %17
30: ; preds = %17, %2
%31 = phi i32 [ 0, %2 ], [ %15, %17 ]
ret i32 %31
}