#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 */
typedef size_t off_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ free(char *);
scalar_t__ rfc1035LabelPack(char *, size_t, char *);
char *strdup(char const *);
char *strtok(char *, char *);
off_t rfc1035NamePack(char *buf, size_t sz, const char *name) {
  off_t off = 0;
  char *copy = strdup(name);
  char *t;
  for (t = strtok(copy, "."); t; t = strtok(NULL, "."))
    off += rfc1035LabelPack(buf + off, sz - off, t);
  free(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)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d2 <rfc1035NamePack+0x82>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <rfc1035LabelPack@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe3c(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    118d <rfc1035NamePack+0x3d>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <rfc1035LabelPack@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rax
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   1060 <strdup@plt>
mov    %rax,%r15
lea    0xe8f(%rip),%rsi
mov    %rax,%rdi
call   1050 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11b4 <rfc1035NamePack+0x64>
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    %rax,%rbx
xor    %edi,%edi
mov    %r12,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
jne    1190 <rfc1035NamePack+0x40>
mov    %r15,%rdi
call   1030 <free@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
xor    %edi,%edi
cmp    %r14,%r15
setbe  %dil
call   1040 <assert@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i64 @rfc1035NamePack(i8* %0, i64 %1, i8* nocapture %2) {
%4 = tail call i8* @strdup(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 %16, label %7
7: ; preds = %3, %7
%8 = phi i8* [ %14, %7 ], [ %5, %3 ]
%9 = phi i64 [ %13, %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 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 = icmp eq i8* %14, null
br i1 %15, label %16, label %7
16: ; preds = %7, %3
%17 = phi i64 [ 0, %3 ], [ %13, %7 ]
%18 = tail call i32 @free(i8* %4)
%19 = getelementptr inbounds i8, i8* %0, i64 %17
%20 = sub i64 %1, %17
%21 = tail call i64 @rfc1035LabelPack(i8* %19, i64 %20, i8* null)
%22 = add i64 %21, %17
%23 = icmp ule i64 %22, %1
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
ret i64 %22
}