void add_tail(unsigned char target[], int tp, int tail_length, int last_mode) {
  int i, prn, temp;
  switch (last_mode) {
  case 2:
  case 3:
  case 4:
    target[tp] = 254;
    tp++;
    tail_length--;
  }
  for (i = tail_length; i > 0; i--) {
    if (i == tail_length) {
      target[tp] = 129;
      tp++;
    } else {
      prn = ((149 * (tp + 1)) % 253) + 1;
      temp = 129 + prn;
      if (temp <= 254) {
        target[tp] = temp;
        tp++;
      } else {
        target[tp] = temp - 254;
        tp++;
      }
    }
  }
}


x86 assembly (clang, -O0):
<add_tail>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0xfffffffe,%eax
sub    $0x2,%eax
ja     1143 <add_tail+0x43>
jmp    1125 <add_tail+0x25>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0xfe,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11ff <add_tail+0xff>
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    1179 <add_tail+0x79>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x81,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ec <add_tail+0xec>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
imul   $0x95,%eax,%eax
mov    $0xfd,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x81,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xfe,-0x20(%rbp)
jg     11c9 <add_tail+0xc9>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11e7 <add_tail+0xe7>
mov    -0x20(%rbp),%eax
sub    $0xfe,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ec <add_tail+0xec>
jmp    11f1 <add_tail+0xf1>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1149 <add_tail+0x49>
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_tail>:
add    $0xfffffffe,%ecx
cmp    $0x2,%ecx
ja     1115 <add_tail+0x15>
movslq %esi,%rsi
movb   $0xfe,(%rdi,%rsi,1)
add    $0x1,%esi
add    $0xffffffff,%edx
test   %edx,%edx
jle    1524 <add_tail+0x424>
movslq %esi,%rax
movb   $0x81,(%rdi,%rax,1)
cmp    $0x1,%edx
je     1524 <add_tail+0x424>
add    $0x1,%esi
xor    %ecx,%ecx
mov    %edx,%eax
sub    $0x2,%eax
cmovb  %ecx,%eax
add    $0x1,%eax
cmp    $0x10,%eax
jae    114a <add_tail+0x4a>
mov    %esi,%r8d
jmp    14c1 <add_tail+0x3c1>
mov    %eax,%r9d
and    $0xfffffff0,%r9d
lea    (%rsi,%r9,1),%r8d
sub    %r9d,%edx
movd   %esi,%xmm0
pshufd $0x0,%xmm0,%xmm9
movdqa 0xe95(%rip),%xmm10
paddd  %xmm9,%xmm10
movdqa 0xe98(%rip),%xmm2
paddd  %xmm9,%xmm2
movdqa 0xe9b(%rip),%xmm3
paddd  %xmm9,%xmm3
paddd  0xe9d(%rip),%xmm9
movdqa 0xea4(%rip),%xmm14
movdqa 0xeab(%rip),%xmm15
movdqa 0xeb2(%rip),%xmm13
movdqa 0xef9(%rip),%xmm12
mov    %r9d,%ecx
nopw   0x0(%rax,%rax,1)
movdqa %xmm10,%xmm4
pmuludq %xmm14,%xmm4
pshufd $0xe8,%xmm4,%xmm8
pshufd $0xf5,%xmm10,%xmm4
pmuludq %xmm14,%xmm4
pshufd $0xe8,%xmm4,%xmm4
punpckldq %xmm4,%xmm8
movdqa %xmm2,%xmm4
pmuludq %xmm14,%xmm4
pshufd $0xe8,%xmm4,%xmm6
pshufd $0xf5,%xmm2,%xmm4
pmuludq %xmm14,%xmm4
pshufd $0xe8,%xmm4,%xmm4
punpckldq %xmm4,%xmm6
movdqa %xmm3,%xmm4
pmuludq %xmm14,%xmm4
pshufd $0xe8,%xmm4,%xmm5
pshufd $0xf5,%xmm3,%xmm4
pmuludq %xmm14,%xmm4
pshufd $0xe8,%xmm4,%xmm4
punpckldq %xmm4,%xmm5
movdqa %xmm9,%xmm4
pmuludq %xmm14,%xmm4
pshufd $0xe8,%xmm4,%xmm4
pshufd $0xf5,%xmm9,%xmm1
pmuludq %xmm14,%xmm1
pshufd $0xe8,%xmm1,%xmm1
punpckldq %xmm1,%xmm4
paddd  %xmm14,%xmm4
paddd  %xmm14,%xmm5
paddd  %xmm14,%xmm6
paddd  %xmm14,%xmm8
pxor   %xmm1,%xmm1
pcmpgtd %xmm8,%xmm1
pand   %xmm15,%xmm1
paddd  %xmm8,%xmm1
movdqa %xmm8,%xmm7
pmuludq %xmm15,%xmm7
pshufd $0xed,%xmm7,%xmm7
pshufd $0xf5,%xmm8,%xmm0
pmuludq %xmm15,%xmm0
pshufd $0xed,%xmm0,%xmm0
punpckldq %xmm0,%xmm7
psubd  %xmm1,%xmm7
paddd  %xmm8,%xmm7
movdqa %xmm7,%xmm0
psrld  $0x1f,%xmm0
psrad  $0x7,%xmm7
paddd  %xmm0,%xmm7
pshufd $0xf5,%xmm7,%xmm0
pmuludq %xmm13,%xmm7
pshufd $0xe8,%xmm7,%xmm1
pmuludq %xmm13,%xmm0
pshufd $0xe8,%xmm0,%xmm0
punpckldq %xmm0,%xmm1
psubd  %xmm1,%xmm8
pxor   %xmm0,%xmm0
pcmpgtd %xmm6,%xmm0
pand   %xmm15,%xmm0
paddd  %xmm6,%xmm0
movdqa %xmm6,%xmm1
pmuludq %xmm15,%xmm1
pshufd $0xed,%xmm1,%xmm1
pshufd $0xf5,%xmm6,%xmm7
pmuludq %xmm15,%xmm7
pshufd $0xed,%xmm7,%xmm7
punpckldq %xmm7,%xmm1
psubd  %xmm0,%xmm1
paddd  %xmm6,%xmm1
movdqa %xmm1,%xmm0
psrld  $0x1f,%xmm0
psrad  $0x7,%xmm1
paddd  %xmm0,%xmm1
pshufd $0xf5,%xmm1,%xmm0
pmuludq %xmm13,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pmuludq %xmm13,%xmm0
pshufd $0xe8,%xmm0,%xmm0
punpckldq %xmm0,%xmm1
psubd  %xmm1,%xmm6
pxor   %xmm0,%xmm0
pcmpgtd %xmm5,%xmm0
pand   %xmm15,%xmm0
paddd  %xmm5,%xmm0
movdqa %xmm5,%xmm1
pmuludq %xmm15,%xmm1
pshufd $0xed,%xmm1,%xmm1
pshufd $0xf5,%xmm5,%xmm7
pmuludq %xmm15,%xmm7
pshufd $0xed,%xmm7,%xmm7
punpckldq %xmm7,%xmm1
psubd  %xmm0,%xmm1
paddd  %xmm5,%xmm1
movdqa %xmm1,%xmm0
psrld  $0x1f,%xmm0
psrad  $0x7,%xmm1
paddd  %xmm0,%xmm1
pshufd $0xf5,%xmm1,%xmm0
pmuludq %xmm13,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pmuludq %xmm13,%xmm0
pshufd $0xe8,%xmm0,%xmm0
punpckldq %xmm0,%xmm1
psubd  %xmm1,%xmm5
pxor   %xmm0,%xmm0
pcmpgtd %xmm4,%xmm0
pand   %xmm15,%xmm0
paddd  %xmm4,%xmm0
movdqa %xmm4,%xmm1
pmuludq %xmm15,%xmm1
pshufd $0xed,%xmm1,%xmm1
pshufd $0xf5,%xmm4,%xmm7
pmuludq %xmm15,%xmm7
pshufd $0xed,%xmm7,%xmm7
punpckldq %xmm7,%xmm1
psubd  %xmm0,%xmm1
paddd  %xmm4,%xmm1
movdqa %xmm1,%xmm0
psrld  $0x1f,%xmm0
psrad  $0x7,%xmm1
paddd  %xmm0,%xmm1
pshufd $0xf5,%xmm1,%xmm0
pmuludq %xmm13,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pmuludq %xmm13,%xmm0
pshufd $0xe8,%xmm0,%xmm0
punpckldq %xmm0,%xmm1
psubd  %xmm1,%xmm4
movdqa 0xc50(%rip),%xmm1
movdqa %xmm1,%xmm11
pcmpgtd %xmm4,%xmm11
movdqa %xmm1,%xmm0
pcmpgtd %xmm5,%xmm0
packssdw %xmm0,%xmm11
movdqa %xmm1,%xmm0
pcmpgtd %xmm6,%xmm0
pcmpgtd %xmm8,%xmm1
packssdw %xmm1,%xmm0
packsswb %xmm0,%xmm11
movdqa 0xc2b(%rip),%xmm0
pand   %xmm0,%xmm4
pand   %xmm0,%xmm5
packuswb %xmm5,%xmm4
pand   %xmm0,%xmm6
pand   %xmm0,%xmm8
packuswb %xmm8,%xmm6
packuswb %xmm6,%xmm4
movdqa %xmm11,%xmm0
pandn  0xc10(%rip),%xmm0
pand   0xc17(%rip),%xmm11
por    %xmm0,%xmm11
paddb  %xmm4,%xmm11
movslq %esi,%rsi
movdqu %xmm11,(%rdi,%rsi,1)
paddd  %xmm12,%xmm9
paddd  %xmm12,%xmm3
paddd  %xmm12,%xmm2
paddd  %xmm12,%xmm10
add    $0x10,%esi
add    $0xfffffff0,%ecx
jne    11c0 <add_tail+0xc0>
cmp    %r9d,%eax
je     1524 <add_tail+0x424>
movslq %r8d,%rax
add    %rax,%rdi
imul   $0x95,%eax,%eax
add    $0x95,%eax
add    $0x1,%edx
cs nopw 0x0(%rax,%rax,1)
nop
cltq
imul   $0xffffffff81848da9,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%esi
shr    $0x1f,%esi
sar    $0x7,%ecx
add    %esi,%ecx
imul   $0xfd,%ecx,%ecx
mov    %eax,%esi
sub    %ecx,%esi
cmp    $0x7d,%esi
setge  %cl
add    %cl,%cl
add    %sil,%cl
add    $0x82,%cl
mov    %cl,(%rdi)
add    $0x1,%rdi
add    $0x95,%eax
add    $0xffffffff,%edx
cmp    $0x2,%edx
ja     14e0 <add_tail+0x3e0>
ret

LLVM IR (-O0):
define void @add_tail(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
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %8
switch i32 %12, label %22 [
i32 2, label %13
i32 3, label %13
i32 4, label %13
]
13: ; preds = %4, %4, %4
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 -2, i8* %17
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
store i32 %19, i32* %6
%20 = load i32, i32* %7
%21 = add nsw i32 %20, -1
store i32 %21, i32* %7
br label %22
22: ; preds = %13, %4
%23 = load i32, i32* %7
store i32 %23, i32* %9
br label %24
24: ; preds = %69, %22
%25 = load i32, i32* %9
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %72
27: ; preds = %24
%28 = load i32, i32* %9
%29 = load i32, i32* %7
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 -127, i8* %35
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %68
38: ; preds = %27
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
%41 = mul nsw i32 149, %40
%42 = srem i32 %41, 253
%43 = add nsw i32 %42, 1
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = add nsw i32 129, %44
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = icmp sle i32 %46, 254
br i1 %47, label %48, label %57
48: ; preds = %38
%49 = load i32, i32* %11
%50 = trunc i32 %49 to i8
%51 = load i8*, i8** %5
%52 = load i32, i32* %6
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 %50, i8* %54
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %67
57: ; preds = %38
%58 = load i32, i32* %11
%59 = sub nsw i32 %58, 254
%60 = trunc i32 %59 to i8
%61 = load i8*, i8** %5
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8 %60, i8* %64
%65 = load i32, i32* %6
%66 = add nsw i32 %65, 1
store i32 %66, i32* %6
br label %67
67: ; preds = %57, %48
br label %68
68: ; preds = %67, %31
br label %69
69: ; preds = %68
%70 = load i32, i32* %9
%71 = add nsw i32 %70, -1
store i32 %71, i32* %9
br label %24
72: ; preds = %24
ret void
}

LLVM IR (-O2):
define void @add_tail(i8* nocapture writeonly %0, i32 %1, i32 %2, i32 %3) {
%5 = add i32 %3, -2
%6 = icmp ult i32 %5, 3
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = sext i32 %1 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 -2, i8* %9
%10 = add nsw i32 %1, 1
%11 = add nsw i32 %2, -1
br label %12
12: ; preds = %4, %7
%13 = phi i32 [ %2, %4 ], [ %11, %7 ]
%14 = phi i32 [ %1, %4 ], [ %10, %7 ]
%15 = icmp sgt i32 %13, 0
br i1 %15, label %16, label %69
16: ; preds = %12
%17 = sext i32 %14 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
store i8 -127, i8* %18
%19 = add nsw i32 %14, 1
%20 = icmp eq i32 %13, 1
br i1 %20, label %69, label %21
21: ; preds = %16
%22 = add i32 %13, 1
%23 = call i32 @llvm.umin.i32(i32 %13, i32 2)
%24 = sub i32 %22, %23
%25 = icmp ult i32 %24, 16
br i1 %25, label %52, label %26
26: ; preds = %21
%27 = and i32 %24, -16
%28 = add i32 %19, %27
%29 = sub i32 %13, %27
%30 = insertelement <16 x i32> poison, i32 %19, i64 0
%31 = shufflevector <16 x i32> %30, <16 x i32> poison, <16 x i32> zeroinitializer
%32 = add <16 x i32> %31, <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
br label %33
33: ; preds = %33, %26
%34 = phi i32 [ 0, %26 ], [ %47, %33 ]
%35 = phi <16 x i32> [ %32, %26 ], [ %48, %33 ]
%36 = add i32 %19, %34
%37 = mul <16 x i32> %35, <i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149>
%38 = add <16 x i32> %37, <i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149, i32 149>
%39 = srem <16 x i32> %38, <i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253, i32 253>
%40 = icmp slt <16 x i32> %39, <i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125, i32 125>
%41 = trunc <16 x i32> %39 to <16 x i8>
%42 = select <16 x i1> %40, <16 x i8> <i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126, i8 -126>, <16 x i8> <i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124, i8 -124>
%43 = add <16 x i8> %42, %41
%44 = sext i32 %36 to i64
%45 = getelementptr inbounds i8, i8* %0, i64 %44
%46 = bitcast i8* %45 to <16 x i8>*
store <16 x i8> %43, <16 x i8>* %46
%47 = add nuw i32 %34, 16
%48 = add <16 x i32> %35, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
%49 = icmp eq i32 %47, %27
br i1 %49, label %50, label %33
50: ; preds = %33
%51 = icmp eq i32 %24, %27
br i1 %51, label %69, label %52
52: ; preds = %21, %50
%53 = phi i32 [ %19, %21 ], [ %28, %50 ]
%54 = phi i32 [ %13, %21 ], [ %29, %50 ]
br label %55
55: ; preds = %52, %55
%56 = phi i32 [ %59, %55 ], [ %53, %52 ]
%57 = phi i32 [ %58, %55 ], [ %54, %52 ]
%58 = add nsw i32 %57, -1
%59 = add nsw i32 %56, 1
%60 = mul nsw i32 %59, 149
%61 = srem i32 %60, 253
%62 = icmp slt i32 %61, 125
%63 = trunc i32 %61 to i8
%64 = select i1 %62, i8 -126, i8 -124
%65 = add i8 %64, %63
%66 = sext i32 %56 to i64
%67 = getelementptr inbounds i8, i8* %0, i64 %66
store i8 %65, i8* %67
%68 = icmp ugt i32 %57, 2
br i1 %68, label %55, label %69
69: ; preds = %55, %50, %16, %12
ret void
}