#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(char *, char const *, size_t);
int strnlen(char const *, int);
int /*<<< orphan*/ tkh_string_is_ticklish(char const *);
int tkh_decode_name_into(const char *s, char *id, int max_length,
                         char *version) {
  if (!tkh_string_is_ticklish(s))
    return -1;
  size_t n = strnlen(s, 64) - 12;
  if (version != NULL) {
    version[0] = s[8];
    version[1] = s[9];
    version[2] = s[10];
    version[3] = 0;
  }
  if (n + 1 > max_length || n < 0)
    return -1;
  memcpy(id, s + 12, n);
  id[n] = 0;
  return n + 1;
}


x86 assembly (clang, -O0):
<tkh_decode_name_into>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <tkh_string_is_ticklish@plt>
cmp    $0x0,%eax
jne    1165 <tkh_decode_name_into+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <tkh_decode_name_into+0xe0>
mov    -0x10(%rbp),%rdi
mov    $0x40,%esi
call   1040 <strnlen@plt>
sub    $0xc,%eax
cltq
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b8 <tkh_decode_name_into+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
mov    0x9(%rax),%cl
mov    -0x28(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x10(%rbp),%rax
mov    0xa(%rax),%cl
mov    -0x28(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x28(%rbp),%rax
movb   $0x0,0x3(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
ja     11d8 <tkh_decode_name_into+0xa8>
cmpq   $0x0,-0x30(%rbp)
jae    11e4 <tkh_decode_name_into+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <tkh_decode_name_into+0xe0>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0xc,%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tkh_decode_name_into>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <tkh_string_is_ticklish@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
je     11af <tkh_decode_name_into+0x7f>
mov    %rbx,%rdi
mov    $0x40,%esi
call   1040 <strnlen@plt>
movslq %eax,%rbp
add    $0xfffffffffffffff4,%rbp
test   %r13,%r13
je     1189 <tkh_decode_name_into+0x59>
mov    0x8(%rbx),%al
mov    %al,0x0(%r13)
mov    0x9(%rbx),%al
mov    %al,0x1(%r13)
mov    0xa(%rbx),%al
mov    %al,0x2(%r13)
movb   $0x0,0x3(%r13)
lea    0x1(%rbp),%r13
movslq %r15d,%rax
cmp    %rax,%r13
ja     11af <tkh_decode_name_into+0x7f>
add    $0xc,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r14,%rbp,1)
mov    %r13d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @tkh_decode_name_into(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%11 = load i8*, i8** %6
%12 = call i32 @tkh_string_is_ticklish(i8* %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %4
store i32 -1, i32* %5
br label %62
15: ; preds = %4
%16 = load i8*, i8** %6
%17 = call i32 @strnlen(i8* %16, i32 64)
%18 = sub nsw i32 %17, 12
%19 = sext i32 %18 to i64
store i64 %19, i64* %10
%20 = load i8*, i8** %9
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %40
22: ; preds = %15
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 8
%25 = load i8, i8* %24
%26 = load i8*, i8** %9
%27 = getelementptr inbounds i8, i8* %26, i64 0
store i8 %25, i8* %27
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i64 9
%30 = load i8, i8* %29
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 %30, i8* %32
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 10
%35 = load i8, i8* %34
%36 = load i8*, i8** %9
%37 = getelementptr inbounds i8, i8* %36, i64 2
store i8 %35, i8* %37
%38 = load i8*, i8** %9
%39 = getelementptr inbounds i8, i8* %38, i64 3
store i8 0, i8* %39
br label %40
40: ; preds = %22, %15
%41 = load i64, i64* %10
%42 = add i64 %41, 1
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = icmp ugt i64 %42, %44
br i1 %45, label %49, label %46
46: ; preds = %40
%47 = load i64, i64* %10
%48 = icmp ult i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %46, %40
store i32 -1, i32* %5
br label %62
50: ; preds = %46
%51 = load i8*, i8** %7
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i64 12
%54 = load i64, i64* %10
%55 = call i32 @memcpy(i8* %51, i8* %53, i64 %54)
%56 = load i8*, i8** %7
%57 = load i64, i64* %10
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8 0, i8* %58
%59 = load i64, i64* %10
%60 = add i64 %59, 1
%61 = trunc i64 %60 to i32
store i32 %61, i32* %5
br label %62
62: ; preds = %50, %49, %14
%63 = load i32, i32* %5
ret i32 %63
}

LLVM IR (-O2):
define i32 @tkh_decode_name_into(i8* %0, i8* %1, i32 %2, i8* writeonly %3) {
%5 = tail call i32 @tkh_string_is_ticklish(i8* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %31, label %7
7: ; preds = %4
%8 = tail call i32 @strnlen(i8* %0, i32 64)
%9 = add nsw i32 %8, -12
%10 = sext i32 %9 to i64
%11 = icmp eq i8* %3, null
br i1 %11, label %22, label %12
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %0, i64 8
%14 = load i8, i8* %13
store i8 %14, i8* %3
%15 = getelementptr inbounds i8, i8* %0, i64 9
%16 = load i8, i8* %15
%17 = getelementptr inbounds i8, i8* %3, i64 1
store i8 %16, i8* %17
%18 = getelementptr inbounds i8, i8* %0, i64 10
%19 = load i8, i8* %18
%20 = getelementptr inbounds i8, i8* %3, i64 2
store i8 %19, i8* %20
%21 = getelementptr inbounds i8, i8* %3, i64 3
store i8 0, i8* %21
br label %22
22: ; preds = %12, %7
%23 = add nsw i64 %10, 1
%24 = sext i32 %2 to i64
%25 = icmp ugt i64 %23, %24
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = getelementptr inbounds i8, i8* %0, i64 12
%28 = tail call i32 @memcpy(i8* %1, i8* %27, i64 %10)
%29 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %29
%30 = trunc i64 %23 to i32
br label %31
31: ; preds = %26, %22, %4
%32 = phi i32 [ -1, %4 ], [ %30, %26 ], [ -1, %22 ]
ret i32 %32
}