#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 *, int);
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("memory error %s (%d byte)\n", "perm_struct",
           sizeof(char) * (unit * siz));
    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    11bc <perm_struct+0x6c>
mov    -0xc(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltq
shl    $0x0,%rax
mov    %eax,%edx
lea    0xe5a(%rip),%rdi
lea    0xe6e(%rip),%rsi
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    1221 <perm_struct+0xd1>
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    11d7 <perm_struct+0x87>
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    0xe35(%rip),%rsi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [27 x i8] c"memory error %s (%d byte)\0A\00"
@.str.1 = [12 x i8] c"perm_struct\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 ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%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
}