#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);
  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    11b1 <str_get_hex_string+0x71>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
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,%r15d
call   1060 <_str_skip_space@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15d,%esi
call   1030 <_str_is_hex_string@plt>
test   %eax,%eax
je     1182 <str_get_hex_string+0x42>
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1184 <str_get_hex_string+0x44>
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 %27
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 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = call i32 @memcpy(i8* %18, i8* %19, i32 %20)
%22 = load i8*, i8** %6
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
%26 = load i8*, i8** %6
store i8* %26, i8** %3
br label %27
27: ; preds = %14, %13
%28 = load i8*, i8** %3
ret i8* %28
}

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 %12, label %6
6: ; preds = %2
%7 = add nsw i32 %1, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i32 @memcpy(i8* %8, i8* %3, i32 %1)
%10 = sext i32 %1 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %2, %6
%13 = phi i8* [ %8, %6 ], [ null, %2 ]
ret i8* %13
}