#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*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
char *key_share_group_name(unsigned int);
int /*<<< orphan*/ print_hex_blob(char *, unsigned int, unsigned int,
                                  int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
unsigned int read_uint(char *, unsigned int, int);
int /*<<< orphan*/ stderr;
void parse_ch_key_share(char *data, unsigned offset, unsigned length,
                        int json) {
  unsigned pos = offset;
  unsigned key_share_length = read_uint(data, pos, 2);
  unsigned group_code = 0;
  unsigned key_exchange_length = 0;
  const char *name = NULL;
  int i = 0;
  pos += 2;
  if (json)
    printf("\"key_share_list\": [");
  for (; pos < offset + length; pos += key_exchange_length) {
    if (json && i != 0)
      printf(", ");
    group_code = read_uint(data, pos, 2);
    name = key_share_group_name(group_code);
    pos += 2;
    key_exchange_length = read_uint(data, pos, 2);
    pos += 2;
    if (name == NULL) {
      name = "unknown";
      fprintf(stderr, "WARNING! Unknown group code (%#.4x)\n", group_code);
    }
    if (json) {
      printf("{\"name\": \"%s\", \"id\": \"%#.4x\", \"key_exchange_length\": "
             "\"%d\",",
             name, group_code, key_exchange_length);
      printf("\"key_exchange\": \"");
      print_hex_blob(data, pos, key_exchange_length, 0, 0, json);
      printf("\"}");
    } else {
      printf("\t\tgroup: %s (id = %#.4x) key_exchange_length: %d\n", name,
             group_code, key_exchange_length);
      printf("\t\t\tkey_exchange: ");
      print_hex_blob(data, pos, key_exchange_length, 0, 0, 0);
      printf("\n");
    }
    i++;
  }
  if (json)
    printf("]");
}


x86 assembly (clang, -O0):
<parse_ch_key_share>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x2,%edx
call   1060 <read_uint@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11bd <parse_ch_key_share+0x6d>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11c2 <parse_ch_key_share+0x72>
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jae    1328 <parse_ch_key_share+0x1d8>
cmpl   $0x0,-0x14(%rbp)
je     11f5 <parse_ch_key_share+0xa5>
cmpl   $0x0,-0x34(%rbp)
je     11f5 <parse_ch_key_share+0xa5>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x2,%edx
call   1060 <read_uint@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <key_share_group_name@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x2,%edx
call   1060 <read_uint@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1269 <parse_ch_key_share+0x119>
lea    0xdca(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xdbb(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0x0,-0x14(%rbp)
je     12c5 <parse_ch_key_share+0x175>
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x14(%rbp),%r9d
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1040 <print_hex_blob@plt>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1311 <parse_ch_key_share+0x1c1>
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x24(%rbp),%edx
xor    %r9d,%r9d
mov    %r9d,%ecx
mov    %r9d,%r8d
call   1040 <print_hex_blob@plt>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11c2 <parse_ch_key_share+0x72>
cmpl   $0x0,-0x14(%rbp)
je     1340 <parse_ch_key_share+0x1f0>
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ch_key_share>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,0x8(%rsp)
mov    $0x2,%edx
call   1070 <read_uint@plt>
lea    0x2(%r14),%ebx
test   %ebp,%ebp
je     119b <parse_ch_key_share+0x3b>
lea    0xe6c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    %r14d,%r12d
mov    %r12d,0x14(%rsp)
cmp    %r12d,%ebx
jae    12e4 <parse_ch_key_share+0x184>
xor    %eax,%eax
jmp    120e <parse_ch_key_share+0xae>
lea    0xedd(%rip),%rdi
mov    %r14,%rsi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xef8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rdi
mov    %ebx,%esi
mov    %r12d,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <print_hex_blob@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x4(%rsp),%eax
mov    %r15d,%ebp
add    %ebx,%r12d
mov    %r12d,%ebx
cmp    0x14(%rsp),%r12d
jae    12e4 <parse_ch_key_share+0x184>
add    $0xffffffff,%eax
mov    %eax,0x4(%rsp)
setb   %al
mov    %ebp,%r15d
test   %ebp,%ebp
je     1231 <parse_ch_key_share+0xd1>
test   %al,%al
je     1231 <parse_ch_key_share+0xd1>
lea    0xdea(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
mov    $0x2,%edx
call   1070 <read_uint@plt>
mov    %eax,%r13d
mov    %eax,%edi
call   1080 <key_share_group_name@plt>
mov    %rax,%r14
lea    0x2(%rbx),%esi
mov    %rbp,%rdi
mov    $0x2,%edx
call   1070 <read_uint@plt>
mov    %eax,%r12d
test   %r14,%r14
jne    1289 <parse_ch_key_share+0x129>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
mov    %r13d,%edx
call   1060 <fprintf@plt>
lea    0xd8e(%rip),%r14
add    $0x4,%ebx
test   %r15d,%r15d
je     11b0 <parse_ch_key_share+0x50>
lea    0xda8(%rip),%rdi
mov    %r14,%rsi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdcc(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rdi
mov    %ebx,%esi
mov    %r12d,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r15d,%r9d
call   1050 <print_hex_blob@plt>
lea    0xdb9(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f6 <parse_ch_key_share+0x96>
test   %ebp,%ebp
je     1300 <parse_ch_key_share+0x1a0>
mov    $0x5d,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"\22key_share_list\22: [\00"
@.str.1 = [3 x i8] c", \00"
@.str.2 = [8 x i8] c"unknown\00"
@stderr = global i32 0
@.str.3 = [37 x i8] c"WARNING! Unknown group code (%#.4x)\0A\00"
@.str.4 = [59 x i8] c"{\22name\22: \22%s\22, \22id\22: \22%#.4x\22, \22key_exchange_length\22: \22%d\22,\00"
@.str.5 = [18 x i8] c"\22key_exchange\22: \22\00"
@.str.6 = [3 x i8] c"\22}\00"
@.str.7 = [50 x i8] c"\09\09group: %s (id = %#.4x) key_exchange_length: %d\0A\00"
@.str.8 = [18 x i8] c"\09\09\09key_exchange: \00"
@.str.9 = [2 x i8] c"\0A\00"
@.str.10 = [2 x i8] c"]\00"
define void @parse_ch_key_share(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 i8*
%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
%16 = load i8*, i8** %5
%17 = load i32, i32* %9
%18 = call i32 @read_uint(i8* %16, i32 %17, i32 2)
store i32 %18, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i8* null, i8** %13
store i32 0, i32* %14
%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 ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %4
br label %26
26: ; preds = %88, %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 %92
32: ; preds = %26
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* %14
%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, %32
%41 = load i8*, i8** %5
%42 = load i32, i32* %9
%43 = call i32 @read_uint(i8* %41, i32 %42, i32 2)
store i32 %43, i32* %11
%44 = load i32, i32* %11
%45 = call i8* @key_share_group_name(i32 %44)
store i8* %45, i8** %13
%46 = load i32, i32* %9
%47 = add i32 %46, 2
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* %12
%51 = load i32, i32* %9
%52 = add i32 %51, 2
store i32 %52, i32* %9
%53 = load i8*, i8** %13
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %59
55: ; preds = %40
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %13
%56 = load i32, i32* @stderr
%57 = load i32, i32* %11
%58 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %55, %40
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %74
62: ; preds = %59
%63 = load i8*, i8** %13
%64 = load i32, i32* %11
%65 = load i32, i32* %12
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i8* %63, i32 %64, i32 %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%68 = load i8*, i8** %5
%69 = load i32, i32* %9
%70 = load i32, i32* %12
%71 = load i32, i32* %8
%72 = call i32 @print_hex_blob(i8* %68, i32 %69, i32 %70, i32 0, i32 0, i32 %71)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %85
74: ; preds = %59
%75 = load i8*, i8** %13
%76 = load i32, i32* %11
%77 = load i32, i32* %12
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0), i8* %75, i32 %76, i32 %77)
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%80 = load i8*, i8** %5
%81 = load i32, i32* %9
%82 = load i32, i32* %12
%83 = call i32 @print_hex_blob(i8* %80, i32 %81, i32 %82, i32 0, i32 0, i32 0)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
br label %85
85: ; preds = %74, %62
%86 = load i32, i32* %14
%87 = add nsw i32 %86, 1
store i32 %87, i32* %14
br label %88
88: ; preds = %85
%89 = load i32, i32* %12
%90 = load i32, i32* %9
%91 = add i32 %90, %89
store i32 %91, i32* %9
br label %26
92: ; preds = %26
%93 = load i32, i32* %8
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %92
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
br label %97
97: ; preds = %95, %92
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"\22key_share_list\22: [\00"
@.str.1 = [3 x i8] c", \00"
@.str.2 = [8 x i8] c"unknown\00"
@stderr = global i32 0
@.str.3 = [37 x i8] c"WARNING! Unknown group code (%#.4x)\0A\00"
@.str.4 = [59 x i8] c"{\22name\22: \22%s\22, \22id\22: \22%#.4x\22, \22key_exchange_length\22: \22%d\22,\00"
@.str.5 = [18 x i8] c"\22key_exchange\22: \22\00"
@.str.6 = [3 x i8] c"\22}\00"
@.str.7 = [50 x i8] c"\09\09group: %s (id = %#.4x) key_exchange_length: %d\0A\00"
@.str.8 = [18 x i8] c"\09\09\09key_exchange: \00"
define void @parse_ch_key_share(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 ne i32 %3, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 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 %46
13: ; preds = %10, %42
%14 = phi i32 [ %43, %42 ], [ 0, %10 ]
%15 = phi i32 [ %44, %42 ], [ %6, %10 ]
%16 = icmp ne i32 %14, 0
%17 = select i1 %7, i1 %16, i1 false
br i1 %17, label %18, label %20
18: ; preds = %13
%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, %13
%21 = tail call i32 @read_uint(i8* %0, i32 %15, i32 2)
%22 = tail call i8* @key_share_group_name(i32 %21)
%23 = add i32 %15, 2
%24 = tail call i32 @read_uint(i8* %0, i32 %23, i32 2)
%25 = add i32 %15, 4
%26 = icmp eq i8* %22, null
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %30
30: ; preds = %27, %20
%31 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %27 ], [ %22, %20 ]
br i1 %7, label %32, label %37
32: ; preds = %30
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i8* %31, i32 %21, i32 %24)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%35 = tail call i32 @print_hex_blob(i8* %0, i32 %25, i32 %24, i32 0, i32 0, i32 %3)
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %42
37: ; preds = %30
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0), i8* %31, i32 %21, i32 %24)
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%40 = tail call i32 @print_hex_blob(i8* %0, i32 %25, i32 %24, i32 0, i32 0, i32 0)
%41 = tail call i32 @putchar(i32 10)
br label %42
42: ; preds = %37, %32
%43 = add nuw nsw i32 %14, 1
%44 = add i32 %24, %25
%45 = icmp ult i32 %44, %11
br i1 %45, label %13, label %46
46: ; preds = %42, %10
br i1 %7, label %47, label %49
47: ; preds = %46
%48 = tail call i32 @putchar(i32 93)
br label %49
49: ; preds = %47, %46
ret void
}