#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*/ _str_is_hex_string(char const *, int);
char *_str_skip_space(char const *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *str_get_hex_string(const char *p, int n) {
  p = _str_skip_space(p);
  if (!_str_is_hex_string(p, n)) {
    return NULL;
  }
  char *s = malloc(n + 1);
  if (s) {
    memcpy(s, p, n);
    s[n] = 0;
  }
  return s;
}


x86 assembly (clang, -O0):
<str_get_hex_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <_str_skip_space@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <_str_is_hex_string@plt>
cmp    $0x0,%eax
jne    117e <str_get_hex_string+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    11bc <str_get_hex_string+0x7c>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b4 <str_get_hex_string+0x74>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_get_hex_string>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r14d
call   1060 <_str_skip_space@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14d,%esi
call   1030 <_str_is_hex_string@plt>
test   %eax,%eax
je     1187 <str_get_hex_string+0x47>
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1187 <str_get_hex_string+0x47>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1189 <str_get_hex_string+0x49>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_get_hex_string(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i8* @_str_skip_space(i8* %7)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i32 @_str_is_hex_string(i8* %9, i32 %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %2
store i8* null, i8** %3
br label %31
14: ; preds = %2
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %29
20: ; preds = %14
%21 = load i8*, i8** %6
%22 = load i8*, i8** %4
%23 = load i32, i32* %5
%24 = call i32 @memcpy(i8* %21, i8* %22, i32 %23)
%25 = load i8*, i8** %6
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %20, %14
%30 = load i8*, i8** %6
store i8* %30, i8** %3
br label %31
31: ; preds = %29, %13
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
define i8* @str_get_hex_string(i8* %0, i32 %1) {
%3 = tail call i8* @_str_skip_space(i8* %0)
%4 = tail call i32 @_str_is_hex_string(i8* %3, i32 %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = add nsw i32 %1, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = tail call i32 @memcpy(i8* %8, i8* %3, i32 %1)
%12 = sext i32 %1 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %6, %10, %2
%15 = phi i8* [ null, %2 ], [ %8, %10 ], [ null, %6 ]
ret i8* %15
}