#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 */
struct tls_sni {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
unsigned int read_uint(char *, unsigned int, int);
char *sni_type_name(unsigned int);
struct tls_sni *parse_sni(char *data, unsigned offset, unsigned length,
                          int json) {
  unsigned i = offset;
  unsigned j = 0;
  unsigned name_list_length = read_uint(data, offset, 2);
  unsigned name_index = 0;
  unsigned name_type;
  unsigned name_length;
  i += 2;
  if (json)
    printf("\"sni_list\": [");
  for (; i < offset + length;) {
    if (json) {
      if (name_index != 0)
        printf(", ");
      printf("{");
    }
    name_type = read_uint(data, i, 1);
    i += 1;
    name_length = read_uint(data, i, 2);
    i += 2;
    if (json)
      printf("\"sni_type_name\": \"%s\", \"name_length\": %d, \"name\": \"",
             sni_type_name(name_type), name_length);
    else
      printf("\t\t[%d]: (type: %s, length: %u) ", name_index,
             sni_type_name(name_type), name_length);
    for (j = 0; j < name_length; j++, i++)
      printf("%c", data[i]);
    name_index++;
    if (json)
      printf("\"}");
    else
      printf("\n");
  }
  if (json)
    printf("]");
  return 0;
}


x86 assembly (clang, -O0):
<parse_sni>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x2,%edx
call   1040 <read_uint@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     118e <parse_sni+0x5e>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1193 <parse_sni+0x63>
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jae    12da <parse_sni+0x1aa>
cmpl   $0x0,-0x14(%rbp)
je     11d4 <parse_sni+0xa4>
cmpl   $0x0,-0x24(%rbp)
je     11c6 <parse_sni+0x96>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x1,%edx
call   1040 <read_uint@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x2,%edx
call   1040 <read_uint@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1239 <parse_sni+0x109>
mov    -0x28(%rbp),%edi
call   1050 <sni_type_name@plt>
mov    %rax,%rsi
mov    -0x2c(%rbp),%edx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125e <parse_sni+0x12e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <sni_type_name@plt>
mov    -0x30(%rbp),%esi
mov    %rax,%rdx
mov    -0x2c(%rbp),%ecx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jae    12a1 <parse_sni+0x171>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%esi
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1265 <parse_sni+0x135>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12c7 <parse_sni+0x197>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d5 <parse_sni+0x1a5>
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1193 <parse_sni+0x63>
cmpl   $0x0,-0x14(%rbp)
je     12f2 <parse_sni+0x1c2>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_sni>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%rbp
mov    $0x2,%edx
call   1050 <read_uint@plt>
lea    0x2(%r12),%ebx
test   %r15d,%r15d
je     1179 <parse_sni+0x39>
lea    0xe8e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    %r12d,%r14d
mov    %r14d,0x4(%rsp)
cmp    %r14d,%ebx
jae    1271 <parse_sni+0x131>
xor    %r13d,%r13d
jmp    11a8 <parse_sni+0x68>
nop
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r13d
cmp    0x4(%rsp),%ebx
jae    1271 <parse_sni+0x131>
test   %r15d,%r15d
je     11ca <parse_sni+0x8a>
test   %r13d,%r13d
je     11c0 <parse_sni+0x80>
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x7b,%edi
call   1030 <putchar@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
call   1050 <read_uint@plt>
mov    %eax,%r14d
lea    0x1(%rbx),%esi
mov    %rbp,%rdi
mov    $0x2,%edx
call   1050 <read_uint@plt>
mov    %eax,%r12d
mov    %r14d,%edi
call   1060 <sni_type_name@plt>
test   %r15d,%r15d
je     1220 <parse_sni+0xe0>
lea    0xe0e(%rip),%rdi
mov    %rax,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1237 <parse_sni+0xf7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xe1e(%rip),%rdi
mov    %r13d,%esi
mov    %rax,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x3,%ebx
test   %r12d,%r12d
je     1255 <parse_sni+0x115>
nop
mov    %ebx,%eax
movsbl 0x0(%rbp,%rax,1),%edi
call   1030 <putchar@plt>
add    $0x1,%ebx
add    $0xffffffff,%r12d
jne    1240 <parse_sni+0x100>
test   %r15d,%r15d
je     1190 <parse_sni+0x50>
lea    0xe00(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    119a <parse_sni+0x5a>
test   %r15d,%r15d
je     1280 <parse_sni+0x140>
mov    $0x5d,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tls_sni = type { i32 }
@.str = [14 x i8] c"\22sni_list\22: [\00"
@.str.1 = [3 x i8] c", \00"
@.str.2 = [2 x i8] c"{\00"
@.str.3 = [52 x i8] c"\22sni_type_name\22: \22%s\22, \22name_length\22: %d, \22name\22: \22\00"
@.str.4 = [32 x i8] c"\09\09[%d]: (type: %s, length: %u) \00"
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [3 x i8] c"\22}\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [2 x i8] c"]\00"
define %struct.tls_sni* @parse_sni(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%15 = load i32, i32* %6
store i32 %15, i32* %9
store i32 0, i32* %10
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = call i32 @read_uint(i8* %16, i32 %17, i32 2)
store i32 %18, i32* %11
store i32 0, i32* %12
%19 = load i32, i32* %9
%20 = add i32 %19, 2
store i32 %20, i32* %9
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %4
br label %26
26: ; preds = %93, %25
%27 = load i32, i32* %9
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = add i32 %28, %29
%31 = icmp ult i32 %27, %30
br i1 %31, label %32, label %94
32: ; preds = %26
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %32
%36 = load i32, i32* %12
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %32
%43 = load i8*, i8** %5
%44 = load i32, i32* %9
%45 = call i32 @read_uint(i8* %43, i32 %44, i32 1)
store i32 %45, i32* %13
%46 = load i32, i32* %9
%47 = add i32 %46, 1
store i32 %47, i32* %9
%48 = load i8*, i8** %5
%49 = load i32, i32* %9
%50 = call i32 @read_uint(i8* %48, i32 %49, i32 2)
store i32 %50, i32* %14
%51 = load i32, i32* %9
%52 = add i32 %51, 2
store i32 %52, i32* %9
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %42
%56 = load i32, i32* %13
%57 = call i8* @sni_type_name(i32 %56)
%58 = load i32, i32* %14
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %57, i32 %58)
br label %66
60: ; preds = %42
%61 = load i32, i32* %12
%62 = load i32, i32* %13
%63 = call i8* @sni_type_name(i32 %62)
%64 = load i32, i32* %14
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %61, i8* %63, i32 %64)
br label %66
66: ; preds = %60, %55
store i32 0, i32* %10
br label %67
67: ; preds = %79, %66
%68 = load i32, i32* %10
%69 = load i32, i32* %14
%70 = icmp ult i32 %68, %69
br i1 %70, label %71, label %84
71: ; preds = %67
%72 = load i8*, i8** %5
%73 = load i32, i32* %9
%74 = zext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %72, i64 %74
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %71
%80 = load i32, i32* %10
%81 = add i32 %80, 1
store i32 %81, i32* %10
%82 = load i32, i32* %9
%83 = add i32 %82, 1
store i32 %83, i32* %9
br label %67
84: ; preds = %67
%85 = load i32, i32* %12
%86 = add i32 %85, 1
store i32 %86, i32* %12
%87 = load i32, i32* %8
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %84
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %93
91: ; preds = %84
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %93
93: ; preds = %91, %89
br label %26
94: ; preds = %26
%95 = load i32, i32* %8
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %94
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
br label %99
99: ; preds = %97, %94
ret %struct.tls_sni* null
}

LLVM IR (-O2):
%struct.tls_sni = type { i32 }
@.str = [14 x i8] c"\22sni_list\22: [\00"
@.str.1 = [3 x i8] c", \00"
@.str.3 = [52 x i8] c"\22sni_type_name\22: \22%s\22, \22name_length\22: %d, \22name\22: \22\00"
@.str.4 = [32 x i8] c"\09\09[%d]: (type: %s, length: %u) \00"
@.str.6 = [3 x i8] c"\22}\00"
define noalias %struct.tls_sni* @parse_sni(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @read_uint(i8* %0, i32 %1, i32 2)
%6 = add i32 %1, 2
%7 = icmp eq i32 %3, 0
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %4
%11 = add i32 %2, %1
%12 = icmp ult i32 %6, %11
br i1 %12, label %13, label %54
13: ; preds = %10, %52
%14 = phi i32 [ %46, %52 ], [ %6, %10 ]
%15 = phi i32 [ %47, %52 ], [ 0, %10 ]
br i1 %7, label %22, label %16
16: ; preds = %13
%17 = icmp eq i32 %15, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %16
%21 = tail call i32 @putchar(i32 123)
br label %22
22: ; preds = %20, %13
%23 = tail call i32 @read_uint(i8* %0, i32 %14, i32 1)
%24 = add nuw i32 %14, 1
%25 = tail call i32 @read_uint(i8* %0, i32 %24, i32 2)
%26 = add i32 %14, 3
%27 = tail call i8* @sni_type_name(i32 %23)
br i1 %7, label %30, label %28
28: ; preds = %22
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %27, i32 %25)
br label %32
30: ; preds = %22
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %15, i8* %27, i32 %25)
br label %32
32: ; preds = %30, %28
%33 = icmp eq i32 %25, 0
br i1 %33, label %45, label %34
34: ; preds = %32, %34
%35 = phi i32 [ %43, %34 ], [ %26, %32 ]
%36 = phi i32 [ %42, %34 ], [ 0, %32 ]
%37 = zext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %0, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = tail call i32 @putchar(i32 %40)
%42 = add nuw i32 %36, 1
%43 = add i32 %35, 1
%44 = icmp eq i32 %42, %25
br i1 %44, label %45, label %34
45: ; preds = %34, %32
%46 = phi i32 [ %26, %32 ], [ %43, %34 ]
%47 = add i32 %15, 1
br i1 %7, label %50, label %48
48: ; preds = %45
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %52
50: ; preds = %45
%51 = tail call i32 @putchar(i32 10)
br label %52
52: ; preds = %50, %48
%53 = icmp ult i32 %46, %11
br i1 %53, label %13, label %54
54: ; preds = %52, %10
br i1 %7, label %57, label %55
55: ; preds = %54
%56 = tail call i32 @putchar(i32 93)
br label %57
57: ; preds = %55, %54
ret %struct.tls_sni* null
}