#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*/ u_int32_t;
typedef int /*<<< orphan*/ u_int16_t;
typedef int u_char;

/* Variables and functions */
char *fcat(char *, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int ntohl(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *printASPath(u_char *asPath, int ASNumLen) {
  char *asBuffer = (char *)malloc(4096);
  int l = asPath[1];
  int i;
  int strlen = 128;
  char str[strlen];
  char *dst = asBuffer;
  char *max = asBuffer + 1024;
  *dst = '\0';
  for (i = 2; i < (l * ASNumLen + 2); i = i + ASNumLen) {
    if (ASNumLen == 2) {
      snprintf(str, strlen, "%d", ntohs(*((u_int16_t *)(asPath + i))));
    } else if (ASNumLen == 4) {
      snprintf(str, strlen, "%d", ntohl(*((u_int32_t *)(asPath + i))));
    } else {
      free(asBuffer);
      return NULL;
    }
    dst = fcat(dst, max, str);
    if (i < (l * ASNumLen + 2)) {
      dst = fcat(dst, max, " ");
    }
  }
  asBuffer[dst - asBuffer] = 0;
  return asBuffer;
}


x86 assembly (clang, -O0):
<printASPath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x80,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x2,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    12d8 <printASPath+0x178>
cmpl   $0x2,-0x14(%rbp)
jne    1227 <printASPath+0xc7>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1070 <ntohs@plt>
mov    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%esi
mov    %eax,%ecx
lea    0xde3(%rip),%rdx
call   1040 <snprintf@plt>
jmp    1283 <printASPath+0x123>
cmpl   $0x4,-0x14(%rbp)
jne    1261 <printASPath+0x101>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1080 <ntohl@plt>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%esi
mov    %eax,%ecx
lea    0xda9(%rip),%rdx
call   1040 <snprintf@plt>
jmp    127e <printASPath+0x11e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12fa <printASPath+0x19a>
jmp    1283 <printASPath+0x123>
mov    -0x60(%rbp),%rdx
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1050 <fcat@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    12c5 <printASPath+0x165>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
lea    0xd47(%rip),%rdx
call   1050 <fcat@plt>
mov    %rax,-0x48(%rbp)
jmp    12ca <printASPath+0x16a>
mov    -0x28(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11d8 <printASPath+0x78>
mov    -0x20(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printASPath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %esi,%ebx
mov    %rdi,%rbp
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbp,0x18(%rsp)
mov    0x4(%rbp),%eax
imul   %ebx,%eax
movb   $0x0,(%r12)
test   %eax,%eax
jle    123d <printASPath+0xdd>
cmp    $0x4,%ebx
je     11ac <printASPath+0x4c>
cmp    $0x2,%ebx
jne    124f <printASPath+0xef>
add    $0x400,%r12
add    $0x2,%eax
mov    %ebx,0xc(%rsp)
mov    %ebx,%r14d
mov    %eax,%r15d
mov    $0x2,%r13d
lea    0x20(%rsp),%rbp
mov    %rdi,0x10(%rsp)
mov    %rdi,%rbx
jmp    1226 <printASPath+0xc6>
cs nopw 0x0(%rax,%rax,1)
nop
call   1080 <ntohl@plt>
mov    %rbp,%rdi
mov    $0x80,%esi
lea    0xe0c(%rip),%rdx
mov    %eax,%ecx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1050 <fcat@plt>
mov    %rax,%rdi
mov    %r12,%rsi
lea    0xded(%rip),%rdx
call   1050 <fcat@plt>
mov    %rax,%rbx
add    %r14,%r13
cmp    %r15,%r13
jge    1245 <printASPath+0xe5>
mov    0x18(%rsp),%rax
mov    (%rax,%r13,4),%edi
cmpl   $0x2,0xc(%rsp)
jne    11e0 <printASPath+0x80>
call   1070 <ntohs@plt>
jmp    11e5 <printASPath+0x85>
mov    %rdi,0x10(%rsp)
mov    %r12,%rbx
movb   $0x0,(%rbx)
mov    0x10(%rsp),%rax
jmp    1256 <printASPath+0xf6>
call   1030 <free@plt>
xor    %eax,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [2 x i8] c" \00"
define i8* @printASPath(i32* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%15 = call i64 @malloc(i32 4096)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %6
%17 = load i32*, i32** %4
%18 = getelementptr inbounds i32, i32* %17, i64 1
%19 = load i32, i32* %18
store i32 %19, i32* %7
store i32 128, i32* %9
%20 = load i32, i32* %9
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %10
%23 = alloca i8, i64 %21
store i64 %21, i64* %11
%24 = load i8*, i8** %6
store i8* %24, i8** %12
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 1024
store i8* %26, i8** %13
%27 = load i8*, i8** %12
store i8 0, i8* %27
store i32 2, i32* %8
br label %28
28: ; preds = %78, %2
%29 = load i32, i32* %8
%30 = load i32, i32* %7
%31 = load i32, i32* %5
%32 = mul nsw i32 %30, %31
%33 = add nsw i32 %32, 2
%34 = icmp slt i32 %29, %33
br i1 %34, label %35, label %82
35: ; preds = %28
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, 2
br i1 %37, label %38, label %47
38: ; preds = %35
%39 = load i32, i32* %9
%40 = load i32*, i32** %4
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
%45 = call i32 @ntohs(i32 %44)
%46 = call i32 @snprintf(i8* %23, i32 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %45)
br label %63
47: ; preds = %35
%48 = load i32, i32* %5
%49 = icmp eq i32 %48, 4
br i1 %49, label %50, label %59
50: ; preds = %47
%51 = load i32, i32* %9
%52 = load i32*, i32** %4
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = call i32 @ntohl(i32 %56)
%58 = call i32 @snprintf(i8* %23, i32 %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %57)
br label %62
59: ; preds = %47
%60 = load i8*, i8** %6
%61 = call i32 @free(i8* %60)
store i8* null, i8** %3
store i32 1, i32* %14
br label %91
62: ; preds = %50
br label %63
63: ; preds = %62, %38
%64 = load i8*, i8** %12
%65 = load i8*, i8** %13
%66 = call i8* @fcat(i8* %64, i8* %65, i8* %23)
store i8* %66, i8** %12
%67 = load i32, i32* %8
%68 = load i32, i32* %7
%69 = load i32, i32* %5
%70 = mul nsw i32 %68, %69
%71 = add nsw i32 %70, 2
%72 = icmp slt i32 %67, %71
br i1 %72, label %73, label %77
73: ; preds = %63
%74 = load i8*, i8** %12
%75 = load i8*, i8** %13
%76 = call i8* @fcat(i8* %74, i8* %75, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %76, i8** %12
br label %77
77: ; preds = %73, %63
br label %78
78: ; preds = %77
%79 = load i32, i32* %8
%80 = load i32, i32* %5
%81 = add nsw i32 %79, %80
store i32 %81, i32* %8
br label %28
82: ; preds = %28
%83 = load i8*, i8** %6
%84 = load i8*, i8** %12
%85 = load i8*, i8** %6
%86 = ptrtoint i8* %84 to i64
%87 = ptrtoint i8* %85 to i64
%88 = sub i64 %86, %87
%89 = getelementptr inbounds i8, i8* %83, i64 %88
store i8 0, i8* %89
%90 = load i8*, i8** %6
store i8* %90, i8** %3
store i32 1, i32* %14
br label %91
91: ; preds = %82, %59
%92 = load i8*, i8** %10
%93 = load i8*, i8** %3
ret i8* %93
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [2 x i8] c" \00"
define i8* @printASPath(i32* nocapture %0, i32 %1) {
%3 = tail call i64 @malloc(i32 4096)
%4 = inttoptr i64 %3 to i8*
%5 = getelementptr inbounds i32, i32* %0, i64 1
%6 = load i32, i32* %5
%7 = alloca [128 x i8]
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%9 = getelementptr inbounds i8, i8* %4, i64 1024
store i8 0, i8* %4
%10 = mul nsw i32 %6, %1
%11 = add nsw i32 %10, 2
%12 = icmp sgt i32 %10, 0
br i1 %12, label %13, label %36
13: ; preds = %2
switch i32 %1, label %27 [
i32 2, label %14
i32 4, label %14
]
14: ; preds = %13, %13
%15 = zext i32 %1 to i64
%16 = zext i32 %11 to i64
%17 = icmp eq i32 %1, 2
br label %18
18: ; preds = %14, %29
%19 = phi i64 [ 2, %14 ], [ %34, %29 ]
%20 = phi i8* [ %4, %14 ], [ %33, %29 ]
%21 = getelementptr inbounds i32, i32* %0, i64 %19
%22 = load i32, i32* %21
br i1 %17, label %23, label %25
23: ; preds = %18
%24 = call i32 @ntohs(i32 %22)
br label %29
25: ; preds = %18
%26 = call i32 @ntohl(i32 %22)
br label %29
27: ; preds = %13
%28 = call i32 @free(i8* %4)
br label %41
29: ; preds = %25, %23
%30 = phi i32 [ %26, %25 ], [ %24, %23 ]
%31 = call i32 @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = call i8* @fcat(i8* %20, i8* %9, i8* %8)
%33 = call i8* @fcat(i8* %32, i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%34 = add i64 %19, %15
%35 = icmp slt i64 %34, %16
br i1 %35, label %18, label %36
36: ; preds = %29, %2
%37 = phi i8* [ %4, %2 ], [ %33, %29 ]
%38 = ptrtoint i8* %37 to i64
%39 = sub i64 %38, %3
%40 = getelementptr inbounds i8, i8* %4, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %36, %27
%42 = phi i8* [ null, %27 ], [ %4, %36 ]
ret i8* %42
}