char dns_parse_rr_label(char *bgof_msg, char **cursor, char *cursor_max,
                        struct buffer *output) {
  char *label_ptr;
  unsigned short label_offset;
  unsigned char chunk_len;
  int label_len;
  chunk_len = (unsigned char)**cursor;
  if (chunk_len != 0) {
    if (((chunk_len >> 6) & 0x03) == 3) {
      label_offset = read_uint16(*cursor);
      label_offset &= 0x3fff;
      label_ptr = bgof_msg + label_offset;
      (*cursor) += 2;
      chunk_len = *label_ptr;
      while ((label_len = dns_parse_rr_label(bgof_msg, &label_ptr, cursor_max,
                                             output)) > 0) {
      }
      if (label_len == 0) {
        return 0;
      } else {
        log_debug(__func__, "Failed to read label pointer.");
        return -1;
      }
    } else {
      (*cursor)++;
      if (*cursor + chunk_len <= cursor_max) {
        buffer_insert(output, *cursor, chunk_len);
        buffer_insert(output, ".", 1);
        (*cursor) += chunk_len;
        return chunk_len;
      } else {
        log_debug(__func__, "End of label outside of buffer!");
        return -1;
      }
    }
  } else {
    (*cursor)++;
    return 0;
  }
}


x86 assembly (clang, -O0):
<dns_parse_rr_label>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%al
mov    %al,-0x33(%rbp)
movzbl -0x33(%rbp),%eax
cmp    $0x0,%eax
je     12b9 <dns_parse_rr_label+0x179>
movzbl -0x33(%rbp),%eax
sar    $0x6,%eax
and    $0x3,%eax
cmp    $0x3,%eax
jne    1226 <dns_parse_rr_label+0xe6>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    $0x0,%al
call   1030 <read_uint16@plt>
mov    %ax,-0x32(%rbp)
movzwl -0x32(%rbp),%eax
and    $0x3fff,%eax
mov    %ax,-0x32(%rbp)
mov    -0x10(%rbp),%rax
movzwl -0x32(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x2,%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x33(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%rsi
call   1060 <dns_parse_rr_label@plt>
movsbl %al,%eax
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jle    11f5 <dns_parse_rr_label+0xb5>
jmp    11cc <dns_parse_rr_label+0x8c>
cmpl   $0x0,-0x38(%rbp)
jne    1208 <dns_parse_rr_label+0xc8>
movb   $0x0,-0x1(%rbp)
jmp    12cb <dns_parse_rr_label+0x18b>
lea    0xdf1(%rip),%rdi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1040 <log_debug@plt>
movb   $0xff,-0x1(%rbp)
jmp    12cb <dns_parse_rr_label+0x18b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movzbl -0x33(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
cmp    -0x20(%rbp),%rax
ja     129b <dns_parse_rr_label+0x15b>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
movzbl -0x33(%rbp),%edx
mov    $0x0,%al
call   1050 <buffer_insert@plt>
mov    -0x28(%rbp),%rdi
lea    0xdbf(%rip),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1050 <buffer_insert@plt>
movzbl -0x33(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x33(%rbp),%al
mov    %al,-0x1(%rbp)
jmp    12cb <dns_parse_rr_label+0x18b>
lea    0xd5e(%rip),%rdi
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1040 <log_debug@plt>
movb   $0xff,-0x1(%rbp)
jmp    12cb <dns_parse_rr_label+0x18b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
movb   $0x0,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dns_parse_rr_label>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rsi),%rdi
movzbl (%rdi),%r15d
test   %r15d,%r15d
je     11b7 <dns_parse_rr_label+0x77>
mov    %rcx,%r12
mov    %rdx,%r13
mov    %r15d,%eax
not    %eax
test   $0xc0,%al
jne    11c3 <dns_parse_rr_label+0x83>
xor    %eax,%eax
call   1030 <read_uint16@plt>
and    $0x3fff,%eax
add    %rbx,%rax
mov    %rax,0x8(%rsp)
addq   $0x2,(%r14)
lea    0x8(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
mov    %r12,%rcx
call   1060 <dns_parse_rr_label@plt>
test   %al,%al
jg     1190 <dns_parse_rr_label+0x50>
je     11be <dns_parse_rr_label+0x7e>
lea    0xe52(%rip),%rdi
lea    0xe5e(%rip),%rsi
jmp    11e5 <dns_parse_rr_label+0xa5>
add    $0x1,%rdi
mov    %rdi,(%r14)
xor    %r15d,%r15d
jmp    1217 <dns_parse_rr_label+0xd7>
lea    0x1(%rdi),%rsi
mov    %rsi,(%r14)
lea    (%rdi,%r15,1),%rax
add    $0x1,%rax
cmp    %r13,%rax
jbe    11f1 <dns_parse_rr_label+0xb1>
lea    0xe22(%rip),%rdi
lea    0xe4c(%rip),%rsi
xor    %eax,%eax
call   1040 <log_debug@plt>
mov    $0xff,%r15b
jmp    1217 <dns_parse_rr_label+0xd7>
mov    %r12,%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <buffer_insert@plt>
lea    0xe2a(%rip),%rsi
mov    %r12,%rdi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <buffer_insert@plt>
add    %r15,(%r14)
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.buffer = type opaque
@__func__.dns_parse_rr_label = [19 x i8] c"dns_parse_rr_label\00"
@.str = [30 x i8] c"Failed to read label pointer.\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [32 x i8] c"End of label outside of buffer!\00"
define signext i8 @dns_parse_rr_label(i8* %0, i8** %1, i8* %2, %struct.buffer* %3) {
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca %struct.buffer*
%10 = alloca i8*
%11 = alloca i16
%12 = alloca i8
%13 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i8* %2, i8** %8
store %struct.buffer* %3, %struct.buffer** %9
%14 = load i8**, i8*** %7
%15 = load i8*, i8** %14
%16 = load i8, i8* %15
store i8 %16, i8* %12
%17 = load i8, i8* %12
%18 = zext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %89
20: ; preds = %4
%21 = load i8, i8* %12
%22 = zext i8 %21 to i32
%23 = ashr i32 %22, 6
%24 = and i32 %23, 3
%25 = icmp eq i32 %24, 3
br i1 %25, label %26, label %59
26: ; preds = %20
%27 = load i8**, i8*** %7
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) bitcast (i32 (...)* @read_uint16 to i32 (i8*, ...)*)(i8* %28)
%30 = trunc i32 %29 to i16
store i16 %30, i16* %11
%31 = load i16, i16* %11
%32 = zext i16 %31 to i32
%33 = and i32 %32, 16383
%34 = trunc i32 %33 to i16
store i16 %34, i16* %11
%35 = load i8*, i8** %6
%36 = load i16, i16* %11
%37 = zext i16 %36 to i32
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8* %39, i8** %10
%40 = load i8**, i8*** %7
%41 = load i8*, i8** %40
%42 = getelementptr inbounds i8, i8* %41, i64 2
store i8* %42, i8** %40
%43 = load i8*, i8** %10
%44 = load i8, i8* %43
store i8 %44, i8* %12
br label %45
45: ; preds = %52, %26
%46 = load i8*, i8** %6
%47 = load i8*, i8** %8
%48 = load %struct.buffer*, %struct.buffer** %9
%49 = call signext i8 @dns_parse_rr_label(i8* %46, i8** %10, i8* %47, %struct.buffer* %48)
%50 = sext i8 %49 to i32
store i32 %50, i32* %13
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %45
br label %45
53: ; preds = %45
%54 = load i32, i32* %13
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
store i8 0, i8* %5
br label %93
57: ; preds = %53
%58 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @log_debug to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @__func__.dns_parse_rr_label, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i8 -1, i8* %5
br label %93
59: ; preds = %20
%60 = load i8**, i8*** %7
%61 = load i8*, i8** %60
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %60
%63 = load i8**, i8*** %7
%64 = load i8*, i8** %63
%65 = load i8, i8* %12
%66 = zext i8 %65 to i32
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %64, i64 %67
%69 = load i8*, i8** %8
%70 = icmp ule i8* %68, %69
br i1 %70, label %71, label %87
71: ; preds = %59
%72 = load %struct.buffer*, %struct.buffer** %9
%73 = load i8**, i8*** %7
%74 = load i8*, i8** %73
%75 = load i8, i8* %12
%76 = zext i8 %75 to i32
%77 = call i32 (%struct.buffer*, i8*, i32, ...) bitcast (i32 (...)* @buffer_insert to i32 (%struct.buffer*, i8*, i32, ...)*)(%struct.buffer* %72, i8* %74, i32 %76)
%78 = load %struct.buffer*, %struct.buffer** %9
%79 = call i32 (%struct.buffer*, i8*, i32, ...) bitcast (i32 (...)* @buffer_insert to i32 (%struct.buffer*, i8*, i32, ...)*)(%struct.buffer* %78, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%80 = load i8, i8* %12
%81 = zext i8 %80 to i32
%82 = load i8**, i8*** %7
%83 = load i8*, i8** %82
%84 = sext i32 %81 to i64
%85 = getelementptr inbounds i8, i8* %83, i64 %84
store i8* %85, i8** %82
%86 = load i8, i8* %12
store i8 %86, i8* %5
br label %93
87: ; preds = %59
%88 = call i32 (i8*, i8*, ...) bitcast (i32 (...)* @log_debug to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @__func__.dns_parse_rr_label, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i8 -1, i8* %5
br label %93
89: ; preds = %4
%90 = load i8**, i8*** %7
%91 = load i8*, i8** %90
%92 = getelementptr inbounds i8, i8* %91, i32 1
store i8* %92, i8** %90
store i8 0, i8* %5
br label %93
93: ; preds = %89, %87, %71, %57, %56
%94 = load i8, i8* %5
ret i8 %94
}

LLVM IR (-O2):
%struct.buffer = type opaque
@__func__.dns_parse_rr_label = [19 x i8] c"dns_parse_rr_label\00"
@.str = [30 x i8] c"Failed to read label pointer.\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [32 x i8] c"End of label outside of buffer!\00"
define signext i8 @dns_parse_rr_label(i8* %0, i8** nocapture %1, i8* %2, %struct.buffer* %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
%7 = load i8*, i8** %1
%8 = load i8, i8* %7
%9 = zext i8 %8 to i32
%10 = icmp eq i8 %8, 0
br i1 %10, label %40, label %11
11: ; preds = %4
%12 = and i32 %9, 192
%13 = icmp eq i32 %12, 192
br i1 %13, label %14, label %28
14: ; preds = %11
%15 = tail call i32 (i8*, ...) bitcast (i32 (...)* @read_uint16 to i32 (i8*, ...)*)(i8* %7)
%16 = and i32 %15, 16383
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
store i8* %18, i8** %5
%19 = load i8*, i8** %1
%20 = getelementptr inbounds i8, i8* %19, i64 2
store i8* %20, i8** %1
br label %21
21: ; preds = %21, %14
%22 = call signext i8 @dns_parse_rr_label(i8* %0, i8** %5, i8* %2, %struct.buffer* %3)
%23 = icmp sgt i8 %22, 0
br i1 %23, label %21, label %24
24: ; preds = %21
%25 = icmp eq i8 %22, 0
br i1 %25, label %42, label %26
26: ; preds = %24
%27 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @log_debug to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @__func__.dns_parse_rr_label, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %42
28: ; preds = %11
%29 = getelementptr inbounds i8, i8* %7, i64 1
store i8* %29, i8** %1
%30 = zext i8 %8 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = icmp ugt i8* %31, %2
br i1 %32, label %38, label %33
33: ; preds = %28
%34 = tail call i32 (%struct.buffer*, i8*, i32, ...) bitcast (i32 (...)* @buffer_insert to i32 (%struct.buffer*, i8*, i32, ...)*)(%struct.buffer* %3, i8* %29, i32 %9)
%35 = tail call i32 (%struct.buffer*, i8*, i32, ...) bitcast (i32 (...)* @buffer_insert to i32 (%struct.buffer*, i8*, i32, ...)*)(%struct.buffer* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%36 = load i8*, i8** %1
%37 = getelementptr inbounds i8, i8* %36, i64 %30
store i8* %37, i8** %1
br label %42
38: ; preds = %28
%39 = tail call i32 (i8*, i8*, ...) bitcast (i32 (...)* @log_debug to i32 (i8*, i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @__func__.dns_parse_rr_label, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %42
40: ; preds = %4
%41 = getelementptr inbounds i8, i8* %7, i64 1
store i8* %41, i8** %1
br label %42
42: ; preds = %24, %40, %38, %33, %26
%43 = phi i8 [ -1, %26 ], [ %8, %33 ], [ -1, %38 ], [ 0, %40 ], [ 0, %24 ]
ret i8 %43
}