#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 */
struct packet {
  int dummy;
};

/* Variables and functions */
scalar_t__ memcmp(unsigned char const *, char *, int);
scalar_t__ parse_kv_packet(struct packet const *, unsigned char const **,
                           size_t *, unsigned char const **, size_t *);
int parse_signature_packet(const struct packet *packet,
                           const unsigned char **signature) {
  const unsigned char *key;
  const unsigned char *val;
  size_t key_sz;
  size_t val_sz;
  if (parse_kv_packet(packet, &key, &key_sz, &val, &val_sz) < 0) {
    return -1;
  }
  if (key_sz != (sizeof("signature") - 1) || val_sz != 32U ||
      memcmp(key, "signature", (sizeof("signature") - 1)) != 0) {
    return -1;
  }
  *signature = val;
  return 0;
}


x86 assembly (clang, -O0):
<parse_signature_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1040 <parse_kv_packet@plt>
cmp    $0x0,%rax
jge    115f <parse_signature_packet+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <parse_signature_packet+0x92>
cmpq   $0x9,-0x30(%rbp)
jne    1194 <parse_signature_packet+0x74>
cmpq   $0x20,-0x38(%rbp)
jne    1194 <parse_signature_packet+0x74>
mov    -0x20(%rbp),%rdi
lea    0xe80(%rip),%rsi
mov    $0x9,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
je     11a0 <parse_signature_packet+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <parse_signature_packet+0x92>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_signature_packet>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x8(%rsp),%r8
call   1040 <parse_kv_packet@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     1181 <parse_signature_packet+0x61>
cmpq   $0x9,0x10(%rsp)
jne    1181 <parse_signature_packet+0x61>
cmpq   $0x20,0x8(%rsp)
jne    1181 <parse_signature_packet+0x61>
mov    0x20(%rsp),%rdi
lea    0xe98(%rip),%rsi
mov    $0x9,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
jne    1181 <parse_signature_packet+0x61>
mov    0x18(%rsp),%rax
mov    %rax,(%rbx)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.packet = type { i32 }
@.str = [10 x i8] c"signature\00"
define i32 @parse_signature_packet(%struct.packet* %0, i8** %1) {
%3 = alloca i32
%4 = alloca %struct.packet*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store %struct.packet* %0, %struct.packet** %4
store i8** %1, i8*** %5
%10 = load %struct.packet*, %struct.packet** %4
%11 = call i64 @parse_kv_packet(%struct.packet* %10, i8** %6, i64* %8, i8** %7, i64* %9)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %28
14: ; preds = %2
%15 = load i64, i64* %8
%16 = icmp ne i64 %15, 9
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = load i64, i64* %9
%19 = icmp ne i64 %18, 32
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = load i8*, i8** %6
%22 = call i64 @memcmp(i8* %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20, %17, %14
store i32 -1, i32* %3
br label %28
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = load i8**, i8*** %5
store i8* %26, i8** %27
store i32 0, i32* %3
br label %28
28: ; preds = %25, %24, %13
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.packet = type { i32 }
@.str = [10 x i8] c"signature\00"
define i32 @parse_signature_packet(%struct.packet* %0, i8** nocapture writeonly %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = bitcast i8** %3 to i8*
%8 = bitcast i8** %4 to i8*
%9 = bitcast i64* %5 to i8*
%10 = bitcast i64* %6 to i8*
%11 = call i64 @parse_kv_packet(%struct.packet* %0, i8** %3, i64* %5, i8** %4, i64* %6)
%12 = icmp slt i64 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %2
%14 = load i64, i64* %5
%15 = icmp ne i64 %14, 9
%16 = load i64, i64* %6
%17 = icmp ne i64 %16, 32
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %25, label %19
19: ; preds = %13
%20 = load i8*, i8** %3
%21 = call i64 @memcmp(i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i8*, i8** %4
store i8* %24, i8** %1
br label %25
25: ; preds = %13, %19, %2, %23
%26 = phi i32 [ 0, %23 ], [ -1, %2 ], [ -1, %19 ], [ -1, %13 ]
ret i32 %26
}