#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*/ strncpy(char *, char *, int);
unsigned short strnlen(char *, int);
int encode_domain_name(char *query) {
  char encoded[256];
  short idx;
  unsigned short len;
  unsigned short toklen;
  len = strnlen(query, 256);
  if (len >= 256 + 1)
    return 1;
  strncpy(&encoded[1], query, 256 - 1);
  encoded[0] = '.';
  toklen = 0;
  for (idx = len; idx >= 0; idx--) {
    if (encoded[idx] == '.') {
      encoded[idx] = toklen;
      toklen = 0;
    } else {
      toklen++;
    }
  }
  strncpy(query, encoded, 256);
  return 0;
}


x86 assembly (clang, -O0):
<encode_domain_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x100,%esi
call   1040 <strnlen@plt>
mov    %ax,-0x114(%rbp)
movzwl -0x114(%rbp),%eax
cmp    $0x101,%eax
jl     1162 <encode_domain_name+0x42>
movl   $0x1,-0x4(%rbp)
jmp    1232 <encode_domain_name+0x112>
lea    -0x110(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x2e,-0x110(%rbp)
movw   $0x0,-0x116(%rbp)
mov    -0x114(%rbp),%ax
mov    %ax,-0x112(%rbp)
movswl -0x112(%rbp),%eax
cmp    $0x0,%eax
jl     1216 <encode_domain_name+0xf6>
movswq -0x112(%rbp),%rax
ff
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x2e,%eax
jne    11e8 <encode_domain_name+0xc8>
mov    -0x116(%rbp),%ax
mov    %al,%cl
movswq -0x112(%rbp),%rax
ff
mov    %cl,-0x110(%rbp,%rax,1)
movw   $0x0,-0x116(%rbp)
jmp    11fa <encode_domain_name+0xda>
mov    -0x116(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x116(%rbp)
jmp    11ff <encode_domain_name+0xdf>
mov    -0x112(%rbp),%ax
add    $0xffff,%ax
mov    %ax,-0x112(%rbp)
jmp    1199 <encode_domain_name+0x79>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_domain_name>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r14
mov    $0x100,%esi
call   1040 <strnlen@plt>
movzwl %ax,%ebx
mov    $0x1,%eax
cmp    $0x100,%ebx
ja     119e <encode_domain_name+0x7e>
lea    0x1(%rsp),%rdi
mov    %r14,%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x2e,(%rsp)
xor    %eax,%eax
jmp    117f <encode_domain_name+0x5f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%eax
add    $0xffffffffffffffff,%rbx
lea    0x1(%rbx),%ecx
test   %cx,%cx
jle    118c <encode_domain_name+0x6c>
cmpb   $0x2e,(%rsp,%rbx,1)
jne    1170 <encode_domain_name+0x50>
mov    %al,(%rsp,%rbx,1)
xor    %eax,%eax
jmp    1173 <encode_domain_name+0x53>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x100,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @encode_domain_name(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i16
%6 = alloca i16
%7 = alloca i16
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call zeroext i16 @strnlen(i8* %8, i32 256)
store i16 %9, i16* %6
%10 = load i16, i16* %6
%11 = zext i16 %10 to i32
%12 = icmp sge i32 %11, 257
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 1, i32* %2
br label %48
14: ; preds = %1
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 1
%16 = load i8*, i8** %3
%17 = call i32 @strncpy(i8* %15, i8* %16, i32 255)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
store i8 46, i8* %18
store i16 0, i16* %7
%19 = load i16, i16* %6
store i16 %19, i16* %5
br label %20
20: ; preds = %41, %14
%21 = load i16, i16* %5
%22 = sext i16 %21 to i32
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %44
24: ; preds = %20
%25 = load i16, i16* %5
%26 = sext i16 %25 to i64
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 46
br i1 %30, label %31, label %37
31: ; preds = %24
%32 = load i16, i16* %7
%33 = trunc i16 %32 to i8
%34 = load i16, i16* %5
%35 = sext i16 %34 to i64
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %35
store i8 %33, i8* %36
store i16 0, i16* %7
br label %40
37: ; preds = %24
%38 = load i16, i16* %7
%39 = add i16 %38, 1
store i16 %39, i16* %7
br label %40
40: ; preds = %37, %31
br label %41
41: ; preds = %40
%42 = load i16, i16* %5
%43 = add i16 %42, -1
store i16 %43, i16* %5
br label %20
44: ; preds = %20
%45 = load i8*, i8** %3
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%47 = call i32 @strncpy(i8* %45, i8* %46, i32 256)
store i32 0, i32* %2
br label %48
48: ; preds = %44, %13
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
define i32 @encode_domain_name(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call zeroext i16 @strnlen(i8* %0, i32 256)
%5 = icmp ugt i16 %4, 256
br i1 %5, label %27, label %6
6: ; preds = %1
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 1
%8 = call i32 @strncpy(i8* %7, i8* %0, i32 255)
store i8 46, i8* %3
%9 = zext i16 %4 to i64
br label %10
10: ; preds = %6, %20
%11 = phi i64 [ %9, %6 ], [ %22, %20 ]
%12 = phi i16 [ 0, %6 ], [ %21, %20 ]
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %11
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 46
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = trunc i16 %12 to i8
store i8 %17, i8* %13
br label %20
18: ; preds = %10
%19 = add i16 %12, 1
br label %20
20: ; preds = %16, %18
%21 = phi i16 [ 0, %16 ], [ %19, %18 ]
%22 = add nsw i64 %11, -1
%23 = trunc i64 %11 to i16
%24 = icmp sgt i16 %23, 0
br i1 %24, label %10, label %25
25: ; preds = %20
%26 = call i32 @strncpy(i8* %0, i8* %3, i32 256)
br label %27
27: ; preds = %1, %25
%28 = phi i32 [ 0, %25 ], [ 1, %1 ]
ret i32 %28
}