#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*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *strdup(char const *);
int strlen(char *);
char *strtok(char *, char *);
int putname(char **buf, size_t buflen, const char *host) {
  char *word;
  int left;
  char *h;
  char *p;
  h = strdup(host);
  left = buflen;
  p = *buf;
  word = strtok(h, ".");
  while (word) {
    if (strlen(word) > 63 || strlen(word) > left) {
      free(h);
      return -1;
    }
    left -= (strlen(word) + 1);
    *p++ = (char)strlen(word);
    memcpy(p, word, strlen(word));
    p += strlen(word);
    word = strtok(NULL, ".");
  }
  *p++ = 0;
  free(h);
  *buf = p;
  return buflen - left;
}


x86 assembly (clang, -O0):
<putname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1267 <putname+0x117>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x3f,%eax
jg     11c6 <putname+0x76>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x2c(%rbp),%eax
jle    11db <putname+0x8b>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <putname+0x14b>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
add    $0x1,%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %al,%cl
mov    -0x40(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
mov    %cl,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xda7(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1197 <putname+0x47>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rdx,%rdi
call   1070 <strdup@plt>
mov    %rbx,0x10(%rsp)
mov    (%rbx),%r14
lea    0xe85(%rip),%rsi
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     1222 <putname+0xd2>
mov    %rax,%rbx
lea    0xe65(%rip),%r13
mov    %r12d,%r15d
xchg   %ax,%ax
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x3f,%eax
jg     1210 <putname+0xc0>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %r15d,%eax
jg     1210 <putname+0xc0>
mov    %rbx,%rdi
call   1040 <strlen@plt>
not    %eax
add    %eax,%r15d
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%r14),%rbp
mov    %al,(%r14)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%r14
add    $0x1,%r14
xor    %edi,%edi
mov    %r13,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a0 <putname+0x50>
jmp    1225 <putname+0xd5>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r12d
jmp    1242 <putname+0xf2>
mov    %r12d,%r15d
movb   $0x0,(%r14)
add    $0x1,%r14
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rax
mov    %r14,(%rax)
sub    %r15d,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i32 @putname(i8** nocapture %0, i64 %1, i8* nocapture %2) {
%4 = tail call i8* @strdup(i8* %2)
%5 = trunc i64 %1 to i32
%6 = load i8*, i8** %0
%7 = tail call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %34, label %9
9: ; preds = %3, %20
%10 = phi i8* [ %31, %20 ], [ %6, %3 ]
%11 = phi i32 [ %23, %20 ], [ %5, %3 ]
%12 = phi i8* [ %32, %20 ], [ %7, %3 ]
%13 = tail call i32 @strlen(i8* %12)
%14 = icmp sgt i32 %13, 63
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = tail call i32 @strlen(i8* %12)
%17 = icmp sgt i32 %16, %11
br i1 %17, label %18, label %20
18: ; preds = %15, %9
%19 = tail call i32 @free(i8* %4)
br label %40
20: ; preds = %15
%21 = tail call i32 @strlen(i8* %12)
%22 = xor i32 %21, -1
%23 = add i32 %11, %22
%24 = tail call i32 @strlen(i8* %12)
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %25, i8* %10
%27 = tail call i32 @strlen(i8* %12)
%28 = tail call i32 @memcpy(i8* %26, i8* %12, i32 %27)
%29 = tail call i32 @strlen(i8* %12)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
%32 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %9
34: ; preds = %20, %3
%35 = phi i32 [ %5, %3 ], [ %23, %20 ]
%36 = phi i8* [ %6, %3 ], [ %31, %20 ]
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 0, i8* %36
%38 = tail call i32 @free(i8* %4)
store i8* %37, i8** %0
%39 = sub i32 %5, %35
br label %40
40: ; preds = %34, %18
%41 = phi i32 [ -1, %18 ], [ %39, %34 ]
ret i32 %41
}