#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*/ memcpy(unsigned char *, unsigned char *, int);
unsigned char strtol(unsigned char *, int /*<<< orphan*/ *, int);
int str_to_bin(unsigned char *str_buffer, int str_buffer_len) {
  if (str_buffer_len <= 0 || str_buffer_len > 512)
    return 0;
  if (str_buffer_len % 2 != 0)
    return 0;
  unsigned char temp_2[2] = {0};
  int i;
  for (i = 0; i < (str_buffer_len / 2); i++) {
    memcpy(temp_2, &str_buffer[i * 2], 2);
    str_buffer[i] = strtol(temp_2, NULL, 16);
  }
  return (i);
}


x86 assembly (clang, -O0):
<str_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1156 <str_to_bin+0x26>
cmpl   $0x200,-0x14(%rbp)
jle    1162 <str_to_bin+0x32>
movl   $0x0,-0x4(%rbp)
jmp    1207 <str_to_bin+0xd7>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1182 <str_to_bin+0x52>
movl   $0x0,-0x4(%rbp)
jmp    1207 <str_to_bin+0xd7>
lea    -0x16(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <memset@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    1201 <str_to_bin+0xd1>
lea    -0x16(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
lea    -0x16(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1199 <str_to_bin+0x69>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    -0x1(%rsi),%eax
xor    %r14d,%r14d
cmp    $0x1ff,%eax
ja     11a0 <str_to_bin+0x80>
mov    %esi,%eax
and    $0x1,%eax
jne    11a0 <str_to_bin+0x80>
movw   $0x0,0x6(%rsp)
xor    %r14d,%r14d
cmp    $0x2,%esi
jb     11a0 <str_to_bin+0x80>
mov    %rdi,%r15
shr    %esi
mov    %esi,%r14d
and    $0x7fff,%r14d
mov    %r14d,%r12d
xor    %ebp,%ebp
lea    0x6(%rsp),%r13
mov    %rdi,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
mov    %al,(%r15,%rbp,1)
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r12
jne    1170 <str_to_bin+0x50>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @str_to_bin(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [2 x i8]
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp sle i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 512
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i32 0, i32* %3
br label %45
14: ; preds = %10
%15 = load i32, i32* %5
%16 = srem i32 %15, 2
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 0, i32* %3
br label %45
19: ; preds = %14
%20 = bitcast [2 x i8]* %6 to i8*
store i32 0, i32* %7
br label %21
21: ; preds = %40, %19
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = sdiv i32 %23, 2
%25 = icmp slt i32 %22, %24
br i1 %25, label %26, label %43
26: ; preds = %21
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%28 = load i8*, i8** %4
%29 = load i32, i32* %7
%30 = mul nsw i32 %29, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = call i32 @memcpy(i8* %27, i8* %32, i32 2)
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%35 = call zeroext i8 @strtol(i8* %34, i32* null, i32 16)
%36 = load i8*, i8** %4
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 %35, i8* %39
br label %40
40: ; preds = %26
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %21
43: ; preds = %21
%44 = load i32, i32* %7
store i32 %44, i32* %3
br label %45
45: ; preds = %43, %18, %13
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
define i32 @str_to_bin(i8* %0, i32 %1) {
%3 = alloca i16
%4 = add i32 %1, -1
%5 = icmp ult i32 %4, 512
%6 = and i32 %1, 1
%7 = icmp eq i32 %6, 0
%8 = and i1 %5, %7
br i1 %8, label %9, label %27
9: ; preds = %2
%10 = bitcast i16* %3 to i8*
store i16 0, i16* %3
%11 = lshr i32 %1, 1
%12 = and i32 %11, 32767
%13 = icmp ugt i32 %1, 1
br i1 %13, label %14, label %25
14: ; preds = %9
%15 = zext i32 %12 to i64
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %23, %16 ]
%18 = shl nuw nsw i64 %17, 1
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = call i32 @memcpy(i8* %10, i8* %19, i32 2)
%21 = call zeroext i8 @strtol(i8* nocapture %10, i32* null, i32 16)
%22 = getelementptr inbounds i8, i8* %0, i64 %17
store i8 %21, i8* %22
%23 = add nuw nsw i64 %17, 1
%24 = icmp eq i64 %23, %15
br i1 %24, label %25, label %16
25: ; preds = %16, %9
%26 = phi i32 [ 0, %9 ], [ %12, %16 ]
br label %27
27: ; preds = %2, %25
%28 = phi i32 [ %26, %25 ], [ 0, %2 ]
ret i32 %28
}