#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*/ snprintf(char *, size_t, char *, char const *);
int strnlen(char const *, int);
int tkh_encode_name_into(const char *s, char *target, int max_length) {
  size_t n = strnlen(s, 53) + 11;
  if (n > max_length)
    return -1;
  snprintf(target, n, "$IDENTITY%s\n", s);
  if (target[n - 2] != '\n')
    target[n - 2] = '\n';
  return n;
}


x86 assembly (clang, -O0):
<tkh_encode_name_into>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x35,%esi
call   1040 <strnlen@plt>
add    $0xb,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jbe    1167 <tkh_encode_name_into+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <tkh_encode_name_into+0x8f>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0xe86(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x2,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11a8 <tkh_encode_name_into+0x88>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x2,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tkh_encode_name_into>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x35,%esi
call   1040 <strnlen@plt>
movslq %eax,%rbx
add    $0xb,%rbx
mov    $0xffffffff,%eax
cmp    %ebp,%ebx
ja     116e <tkh_encode_name_into+0x4e>
lea    0xeb0(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rcx
call   1030 <snprintf@plt>
cmpb   $0xa,-0x2(%rbx,%r14,1)
je     116c <tkh_encode_name_into+0x4c>
movb   $0xa,-0x2(%r14,%rbx,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"$IDENTITY%s\0A\00"
define i32 @tkh_encode_name_into(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = call i32 @strnlen(i8* %9, i32 53)
%11 = add nsw i32 %10, 11
%12 = sext i32 %11 to i64
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = icmp ugt i64 %13, %15
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %38
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = load i64, i64* %8
%21 = load i8*, i8** %5
%22 = call i32 @snprintf(i8* %19, i64 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %6
%24 = load i64, i64* %8
%25 = sub i64 %24, 2
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 10
br i1 %29, label %30, label %35
30: ; preds = %18
%31 = load i8*, i8** %6
%32 = load i64, i64* %8
%33 = sub i64 %32, 2
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 10, i8* %34
br label %35
35: ; preds = %30, %18
%36 = load i64, i64* %8
%37 = trunc i64 %36 to i32
store i32 %37, i32* %4
br label %38
38: ; preds = %35, %17
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@.str = [13 x i8] c"$IDENTITY%s\0A\00"
define i32 @tkh_encode_name_into(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strnlen(i8* %0, i32 53)
%5 = add nsw i32 %4, 11
%6 = sext i32 %5 to i64
%7 = icmp ugt i32 %5, %2
br i1 %7, label %15, label %8
8: ; preds = %3
%9 = tail call i32 @snprintf(i8* %1, i64 %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = add nsw i64 %6, -2
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %15, label %14
14: ; preds = %8
store i8 10, i8* %11
br label %15
15: ; preds = %8, %14, %3
%16 = phi i32 [ -1, %3 ], [ %5, %14 ], [ %5, %8 ]
ret i32 %16
}