#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 int uint8_t;
typedef int ssize_t;

/* Variables and functions */
ssize_t dns_str2namebuf(const char *name, uint8_t *buffer) {
  static uint8_t *bufname;
  static uint8_t *lenptr;
  static uint8_t total_len;
  static uint8_t label_len;
  lenptr = buffer;
  bufname = buffer + 1;
  total_len = 0;
  label_len = 0;
  while (true) {
    char c = *(name++);
    total_len++;
    if (total_len > 254) {
      return -1;
    }
    if (c == '.') {
      *lenptr = label_len;
      if (total_len == 1) {
        total_len--;
        break;
      }
      if (*name == 0) {
        *(bufname++) = 0;
        break;
      }
      if (*name == '.') {
        return -1;
      }
      lenptr = bufname++;
      label_len = 0;
    } else if (c == 0) {
      *lenptr = label_len;
      *(bufname++) = 0;
      break;
    } else {
      *(bufname++) = (uint8_t)c;
      label_len++;
      if (label_len >= 64) {
        return -1;
      }
    }
  }
  return total_len + 1;
}


x86 assembly (clang, -O0):
<dns_str2namebuf>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,0x2f19(%rip)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,0x2f02(%rip)
movl   $0x0,0x2f08(%rip)
movl   $0x0,0x2f02(%rip)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x19(%rbp)
mov    0x2ee4(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2edb(%rip)
cmpl   $0xfe,0x2ed1(%rip)
jle    1179 <dns_str2namebuf+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <dns_str2namebuf+0x1c7>
movsbl -0x19(%rbp),%eax
cmp    $0x2e,%eax
jne    122d <dns_str2namebuf+0x12d>
mov    0x2eb0(%rip),%ecx
mov    0x2e9d(%rip),%rax
mov    %ecx,(%rax)
cmpl   $0x1,0x2e9c(%rip)
jne    11b6 <dns_str2namebuf+0xb6>
mov    0x2e90(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2e87(%rip)
jmp    12bb <dns_str2namebuf+0x1bb>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11e6 <dns_str2namebuf+0xe6>
mov    0x2e5b(%rip),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,0x2e4d(%rip)
movl   $0x0,(%rax)
jmp    12bb <dns_str2namebuf+0x1bb>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1202 <dns_str2namebuf+0x102>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <dns_str2namebuf+0x1c7>
mov    0x2e1f(%rip),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,0x2e11(%rip)
mov    %rax,0x2e12(%rip)
movl   $0x0,0x2e14(%rip)
jmp    12b6 <dns_str2namebuf+0x1b6>
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jne    1269 <dns_str2namebuf+0x169>
mov    0x2dfc(%rip),%ecx
mov    0x2de9(%rip),%rax
mov    %ecx,(%rax)
mov    0x2dd8(%rip),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,0x2dca(%rip)
movl   $0x0,(%rax)
jmp    12bb <dns_str2namebuf+0x1bb>
movsbl -0x19(%rbp),%ecx
mov    0x2db4(%rip),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,0x2da6(%rip)
mov    %ecx,(%rax)
mov    0x2db2(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2da9(%rip)
cmpl   $0x40,0x2da2(%rip)
jl     12ac <dns_str2namebuf+0x1ac>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <dns_str2namebuf+0x1c7>
jmp    12b1 <dns_str2namebuf+0x1b1>
jmp    12b6 <dns_str2namebuf+0x1b6>
jmp    113a <dns_str2namebuf+0x3a>
mov    0x2d77(%rip),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<dns_str2namebuf>:
lea    0x8(%rsi),%r10
xor    %eax,%eax
mov    $0x1,%cl
lea    0x2f15(%rip),%r8
xor    %r11d,%r11d
mov    %rsi,%r9
jmp    1131 <dns_str2namebuf+0x31>
nopw   0x0(%rax,%rax,1)
add    $0x2,%rax
add    $0x8,%r10
xor    %ecx,%ecx
cmp    $0xfe,%eax
je     11ab <dns_str2namebuf+0xab>
movsbl (%rdi,%rax,1),%edx
cmp    $0x2e,%edx
je     1150 <dns_str2namebuf+0x50>
test   %dl,%dl
je     11b1 <dns_str2namebuf+0xb1>
mov    %edx,0x4(%rsi,%rax,4)
cmp    $0x3e,%r11d
jg     11ab <dns_str2namebuf+0xab>
add    $0x1,%r11d
jmp    116d <dns_str2namebuf+0x6d>
xchg   %ax,%ax
mov    %r11d,(%r9)
test   $0x1,%cl
jne    11bd <dns_str2namebuf+0xbd>
movzbl 0x1(%rdi,%rax,1),%ecx
test   %cl,%cl
je     11c1 <dns_str2namebuf+0xc1>
cmp    $0x2e,%cl
je     11ab <dns_str2namebuf+0xab>
lea    -0x4(%r10),%r9
xor    %r11d,%r11d
movsbl 0x1(%rdi,%rax,1),%ecx
cmp    $0x2e,%ecx
je     1190 <dns_str2namebuf+0x90>
test   %cl,%cl
je     11ca <dns_str2namebuf+0xca>
mov    %ecx,0x8(%rsi,%rax,4)
cmp    $0x3e,%r11d
jg     11ab <dns_str2namebuf+0xab>
add    $0x1,%r11d
jmp    1120 <dns_str2namebuf+0x20>
nopl   0x0(%rax,%rax,1)
mov    %r11d,(%r9)
movzbl 0x2(%rdi,%rax,1),%ecx
test   %cl,%cl
je     11d2 <dns_str2namebuf+0xd2>
xor    %r11d,%r11d
mov    %r10,%r9
cmp    $0x2e,%cl
jne    1120 <dns_str2namebuf+0x20>
mov    $0xffffffff,%eax
ret
add    $0x1,%eax
add    $0xfffffffffffffffc,%r10
mov    %r11d,(%r9)
jmp    11d5 <dns_str2namebuf+0xd5>
xor    %eax,%eax
jmp    11d8 <dns_str2namebuf+0xd8>
add    $0x1,%eax
add    $0xfffffffffffffffc,%r10
jmp    11d5 <dns_str2namebuf+0xd5>
add    $0x2,%eax
mov    %r11d,(%r9)
jmp    11d5 <dns_str2namebuf+0xd5>
add    $0x2,%eax
mov    %r10,%r8
movl   $0x0,(%r8)
add    $0x1,%eax
ret

LLVM IR (-O0):
@dns_str2namebuf.bufname = internal global i32* null
@dns_str2namebuf.lenptr = internal global i32* null
@dns_str2namebuf.total_len = internal global i32 0
@dns_str2namebuf.label_len = internal global i32 0
define i32 @dns_str2namebuf(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8
store i8* %0, i8** %4
store i32* %1, i32** %5
%7 = load i32*, i32** %5
store i32* %7, i32** @dns_str2namebuf.lenptr
%8 = load i32*, i32** %5
%9 = getelementptr inbounds i32, i32* %8, i64 1
store i32* %9, i32** @dns_str2namebuf.bufname
store i32 0, i32* @dns_str2namebuf.total_len
store i32 0, i32* @dns_str2namebuf.label_len
br label %10
10: ; preds = %2, %69
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %4
%13 = load i8, i8* %11
store i8 %13, i8* %6
%14 = load i32, i32* @dns_str2namebuf.total_len
%15 = add nsw i32 %14, 1
store i32 %15, i32* @dns_str2namebuf.total_len
%16 = load i32, i32* @dns_str2namebuf.total_len
%17 = icmp sgt i32 %16, 254
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 -1, i32* %3
br label %73
19: ; preds = %10
%20 = load i8, i8* %6
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 46
br i1 %22, label %23, label %48
23: ; preds = %19
%24 = load i32, i32* @dns_str2namebuf.label_len
%25 = load i32*, i32** @dns_str2namebuf.lenptr
store i32 %24, i32* %25
%26 = load i32, i32* @dns_str2namebuf.total_len
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @dns_str2namebuf.total_len
%30 = add nsw i32 %29, -1
store i32 %30, i32* @dns_str2namebuf.total_len
br label %70
31: ; preds = %23
%32 = load i8*, i8** %4
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32*, i32** @dns_str2namebuf.bufname
%38 = getelementptr inbounds i32, i32* %37, i32 1
store i32* %38, i32** @dns_str2namebuf.bufname
store i32 0, i32* %37
br label %70
39: ; preds = %31
%40 = load i8*, i8** %4
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 46
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 -1, i32* %3
br label %73
45: ; preds = %39
%46 = load i32*, i32** @dns_str2namebuf.bufname
%47 = getelementptr inbounds i32, i32* %46, i32 1
store i32* %47, i32** @dns_str2namebuf.bufname
store i32* %46, i32** @dns_str2namebuf.lenptr
store i32 0, i32* @dns_str2namebuf.label_len
br label %69
48: ; preds = %19
%49 = load i8, i8* %6
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = load i32, i32* @dns_str2namebuf.label_len
%54 = load i32*, i32** @dns_str2namebuf.lenptr
store i32 %53, i32* %54
%55 = load i32*, i32** @dns_str2namebuf.bufname
%56 = getelementptr inbounds i32, i32* %55, i32 1
store i32* %56, i32** @dns_str2namebuf.bufname
store i32 0, i32* %55
br label %70
57: ; preds = %48
%58 = load i8, i8* %6
%59 = sext i8 %58 to i32
%60 = load i32*, i32** @dns_str2namebuf.bufname
%61 = getelementptr inbounds i32, i32* %60, i32 1
store i32* %61, i32** @dns_str2namebuf.bufname
store i32 %59, i32* %60
%62 = load i32, i32* @dns_str2namebuf.label_len
%63 = add nsw i32 %62, 1
store i32 %63, i32* @dns_str2namebuf.label_len
%64 = load i32, i32* @dns_str2namebuf.label_len
%65 = icmp sge i32 %64, 64
br i1 %65, label %66, label %67
66: ; preds = %57
store i32 -1, i32* %3
br label %73
67: ; preds = %57
br label %68
68: ; preds = %67
br label %69
69: ; preds = %68, %45
br label %10
70: ; preds = %52, %36, %28
%71 = load i32, i32* @dns_str2namebuf.total_len
%72 = add nsw i32 %71, 1
store i32 %72, i32* %3
br label %73
73: ; preds = %70, %66, %44, %18
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@dns_str2namebuf.total_len = internal unnamed_addr global i32 0
define i32 @dns_str2namebuf(i8* nocapture %0, i32* %1) {
br label %20
3: ; preds = %31, %40
%4 = phi i32* [ %25, %40 ], [ %27, %31 ]
%5 = phi i32 [ %42, %40 ], [ 0, %31 ]
%6 = getelementptr inbounds i32, i32* %26, i64 2
%7 = getelementptr inbounds i8, i8* %22, i64 2
%8 = load i8, i8* %28
switch i8 %8, label %11 [
i8 46, label %9
i8 0, label %33
]
9: ; preds = %3
store i32 %5, i32* %4
%10 = load i8, i8* %7
switch i8 %10, label %15 [
i8 0, label %44
i8 46, label %50
]
11: ; preds = %3
%12 = sext i8 %8 to i32
store i32 %12, i32* %6
%13 = add nsw i32 %5, 1
%14 = icmp sgt i32 %5, 62
br i1 %14, label %50, label %15
15: ; preds = %11, %9
%16 = phi i32* [ %4, %11 ], [ %6, %9 ]
%17 = phi i32 [ %13, %11 ], [ 0, %9 ]
%18 = add nuw nsw i32 %21, 2
%19 = icmp eq i32 %18, 255
br i1 %19, label %50, label %20
20: ; preds = %15, %2
%21 = phi i32 [ 1, %2 ], [ %18, %15 ]
%22 = phi i8* [ %0, %2 ], [ %7, %15 ]
%23 = phi i1 [ true, %2 ], [ false, %15 ]
%24 = phi i32 [ 0, %2 ], [ %17, %15 ]
%25 = phi i32* [ %1, %2 ], [ %16, %15 ]
%26 = phi i32* [ %1, %2 ], [ %6, %15 ]
%27 = getelementptr inbounds i32, i32* %26, i64 1
%28 = getelementptr inbounds i8, i8* %22, i64 1
%29 = load i8, i8* %22
switch i8 %29, label %40 [
i8 46, label %30
i8 0, label %35
]
30: ; preds = %20
store i32 %24, i32* %25
br i1 %23, label %46, label %31
31: ; preds = %30
%32 = load i8, i8* %28
switch i8 %32, label %3 [
i8 0, label %46
i8 46, label %50
]
33: ; preds = %3
%34 = add nuw nsw i32 %21, 1
br label %35
35: ; preds = %20, %33
%36 = phi i32 [ %34, %33 ], [ %21, %20 ]
%37 = phi i32 [ %5, %33 ], [ %24, %20 ]
%38 = phi i32* [ %4, %33 ], [ %25, %20 ]
%39 = phi i32* [ %6, %33 ], [ %27, %20 ]
store i32 %37, i32* %38
br label %46
40: ; preds = %20
%41 = sext i8 %29 to i32
store i32 %41, i32* %27
%42 = add nsw i32 %24, 1
%43 = icmp sgt i32 %24, 62
br i1 %43, label %50, label %3
44: ; preds = %9
%45 = add nuw nsw i32 %21, 1
br label %46
46: ; preds = %44, %31, %30, %35
%47 = phi i32* [ %39, %35 ], [ %6, %44 ], [ %27, %31 ], [ @dns_str2namebuf.total_len, %30 ]
%48 = phi i32 [ %36, %35 ], [ %45, %44 ], [ %21, %31 ], [ 0, %30 ]
store i32 0, i32* %47
%49 = add nuw nsw i32 %48, 1
br label %50
50: ; preds = %40, %31, %9, %11, %15, %46
%51 = phi i32 [ %49, %46 ], [ -1, %15 ], [ -1, %11 ], [ -1, %9 ], [ -1, %31 ], [ -1, %40 ]
ret i32 %51
}