const char *spinel_next_packed_datatype(const char *pack_format) {
  int depth = 0;
  do {
    switch (*++pack_format) {
    case '(':
      depth++;
      break;
    case ')':
      depth--;
      if (depth == 0) {
        pack_format++;
      }
      break;
    }
  } while ((depth > 0) && *pack_format != 0);
  return pack_format;
}


x86 assembly (clang, -O0):
<spinel_next_packed_datatype>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl 0x1(%rax),%eax
mov    %eax,-0x10(%rbp)
sub    $0x28,%eax
je     1144 <spinel_next_packed_datatype+0x44>
jmp    1133 <spinel_next_packed_datatype+0x33>
mov    -0x10(%rbp),%eax
sub    $0x29,%eax
je     1152 <spinel_next_packed_datatype+0x52>
jmp    1176 <spinel_next_packed_datatype+0x76>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1176 <spinel_next_packed_datatype+0x76>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1171 <spinel_next_packed_datatype+0x71>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1176 <spinel_next_packed_datatype+0x76>
jmp    117b <spinel_next_packed_datatype+0x7b>
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x11(%rbp)
jle    119a <spinel_next_packed_datatype+0x9a>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    110f <spinel_next_packed_datatype+0xf>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<spinel_next_packed_datatype>:
xor    %ecx,%ecx
jmp    111a <spinel_next_packed_datatype+0x1a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ecx
mov    %rax,%rdi
test   %dl,%dl
je     1154 <spinel_next_packed_datatype+0x54>
lea    0x1(%rdi),%rax
movzbl 0x1(%rdi),%edx
movsbl %dl,%esi
cmp    $0x29,%esi
je     1140 <spinel_next_packed_datatype+0x40>
cmp    $0x28,%esi
je     1110 <spinel_next_packed_datatype+0x10>
test   %ecx,%ecx
jg     114f <spinel_next_packed_datatype+0x4f>
jmp    1154 <spinel_next_packed_datatype+0x54>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x2,%rdi
add    $0xffffffff,%ecx
cmove  %rdi,%rax
test   %ecx,%ecx
jle    1154 <spinel_next_packed_datatype+0x54>
movzbl (%rax),%edx
jmp    1113 <spinel_next_packed_datatype+0x13>
ret

LLVM IR (-O0):
define i8* @spinel_next_packed_datatype(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %4
4: ; preds = %30, %1
%5 = load i8*, i8** %2
%6 = getelementptr inbounds i8, i8* %5, i32 1
store i8* %6, i8** %2
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
switch i32 %8, label %21 [
i32 40, label %9
i32 41, label %12
]
9: ; preds = %4
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
store i32 %11, i32* %3
br label %21
12: ; preds = %4
%13 = load i32, i32* %3
%14 = add nsw i32 %13, -1
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %2
br label %20
20: ; preds = %17, %12
br label %21
21: ; preds = %4, %20, %9
br label %22
22: ; preds = %21
%23 = load i32, i32* %3
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i8*, i8** %2
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br label %30
30: ; preds = %25, %22
%31 = phi i1 [ false, %22 ], [ %29, %25 ]
br i1 %31, label %4, label %32
32: ; preds = %30
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
define i8* @spinel_next_packed_datatype(i8* %0) {
br label %2
2: ; preds = %21, %1
%3 = phi i8* [ %0, %1 ], [ %24, %21 ]
%4 = phi i32 [ 0, %1 ], [ %23, %21 ]
%5 = getelementptr inbounds i8, i8* %3, i64 1
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
switch i32 %7, label %15 [
i32 40, label %8
i32 41, label %10
]
8: ; preds = %2
%9 = add nuw nsw i32 %4, 1
br label %21
10: ; preds = %2
%11 = add nsw i32 %4, -1
%12 = icmp eq i32 %11, 0
%13 = getelementptr inbounds i8, i8* %3, i64 2
%14 = select i1 %12, i8* %13, i8* %5
br label %15
15: ; preds = %10, %2
%16 = phi i8* [ %5, %2 ], [ %14, %10 ]
%17 = phi i32 [ %4, %2 ], [ %11, %10 ]
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i8, i8* %16
br label %21
21: ; preds = %19, %8
%22 = phi i8 [ %20, %19 ], [ %6, %8 ]
%23 = phi i32 [ %17, %19 ], [ %9, %8 ]
%24 = phi i8* [ %16, %19 ], [ %5, %8 ]
%25 = icmp eq i8 %22, 0
br i1 %25, label %26, label %2
26: ; preds = %15, %21
%27 = phi i8* [ %16, %15 ], [ %24, %21 ]
ret i8* %27
}