#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 */
typedef int /*<<< orphan*/ uint64_t;

/* Variables and functions */
scalar_t__ safe_strtohexa(char const *, unsigned char *, int);
scalar_t__ safe_strtoull(char const *, int /*<<< orphan*/ *);
int strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
int get_bkey_from_str(const char *str, unsigned char *bkey) {
  if (strncmp(str, "0x", 2) == 0) {
    if (safe_strtohexa(str + 2, bkey, 32)) {
      return (strlen(str + 2) / 2);
    }
  } else {
    if (safe_strtoull(str, (uint64_t *)bkey)) {
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_bkey_from_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11b1 <get_bkey_from_str+0x71>
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x20,%edx
call   1040 <safe_strtohexa@plt>
cmp    $0x0,%rax
je     11ac <get_bkey_from_str+0x6c>
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
call   1050 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    11e0 <get_bkey_from_str+0xa0>
jmp    11d9 <get_bkey_from_str+0x99>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <safe_strtoull@plt>
cmp    $0x0,%rax
je     11d4 <get_bkey_from_str+0x94>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <get_bkey_from_str+0xa0>
jmp    11d9 <get_bkey_from_str+0x99>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_bkey_from_str>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1177 <get_bkey_from_str+0x37>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <safe_strtoull@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11a8 <get_bkey_from_str+0x68>
jmp    11a3 <get_bkey_from_str+0x63>
add    $0x2,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1040 <safe_strtohexa@plt>
test   %rax,%rax
je     11a3 <get_bkey_from_str+0x63>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
jmp    11a8 <get_bkey_from_str+0x68>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
define i32 @get_bkey_from_str(i8* %0, i8* %1) {
%3 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 2
%7 = tail call i64 @safe_strtohexa(i8* %6, i8* %1, i32 32)
%8 = icmp eq i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %5
%10 = tail call i32 @strlen(i8* %6)
%11 = sdiv i32 %10, 2
br label %17
12: ; preds = %2
%13 = bitcast i8* %1 to i32*
%14 = tail call i64 @safe_strtoull(i8* %0, i32* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12, %5
br label %17
17: ; preds = %12, %16, %9
%18 = phi i32 [ %11, %9 ], [ -1, %16 ], [ 0, %12 ]
ret i32 %18
}