#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*/ exit(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, char *, char *);
void perm_struct(void *a, int unit, int *perm, int siz) {
  int i;
  char *s, *ss = a;
  if (!(s = (char *)malloc(sizeof(char) * (unit * siz)))) {
    printf("%s:memory error %s", "perm_struct", "s");
    exit(1);
  };
  memcpy(s, ss, unit * siz);
  for (i = 0; i < siz; i++)
    memcpy(ss + unit * perm[i], s + unit * i, unit);
  free(s);
}


x86 assembly (clang, -O0):
<perm_struct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b4 <perm_struct+0x64>
lea    0xe69(%rip),%rdi
lea    0xe75(%rip),%rsi
lea    0xe6c(%rip),%rdx
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
imul   -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1219 <perm_struct+0xc9>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
movslq -0x20(%rbp),%rdx
imul   (%rcx,%rdx,4),%eax
cltq
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%eax
imul   -0x20(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0xc(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11cf <perm_struct+0x7f>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perm_struct>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r12
mov    %ecx,%ebp
imul   %esi,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d8 <perm_struct+0x88>
mov    %rax,(%rsp)
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
test   %r13d,%r13d
jle    11c1 <perm_struct+0x71>
movslq %ebx,%rbx
mov    %r13d,%r13d
xor    %r15d,%r15d
mov    (%rsp),%rbp
nopw   0x0(%rax,%rax,1)
movslq (%r14,%r15,4),%rdi
imul   %rbx,%rdi
add    %r12,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
add    $0x1,%r15
add    %rbx,%rbp
cmp    %r15,%r13
jne    11a0 <perm_struct+0x50>
mov    (%rsp),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xe21(%rip),%rdi
lea    0xe2d(%rip),%rsi
lea    0xe24(%rip),%rdx
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"%s:memory error %s\00"
@.str.1 = [12 x i8] c"perm_struct\00"
@.str.2 = [2 x i8] c"s\00"
define void @perm_struct(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 i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
store i8* %12, i8** %11
%13 = load i32, i32* %6
%14 = load i32, i32* %8
%15 = mul nsw i32 %13, %14
%16 = sext i32 %15 to i64
%17 = mul i64 1, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %10
%21 = icmp ne i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %4
%23 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %4
%26 = load i8*, i8** %10
%27 = load i8*, i8** %11
%28 = load i32, i32* %6
%29 = load i32, i32* %8
%30 = mul nsw i32 %28, %29
%31 = call i32 @memcpy(i8* %26, i8* %27, i32 %30)
store i32 0, i32* %9
br label %32
32: ; preds = %55, %25
%33 = load i32, i32* %9
%34 = load i32, i32* %8
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %58
36: ; preds = %32
%37 = load i8*, i8** %11
%38 = load i32, i32* %6
%39 = load i32*, i32** %7
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = mul nsw i32 %38, %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %37, i64 %45
%47 = load i8*, i8** %10
%48 = load i32, i32* %6
%49 = load i32, i32* %9
%50 = mul nsw i32 %48, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %47, i64 %51
%53 = load i32, i32* %6
%54 = call i32 @memcpy(i8* %46, i8* %52, i32 %53)
br label %55
55: ; preds = %36
%56 = load i32, i32* %9
%57 = add nsw i32 %56, 1
store i32 %57, i32* %9
br label %32
58: ; preds = %32
%59 = load i8*, i8** %10
%60 = call i32 @free(i8* %59)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"%s:memory error %s\00"
@.str.1 = [12 x i8] c"perm_struct\00"
@.str.2 = [2 x i8] c"s\00"
define void @perm_struct(i8* %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = mul nsw i32 %3, %1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %4
%13 = tail call i32 @memcpy(i8* %7, i8* %0, i32 %5)
%14 = icmp sgt i32 %3, 0
br i1 %14, label %15, label %30
15: ; preds = %12
%16 = sext i32 %1 to i64
%17 = zext i32 %3 to i64
br label %18
18: ; preds = %15, %18
%19 = phi i64 [ 0, %15 ], [ %28, %18 ]
%20 = getelementptr inbounds i32, i32* %2, i64 %19
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, %1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = mul nsw i64 %19, %16
%26 = getelementptr inbounds i8, i8* %7, i64 %25
%27 = tail call i32 @memcpy(i8* %24, i8* %26, i32 %1)
%28 = add nuw nsw i64 %19, 1
%29 = icmp eq i64 %28, %17
br i1 %29, label %30, label %18
30: ; preds = %18, %12
%31 = tail call i32 @free(i8* %7)
ret void
}