#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 /*<<< orphan*/ uint8_t;
typedef int uint16_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stderr;
int parse_bgp_path_attr_community(char *buffer, int buffer_len, uint8_t *input,
                                  int len) {
  if (len % 4 != 0) {
    fprintf(stderr, "Malformed community of length %u\n", len);
  }
  int idx = 0;
  char *buffer_idx = buffer;
  int remaining = buffer_len;
  while (idx < len) {
    if (idx != 0) {
      snprintf(buffer_idx, remaining, " ");
      buffer_idx++;
      remaining--;
    }
    uint16_t *a, *b;
    a = (uint16_t *)(input + idx);
    idx += 2;
    b = (uint16_t *)(input + idx);
    idx += 2;
    snprintf(buffer_idx, remaining, "%04x:%04x", *a, *b);
    buffer_idx += 9;
    remaining -= 9;
  }
  return len;
}


x86 assembly (clang, -O0):
<parse_bgp_path_attr_community>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1162 <parse_bgp_path_attr_community+0x42>
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xea3(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    122b <parse_bgp_path_attr_community+0x10b>
cmpl   $0x0,-0x20(%rbp)
je     11b7 <parse_bgp_path_attr_community+0x97>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
lea    0xe87(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    (%rax),%r8d
lea    0xe1a(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x9,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
sub    $0x9,%eax
mov    %eax,-0x2c(%rbp)
jmp    1177 <parse_bgp_path_attr_community+0x57>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_bgp_path_attr_community>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
test   $0x3,%r14b
je     1154 <parse_bgp_path_attr_community+0x34>
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
lea    0xeb4(%rip),%rsi
mov    %r14d,%edx
call   1040 <fprintf@plt>
test   %r14d,%r14d
jle    11d5 <parse_bgp_path_attr_community+0xb5>
mov    (%r15),%ecx
mov    0x8(%r15),%r8d
lea    0xebd(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    $0x5,%r14d
jb     11d5 <parse_bgp_path_attr_community+0xb5>
add    $0xa,%rbx
add    $0xfffffff6,%ebp
mov    $0x4,%r12d
cs nopw 0x0(%rax,%rax,1)
lea    0x1(%rbp),%esi
lea    -0x1(%rbx),%rdi
lea    0xe84(%rip),%rdx
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    0x4(%r12),%r13
mov    (%r15,%r12,4),%ecx
mov    0x8(%r15,%r12,4),%r8d
mov    %rbx,%rdi
mov    %ebp,%esi
lea    0xe65(%rip),%rdx
xor    %eax,%eax
call   1030 <snprintf@plt>
add    $0xa,%rbx
add    $0xfffffff6,%ebp
mov    %r13,%r12
cmp    %r14d,%r13d
jl     1190 <parse_bgp_path_attr_community+0x70>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"Malformed community of length %u\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [10 x i8] c"%04x:%04x\00"
define i32 @parse_bgp_path_attr_community(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 i8*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%14 = load i32, i32* %8
%15 = srem i32 %14, 4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = load i32, i32* %8
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %4
store i32 0, i32* %9
%22 = load i8*, i8** %5
store i8* %22, i8** %10
%23 = load i32, i32* %6
store i32 %23, i32* %11
br label %24
24: ; preds = %39, %21
%25 = load i32, i32* %9
%26 = load i32, i32* %8
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %63
28: ; preds = %24
%29 = load i32, i32* %9
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = load i8*, i8** %10
%33 = load i32, i32* %11
%34 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %32, i32 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%35 = load i8*, i8** %10
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %10
%37 = load i32, i32* %11
%38 = add nsw i32 %37, -1
store i32 %38, i32* %11
br label %39
39: ; preds = %31, %28
%40 = load i32*, i32** %7
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
store i32* %43, i32** %12
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 2
store i32 %45, i32* %9
%46 = load i32*, i32** %7
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32* %49, i32** %13
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 2
store i32 %51, i32* %9
%52 = load i8*, i8** %10
%53 = load i32, i32* %11
%54 = load i32*, i32** %12
%55 = load i32, i32* %54
%56 = load i32*, i32** %13
%57 = load i32, i32* %56
%58 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %52, i32 %53, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %55, i32 %57)
%59 = load i8*, i8** %10
%60 = getelementptr inbounds i8, i8* %59, i64 9
store i8* %60, i8** %10
%61 = load i32, i32* %11
%62 = sub nsw i32 %61, 9
store i32 %62, i32* %11
br label %24
63: ; preds = %24
%64 = load i32, i32* %8
ret i32 %64
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"Malformed community of length %u\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [10 x i8] c"%04x:%04x\00"
define i32 @parse_bgp_path_attr_community(i8* %0, i32 %1, i32* nocapture %2, i32 returned %3) {
%5 = and i32 %3, 3
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %10
10: ; preds = %7, %4
%11 = icmp sgt i32 %3, 0
br i1 %11, label %12, label %36
12: ; preds = %10
%13 = load i32, i32* %2
%14 = getelementptr inbounds i32, i32* %2, i64 2
%15 = load i32, i32* %14
%16 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %15)
%17 = icmp ugt i32 %3, 4
br i1 %17, label %18, label %36
18: ; preds = %12, %18
%19 = phi i64 [ %30, %18 ], [ 4, %12 ]
%20 = phi i8* [ %25, %18 ], [ %0, %12 ]
%21 = phi i32 [ %26, %18 ], [ %1, %12 ]
%22 = add nsw i32 %21, -9
%23 = getelementptr inbounds i8, i8* %20, i64 9
%24 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %23, i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = getelementptr inbounds i8, i8* %20, i64 10
%26 = add nsw i32 %21, -10
%27 = getelementptr inbounds i32, i32* %2, i64 %19
%28 = or i64 %19, 2
%29 = getelementptr inbounds i32, i32* %2, i64 %28
%30 = add nuw i64 %19, 4
%31 = load i32, i32* %27
%32 = load i32, i32* %29
%33 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %25, i32 %26, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %32)
%34 = trunc i64 %30 to i32
%35 = icmp slt i32 %34, %3
br i1 %35, label %18, label %36
36: ; preds = %18, %12, %10
ret i32 %3
}