#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*/ assert(int);
scalar_t__ rfc1035LabelPack(char *, size_t, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ xfree(char *);
char *xstrdup(char const *);
int rfc1035NamePack(char *buf, size_t sz, const char *name) {
  unsigned int off = 0;
  char *copy = xstrdup(name);
  char *t;
  for (t = strtok(copy, "."); t; t = strtok(NULL, "."))
    off += rfc1035LabelPack(buf + off, sz - off, t);
  xfree(copy);
  off += rfc1035LabelPack(buf + off, sz - off, NULL);
  assert(off <= sz);
  return off;
}


x86 assembly (clang, -O0):
<rfc1035NamePack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d9 <rfc1035NamePack+0x89>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
sub    %rax,%rsi
mov    -0x30(%rbp),%rdx
call   1070 <rfc1035LabelPack@plt>
mov    %rax,%rcx
mov    -0x1c(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe35(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    118c <rfc1035NamePack+0x3c>
mov    -0x28(%rbp),%rdi
call   1030 <xfree@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
sub    %rax,%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <rfc1035LabelPack@plt>
mov    %rax,%rcx
mov    -0x1c(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rfc1035NamePack>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r13
mov    %rdx,%rdi
call   1040 <xstrdup@plt>
mov    %rax,%r15
lea    0xe8f(%rip),%rsi
mov    %rax,%rdi
call   1060 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11b3 <rfc1035NamePack+0x63>
lea    0xe79(%rip),%r12
nopw   0x0(%rax,%rax,1)
lea    (%rbx,%r13,1),%rdi
mov    %r14,%rsi
sub    %rbx,%rsi
mov    %rax,%rdx
call   1070 <rfc1035LabelPack@plt>
add    %eax,%ebx
xor    %edi,%edi
mov    %r12,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
jne    1190 <rfc1035NamePack+0x40>
mov    %r15,%rdi
call   1030 <xfree@plt>
add    %rbx,%r13
mov    %r14,%rsi
sub    %rbx,%rsi
mov    %r13,%rdi
xor    %edx,%edx
call   1070 <rfc1035LabelPack@plt>
mov    %rax,%r15
add    %rbx,%r15
mov    %r15d,%eax
xor    %edi,%edi
cmp    %r14,%rax
setbe  %dil
call   1050 <assert@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i32 @rfc1035NamePack(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
store i32 0, i32* %7
%10 = load i8*, i8** %6
%11 = call i8* @xstrdup(i8* %10)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = call i8* @strtok(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %9
br label %14
14: ; preds = %32, %3
%15 = load i8*, i8** %9
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %34
17: ; preds = %14
%18 = load i8*, i8** %4
%19 = load i32, i32* %7
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i64, i64* %5
%23 = load i32, i32* %7
%24 = zext i32 %23 to i64
%25 = sub i64 %22, %24
%26 = load i8*, i8** %9
%27 = call i64 @rfc1035LabelPack(i8* %21, i64 %25, i8* %26)
%28 = load i32, i32* %7
%29 = zext i32 %28 to i64
%30 = add nsw i64 %29, %27
%31 = trunc i64 %30 to i32
store i32 %31, i32* %7
br label %32
32: ; preds = %17
%33 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %33, i8** %9
br label %14
34: ; preds = %14
%35 = load i8*, i8** %8
%36 = call i32 @xfree(i8* %35)
%37 = load i8*, i8** %4
%38 = load i32, i32* %7
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i64, i64* %5
%42 = load i32, i32* %7
%43 = zext i32 %42 to i64
%44 = sub i64 %41, %43
%45 = call i64 @rfc1035LabelPack(i8* %40, i64 %44, i8* null)
%46 = load i32, i32* %7
%47 = zext i32 %46 to i64
%48 = add nsw i64 %47, %45
%49 = trunc i64 %48 to i32
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = zext i32 %50 to i64
%52 = load i64, i64* %5
%53 = icmp ule i64 %51, %52
%54 = zext i1 %53 to i32
%55 = call i32 @assert(i32 %54)
%56 = load i32, i32* %7
ret i32 %56
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i32 @rfc1035NamePack(i8* %0, i64 %1, i8* %2) {
%4 = tail call i8* @xstrdup(i8* %2)
%5 = tail call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %3, %7
%8 = phi i8* [ %14, %7 ], [ %5, %3 ]
%9 = phi i64 [ %15, %7 ], [ 0, %3 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = sub i64 %1, %9
%12 = tail call i64 @rfc1035LabelPack(i8* %10, i64 %11, i8* %8)
%13 = add nsw i64 %12, %9
%14 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = and i64 %13, 4294967295
%16 = icmp eq i8* %14, null
br i1 %16, label %17, label %7
17: ; preds = %7, %3
%18 = phi i64 [ 0, %3 ], [ %15, %7 ]
%19 = tail call i32 @xfree(i8* %4)
%20 = getelementptr inbounds i8, i8* %0, i64 %18
%21 = sub i64 %1, %18
%22 = tail call i64 @rfc1035LabelPack(i8* %20, i64 %21, i8* null)
%23 = add nsw i64 %22, %18
%24 = trunc i64 %23 to i32
%25 = and i64 %23, 4294967295
%26 = icmp ule i64 %25, %1
%27 = zext i1 %26 to i32
%28 = tail call i32 @assert(i32 %27)
ret i32 %24
}