#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 *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void convert_string_hex(char *s) {
  int len;
  char *p, *buff, *pbuff;
  len = strlen(s);
  buff = (char *)malloc(len * sizeof(char));
  p = s;
  pbuff = buff;
  if (len & 1) {
    sscanf(p, "%1x", (unsigned int *)pbuff++);
    p++;
    len++;
  }
  while (*p) {
    sscanf(p, "%02x", (unsigned int *)pbuff++);
    p += 2;
  }
  strncpy(s, buff, len);
  free(buff);
}


x86 assembly (clang, -O0):
<convert_string_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11ce <convert_string_hex+0x7e>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    %rdx,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
lea    0xe4c(%rip),%rsi
call   1070 <sscanf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11d3 <convert_string_hex+0x83>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1210 <convert_string_hex+0xc0>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    %rdx,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
lea    0xe0a(%rip),%rsi
call   1070 <sscanf@plt>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    11d3 <convert_string_hex+0x83>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_string_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
test   $0x1,%r12b
mov    %rbp,(%rsp)
mov    %rax,%rbx
je     119b <convert_string_hex+0x4b>
add    $0x1,%rbx
lea    0xe78(%rip),%rsi
mov    %rbp,%rdi
mov    %r15,%rdx
call   1070 <sscanf@plt>
add    $0x1,%rbp
add    $0x1,%r12d
cmpb   $0x0,0x0(%rbp)
je     11cf <convert_string_hex+0x7f>
lea    0xe5c(%rip),%r13
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbx),%r14
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1070 <sscanf@plt>
cmpb   $0x0,0x2(%rbp)
lea    0x2(%rbp),%rbp
mov    %r14,%rbx
jne    11b0 <convert_string_hex+0x60>
mov    (%rsp),%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <strncpy@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%1x\00"
@.str.1 = [5 x i8] c"%02x\00"
define void @convert_string_hex(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = mul i64 %10, 1
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
%15 = load i8*, i8** %2
store i8* %15, i8** %4
%16 = load i8*, i8** %5
store i8* %16, i8** %6
%17 = load i32, i32* %3
%18 = and i32 %17, 1
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %1
%21 = load i8*, i8** %4
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %6
%24 = bitcast i8* %22 to i32*
%25 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %24)
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %4
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
store i32 %29, i32* %3
br label %30
30: ; preds = %20, %1
br label %31
31: ; preds = %35, %30
%32 = load i8*, i8** %4
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %31
%36 = load i8*, i8** %4
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %6
%39 = bitcast i8* %37 to i32*
%40 = call i32 @sscanf(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %39)
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i64 2
store i8* %42, i8** %4
br label %31
43: ; preds = %31
%44 = load i8*, i8** %2
%45 = load i8*, i8** %5
%46 = load i32, i32* %3
%47 = call i32 @strncpy(i8* %44, i8* %45, i32 %46)
%48 = load i8*, i8** %5
%49 = call i32 @free(i8* %48)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%1x\00"
@.str.1 = [5 x i8] c"%02x\00"
define void @convert_string_hex(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i64 @malloc(i32 %2)
%4 = inttoptr i64 %3 to i8*
%5 = and i32 %2, 1
%6 = icmp eq i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %4, i64 1
%9 = inttoptr i64 %3 to i32*
%10 = tail call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %9)
%11 = getelementptr inbounds i8, i8* %0, i64 1
%12 = add nsw i32 %2, 1
br label %13
13: ; preds = %7, %1
%14 = phi i8* [ %11, %7 ], [ %0, %1 ]
%15 = phi i32 [ %12, %7 ], [ %2, %1 ]
%16 = phi i8* [ %8, %7 ], [ %4, %1 ]
%17 = load i8, i8* %14
%18 = icmp eq i8 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %13, %19
%20 = phi i8* [ %22, %19 ], [ %16, %13 ]
%21 = phi i8* [ %25, %19 ], [ %14, %13 ]
%22 = getelementptr inbounds i8, i8* %20, i64 1
%23 = bitcast i8* %20 to i32*
%24 = tail call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %23)
%25 = getelementptr inbounds i8, i8* %21, i64 2
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %19
28: ; preds = %19, %13
%29 = tail call i32 @strncpy(i8* %0, i8* %4, i32 %15)
%30 = tail call i32 @free(i8* %4)
ret void
}