#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 */
unsigned char *calloc(int, int);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ switchcharsequence(unsigned char *, int, int, int);
void switchcharacters(unsigned char *mapping, int len,
                      unsigned char *scrambling, int slen, int dflag) {
  unsigned char scramblingchar = '0';
  unsigned char needswitch = '0';
  while (slen > 0) {
    scramblingchar = scrambling[slen - 1];
    int i = 0;
    int buckets = 1;
    for (i = 0; i < 8; i++) {
      if (dflag)
        printf(
            "DEBUG - Current scrambling character value is '%c' (%d), run %d\n",
            scramblingchar, scramblingchar, i);
      needswitch = scramblingchar & 0x01;
      if (needswitch == 0x01) {
        int startpos = 0;
        int endpos = 0;
        int j = 0;
        for (j = 0; j < buckets; j++) {
          startpos = j * (len / buckets);
          endpos = (j + 1) * (len / buckets) - 1;
          if (dflag)
            printf("DEBUG - Switching mapping with position sequence %d, %d\n",
                   startpos, endpos);
          switchcharsequence(mapping, startpos, endpos, dflag);
        }
      }
      buckets <<= 1;
      scramblingchar >>= 1;
      if (buckets >= len)
        break;
    }
    slen--;
  }
  if (dflag) {
    unsigned char *debugout = calloc(len + 1, sizeof(char));
    memcpy(debugout, mapping, len);
    debugout[len] = 0x00;
    printf("DEBUG - New mapping sequence is now %s (length is %d)\n", debugout,
           len);
    free(debugout);
  }
}


x86 assembly (clang, -O0):
<switchcharacters>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movb   $0x30,-0x21(%rbp)
movb   $0x30,-0x22(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    12ce <switchcharacters+0x17e>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x8,-0x28(%rbp)
jge    12c0 <switchcharacters+0x170>
cmpl   $0x0,-0x20(%rbp)
je     11d1 <switchcharacters+0x81>
movzbl -0x21(%rbp),%esi
movzbl -0x21(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movzbl -0x21(%rbp),%eax
and    $0x1,%eax
mov    %al,-0x22(%rbp)
movzbl -0x22(%rbp),%eax
cmp    $0x1,%eax
jne    1289 <switchcharacters+0x139>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1284 <switchcharacters+0x134>
mov    -0x38(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
imul   %ecx,%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1264 <switchcharacters+0x114>
mov    -0x30(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1070 <switchcharsequence@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1204 <switchcharacters+0xb4>
jmp    1289 <switchcharacters+0x139>
mov    -0x2c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movzbl -0x21(%rbp),%eax
sar    $0x1,%eax
mov    %al,-0x21(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     12ad <switchcharacters+0x15d>
jmp    12c0 <switchcharacters+0x170>
jmp    12b2 <switchcharacters+0x162>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11a4 <switchcharacters+0x54>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1172 <switchcharacters+0x22>
cmpl   $0x0,-0x20(%rbp)
je     1326 <switchcharacters+0x1d6>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x40(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switchcharacters>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%ebp
mov    %rdx,0x20(%rsp)
mov    %rsi,0x8(%rsp)
mov    %rdi,0x18(%rsp)
test   %ecx,%ecx
jle    1278 <switchcharacters+0x128>
mov    %ecx,%eax
jmp    1191 <switchcharacters+0x41>
nopl   0x0(%rax)
cmpq   $0x1,0x28(%rsp)
mov    0x30(%rsp),%rax
jle    1278 <switchcharacters+0x128>
mov    %rax,0x28(%rsp)
add    $0xffffffffffffffff,%rax
mov    %rax,0x30(%rsp)
mov    %eax,%eax
mov    0x20(%rsp),%rcx
mov    (%rcx,%rax,1),%r14b
movl   $0x1,0x4(%rsp)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %ebp,%ebp
je     11da <switchcharacters+0x8a>
movzbl %r14b,%edx
lea    0xe31(%rip),%rdi
mov    %edx,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,0x14(%rsp)
mov    %r14b,0x3(%rsp)
test   $0x1,%r14b
jne    1220 <switchcharacters+0xd0>
mov    0x4(%rsp),%eax
add    %eax,%eax
mov    %eax,0x4(%rsp)
cmp    0x8(%rsp),%eax
mov    0x3(%rsp),%r14b
mov    0x14(%rsp),%ebx
jge    1180 <switchcharacters+0x30>
shr    %r14b
add    $0x1,%ebx
cmp    $0x8,%ebx
jne    11c0 <switchcharacters+0x70>
jmp    1180 <switchcharacters+0x30>
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
cltd
mov    0x4(%rsp),%ebx
idiv   %ebx
mov    %eax,%r13d
lea    -0x1(%r13),%r15d
xor    %r14d,%r14d
jmp    125a <switchcharacters+0x10a>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rdi
mov    %r14d,%esi
mov    %r12d,%edx
mov    %ebp,%ecx
call   1070 <switchcharsequence@plt>
add    %r13d,%r14d
add    $0xffffffff,%ebx
je     11e9 <switchcharacters+0x99>
lea    (%r15,%r14,1),%r12d
test   %ebp,%ebp
je     1240 <switchcharacters+0xf0>
lea    0xdd8(%rip),%rdi
mov    %r14d,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1240 <switchcharacters+0xf0>
test   %ebp,%ebp
je     12d1 <switchcharacters+0x181>
mov    0x8(%rsp),%rbx
lea    0x1(%rbx),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    0x18(%rsp),%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,0x0(%rbp,%rax,1)
lea    0xdcb(%rip),%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [65 x i8] c"DEBUG - Current scrambling character value is '%c' (%d), run %d\0A\00"
@.str.1 = [57 x i8] c"DEBUG - Switching mapping with position sequence %d, %d\0A\00"
@.str.2 = [55 x i8] c"DEBUG - New mapping sequence is now %s (length is %d)\0A\00"
define void @switchcharacters(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i8
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i8 48, i8* %11
store i8 48, i8* %12
br label %19
19: ; preds = %99, %5
%20 = load i32, i32* %9
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %102
22: ; preds = %19
%23 = load i8*, i8** %8
%24 = load i32, i32* %9
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i8, i8* %27
store i8 %28, i8* %11
store i32 0, i32* %13
store i32 1, i32* %14
store i32 0, i32* %13
br label %29
29: ; preds = %96, %22
%30 = load i32, i32* %13
%31 = icmp slt i32 %30, 8
br i1 %31, label %32, label %99
32: ; preds = %29
%33 = load i32, i32* %10
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %32
%36 = load i8, i8* %11
%37 = zext i8 %36 to i32
%38 = load i8, i8* %11
%39 = zext i8 %38 to i32
%40 = load i32, i32* %13
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i32 %37, i32 %39, i32 %40)
br label %42
42: ; preds = %35, %32
%43 = load i8, i8* %11
%44 = zext i8 %43 to i32
%45 = and i32 %44, 1
%46 = trunc i32 %45 to i8
store i8 %46, i8* %12
%47 = load i8, i8* %12
%48 = zext i8 %47 to i32
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %84
50: ; preds = %42
store i32 0, i32* %15
store i32 0, i32* %16
store i32 0, i32* %17
store i32 0, i32* %17
br label %51
51: ; preds = %80, %50
%52 = load i32, i32* %17
%53 = load i32, i32* %14
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %83
55: ; preds = %51
%56 = load i32, i32* %17
%57 = load i32, i32* %7
%58 = load i32, i32* %14
%59 = sdiv i32 %57, %58
%60 = mul nsw i32 %56, %59
store i32 %60, i32* %15
%61 = load i32, i32* %17
%62 = add nsw i32 %61, 1
%63 = load i32, i32* %7
%64 = load i32, i32* %14
%65 = sdiv i32 %63, %64
%66 = mul nsw i32 %62, %65
%67 = sub nsw i32 %66, 1
store i32 %67, i32* %16
%68 = load i32, i32* %10
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %55
%71 = load i32, i32* %15
%72 = load i32, i32* %16
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %71, i32 %72)
br label %74
74: ; preds = %70, %55
%75 = load i8*, i8** %6
%76 = load i32, i32* %15
%77 = load i32, i32* %16
%78 = load i32, i32* %10
%79 = call i32 @switchcharsequence(i8* %75, i32 %76, i32 %77, i32 %78)
br label %80
80: ; preds = %74
%81 = load i32, i32* %17
%82 = add nsw i32 %81, 1
store i32 %82, i32* %17
br label %51
83: ; preds = %51
br label %84
84: ; preds = %83, %42
%85 = load i32, i32* %14
%86 = shl i32 %85, 1
store i32 %86, i32* %14
%87 = load i8, i8* %11
%88 = zext i8 %87 to i32
%89 = ashr i32 %88, 1
%90 = trunc i32 %89 to i8
store i8 %90, i8* %11
%91 = load i32, i32* %14
%92 = load i32, i32* %7
%93 = icmp sge i32 %91, %92
br i1 %93, label %94, label %95
94: ; preds = %84
br label %99
95: ; preds = %84
br label %96
96: ; preds = %95
%97 = load i32, i32* %13
%98 = add nsw i32 %97, 1
store i32 %98, i32* %13
br label %29
99: ; preds = %94, %29
%100 = load i32, i32* %9
%101 = add nsw i32 %100, -1
store i32 %101, i32* %9
br label %19
102: ; preds = %19
%103 = load i32, i32* %10
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %122
105: ; preds = %102
%106 = load i32, i32* %7
%107 = add nsw i32 %106, 1
%108 = call i8* @calloc(i32 %107, i32 1)
store i8* %108, i8** %18
%109 = load i8*, i8** %18
%110 = load i8*, i8** %6
%111 = load i32, i32* %7
%112 = call i32 @memcpy(i8* %109, i8* %110, i32 %111)
%113 = load i8*, i8** %18
%114 = load i32, i32* %7
%115 = sext i32 %114 to i64
%116 = getelementptr inbounds i8, i8* %113, i64 %115
store i8 0, i8* %116
%117 = load i8*, i8** %18
%118 = load i32, i32* %7
%119 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %117, i32 %118)
%120 = load i8*, i8** %18
%121 = call i32 @free(i8* %120)
br label %122
122: ; preds = %105, %102
ret void
}

LLVM IR (-O2):
@.str = [65 x i8] c"DEBUG - Current scrambling character value is '%c' (%d), run %d\0A\00"
@.str.1 = [57 x i8] c"DEBUG - Switching mapping with position sequence %d, %d\0A\00"
@.str.2 = [55 x i8] c"DEBUG - New mapping sequence is now %s (length is %d)\0A\00"
define void @switchcharacters(i8* %0, i32 %1, i8* nocapture %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %48
7: ; preds = %5
%8 = icmp eq i32 %4, 0
%9 = zext i32 %3 to i64
br label %10
10: ; preds = %7, %46
%11 = phi i64 [ %9, %7 ], [ %12, %46 ]
%12 = add nsw i64 %11, -1
%13 = and i64 %12, 4294967295
%14 = getelementptr inbounds i8, i8* %2, i64 %13
%15 = load i8, i8* %14
br label %16
16: ; preds = %10, %42
%17 = phi i32 [ 1, %10 ], [ %40, %42 ]
%18 = phi i32 [ 0, %10 ], [ %44, %42 ]
%19 = phi i8 [ %15, %10 ], [ %43, %42 ]
br i1 %8, label %23, label %20
20: ; preds = %16
%21 = zext i8 %19 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %21, i32 %18)
br label %23
23: ; preds = %20, %16
%24 = and i8 %19, 1
%25 = icmp eq i8 %24, 0
br i1 %25, label %39, label %26
26: ; preds = %23
%27 = sdiv i32 %1, %17
br label %28
28: ; preds = %26, %36
%29 = phi i32 [ 0, %26 ], [ %31, %36 ]
%30 = mul nsw i32 %27, %29
%31 = add nuw nsw i32 %29, 1
%32 = mul nsw i32 %27, %31
%33 = add nsw i32 %32, -1
br i1 %8, label %36, label %34
34: ; preds = %28
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %33)
br label %36
36: ; preds = %34, %28
%37 = tail call i32 @switchcharsequence(i8* %0, i32 %30, i32 %33, i32 %4)
%38 = icmp eq i32 %31, %17
br i1 %38, label %39, label %28
39: ; preds = %36, %23
%40 = shl i32 %17, 1
%41 = icmp slt i32 %40, %1
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = lshr i8 %19, 1
%44 = add nuw nsw i32 %18, 1
%45 = icmp eq i32 %44, 8
br i1 %45, label %46, label %16
46: ; preds = %39, %42
%47 = icmp sgt i64 %11, 1
br i1 %47, label %10, label %48
48: ; preds = %46, %5
%49 = icmp eq i32 %4, 0
br i1 %49, label %58, label %50
50: ; preds = %48
%51 = add nsw i32 %1, 1
%52 = tail call i8* @calloc(i32 %51, i32 1)
%53 = tail call i32 @memcpy(i8* %52, i8* %0, i32 %1)
%54 = sext i32 %1 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %52, i32 %1)
%57 = tail call i32 @free(i8* %52)
br label %58
58: ; preds = %50, %48
ret void
}