#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 sscanf(char const *, char *, unsigned int *);
int strlen(char const *);
int parse_key(const char *str, unsigned char key[]) {
  if (strlen(str) != 64)
    return 1;
  for (int i = 0; i < 32; i++) {
    int res = sscanf(str + 2 * i, "%02x", (unsigned int *)&(key[i]));
    if (res != 1)
      return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<parse_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x40,%eax
je     114e <parse_key+0x2e>
movl   $0x1,-0x4(%rbp)
jmp    11b8 <parse_key+0x98>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x20,-0x1c(%rbp)
jge    11b1 <parse_key+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
add    %rax,%rdx
lea    0xe80(%rip),%rsi
call   1040 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
je     119e <parse_key+0x7e>
movl   $0x2,-0x4(%rbp)
jmp    11b8 <parse_key+0x98>
jmp    11a3 <parse_key+0x83>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1155 <parse_key+0x35>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_key>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0x40,%ecx
jne    117b <parse_key+0x5b>
xor    %ebx,%ebx
lea    0xeb8(%rip),%r15
nopl   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    1176 <parse_key+0x56>
add    $0x1,%rbx
add    $0x2,%r12
cmp    $0x20,%rbx
jne    1150 <parse_key+0x30>
xor    %eax,%eax
jmp    117b <parse_key+0x5b>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i32 @parse_key(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = icmp ne i32 %9, 64
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %36
12: ; preds = %2
store i32 0, i32* %6
br label %13
13: ; preds = %32, %12
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 32
br i1 %15, label %16, label %35
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = load i32, i32* %6
%19 = mul nsw i32 2, %18
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = bitcast i8* %25 to i32*
%27 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 1
br i1 %29, label %30, label %31
30: ; preds = %16
store i32 2, i32* %3
br label %36
31: ; preds = %16
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %13
35: ; preds = %13
store i32 0, i32* %3
br label %36
36: ; preds = %35, %30, %11
%37 = load i32, i32* %3
ret i32 %37
}

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