#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 */
double **allocate_matrix(int, int);
int /*<<< orphan*/ deallocate_matrix(double ***);
void transpose_matrix(double ***matrix, int *num_rows, int *num_cols) {
  double **tmp_matrix = allocate_matrix(*num_cols, *num_rows);
  double **tmp_swap = *matrix;
  int tmp = *num_rows;
  int i, j;
  for (i = 0; i < *num_rows; ++i) {
    for (j = 0; j < *num_cols; ++j) {
      tmp_matrix[j][i] = (*matrix)[i][j];
    }
  }
  *num_rows = *num_cols;
  *num_cols = tmp;
  *matrix = tmp_matrix;
  deallocate_matrix(&tmp_swap);
}


x86 assembly (clang, -O0):
<transpose_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1030 <allocate_matrix@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    11d7 <transpose_matrix+0xb7>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    11c4 <transpose_matrix+0xa4>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x30(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    117a <transpose_matrix+0x5a>
jmp    11c9 <transpose_matrix+0xa9>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1164 <transpose_matrix+0x44>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
lea    -0x28(%rbp),%rdi
call   1040 <deallocate_matrix@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transpose_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    (%rdx),%edi
mov    (%rsi),%esi
call   1030 <allocate_matrix@plt>
mov    (%r14),%r8
mov    %r8,(%rsp)
mov    (%r12),%r10d
mov    (%r15),%r11d
test   %r10d,%r10d
jle    120d <transpose_matrix+0xed>
lea    -0x1(%r11),%r9
mov    %r11d,%esi
and    $0x3,%esi
mov    %r11d,%r13d
and    $0xfffffffc,%r13d
xor    %edi,%edi
jmp    117d <transpose_matrix+0x5d>
nopl   0x0(%rax)
add    $0x1,%rdi
cmp    %r10,%rdi
je     120d <transpose_matrix+0xed>
test   %r11d,%r11d
jle    1170 <transpose_matrix+0x50>
mov    (%r8,%rdi,8),%rcx
xor    %edx,%edx
cmp    $0x3,%r9
jb     11d7 <transpose_matrix+0xb7>
xchg   %ax,%ax
movsd  (%rcx,%rdx,8),%xmm0
mov    (%rax,%rdx,8),%rbx
movsd  %xmm0,(%rbx,%rdi,8)
movsd  0x8(%rcx,%rdx,8),%xmm0
mov    0x8(%rax,%rdx,8),%rbx
movsd  %xmm0,(%rbx,%rdi,8)
movsd  0x10(%rcx,%rdx,8),%xmm0
mov    0x10(%rax,%rdx,8),%rbx
movsd  %xmm0,(%rbx,%rdi,8)
movsd  0x18(%rcx,%rdx,8),%xmm0
mov    0x18(%rax,%rdx,8),%rbx
movsd  %xmm0,(%rbx,%rdi,8)
add    $0x4,%rdx
cmp    %rdx,%r13
jne    1190 <transpose_matrix+0x70>
test   %rsi,%rsi
je     1170 <transpose_matrix+0x50>
lea    (%rax,%rdx,8),%rbx
lea    (%rcx,%rdx,8),%rcx
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
movsd  (%rcx,%rdx,8),%xmm0
mov    (%rbx,%rdx,8),%rbp
movsd  %xmm0,0x0(%rbp,%rdi,8)
add    $0x1,%rdx
cmp    %rdx,%rsi
jne    11f0 <transpose_matrix+0xd0>
jmp    1170 <transpose_matrix+0x50>
mov    %r11d,(%r12)
mov    %r10d,(%r15)
mov    %rax,(%r14)
mov    %rsp,%rdi
call   1040 <deallocate_matrix@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @transpose_matrix(double*** %0, i32* %1, i32* %2) {
%4 = alloca double***
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca double**
%8 = alloca double**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store double*** %0, double**** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%12 = load i32*, i32** %6
%13 = load i32, i32* %12
%14 = load i32*, i32** %5
%15 = load i32, i32* %14
%16 = call double** @allocate_matrix(i32 %13, i32 %15)
store double** %16, double*** %7
%17 = load double***, double**** %4
%18 = load double**, double*** %17
store double** %18, double*** %8
%19 = load i32*, i32** %5
%20 = load i32, i32* %19
store i32 %20, i32* %9
store i32 0, i32* %10
br label %21
21: ; preds = %55, %3
%22 = load i32, i32* %10
%23 = load i32*, i32** %5
%24 = load i32, i32* %23
%25 = icmp slt i32 %22, %24
br i1 %25, label %26, label %58
26: ; preds = %21
store i32 0, i32* %11
br label %27
27: ; preds = %51, %26
%28 = load i32, i32* %11
%29 = load i32*, i32** %6
%30 = load i32, i32* %29
%31 = icmp slt i32 %28, %30
br i1 %31, label %32, label %54
32: ; preds = %27
%33 = load double***, double**** %4
%34 = load double**, double*** %33
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds double*, double** %34, i64 %36
%38 = load double*, double** %37
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %38, i64 %40
%42 = load double, double* %41
%43 = load double**, double*** %7
%44 = load i32, i32* %11
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds double*, double** %43, i64 %45
%47 = load double*, double** %46
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds double, double* %47, i64 %49
store double %42, double* %50
br label %51
51: ; preds = %32
%52 = load i32, i32* %11
%53 = add nsw i32 %52, 1
store i32 %53, i32* %11
br label %27
54: ; preds = %27
br label %55
55: ; preds = %54
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %21
58: ; preds = %21
%59 = load i32*, i32** %6
%60 = load i32, i32* %59
%61 = load i32*, i32** %5
store i32 %60, i32* %61
%62 = load i32, i32* %9
%63 = load i32*, i32** %6
store i32 %62, i32* %63
%64 = load double**, double*** %7
%65 = load double***, double**** %4
store double** %64, double*** %65
%66 = call i32 @deallocate_matrix(double*** %8)
ret void
}

LLVM IR (-O2):
define void @transpose_matrix(double*** nocapture %0, i32* nocapture %1, i32* nocapture %2) {
%4 = alloca double**
%5 = load i32, i32* %2
%6 = load i32, i32* %1
%7 = tail call double** @allocate_matrix(i32 %5, i32 %6)
%8 = bitcast double*** %4 to i8*
%9 = load double**, double*** %0
store double** %9, double*** %4
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 0
%12 = load i32, i32* %2
br i1 %11, label %13, label %72
13: ; preds = %3
%14 = icmp sgt i32 %12, 0
%15 = zext i32 %10 to i64
%16 = zext i32 %12 to i64
%17 = add nsw i64 %16, -1
%18 = and i64 %16, 3
%19 = icmp ult i64 %17, 3
%20 = and i64 %16, 4294967292
%21 = icmp eq i64 %18, 0
br label %22
22: ; preds = %13, %69
%23 = phi i64 [ 0, %13 ], [ %70, %69 ]
br i1 %14, label %24, label %69
24: ; preds = %22
%25 = getelementptr inbounds double*, double** %9, i64 %23
%26 = load double*, double** %25
br i1 %19, label %56, label %27
27: ; preds = %24, %27
%28 = phi i64 [ %53, %27 ], [ 0, %24 ]
%29 = phi i64 [ %54, %27 ], [ 0, %24 ]
%30 = getelementptr inbounds double, double* %26, i64 %28
%31 = load double, double* %30
%32 = getelementptr inbounds double*, double** %7, i64 %28
%33 = load double*, double** %32
%34 = getelementptr inbounds double, double* %33, i64 %23
store double %31, double* %34
%35 = or i64 %28, 1
%36 = getelementptr inbounds double, double* %26, i64 %35
%37 = load double, double* %36
%38 = getelementptr inbounds double*, double** %7, i64 %35
%39 = load double*, double** %38
%40 = getelementptr inbounds double, double* %39, i64 %23
store double %37, double* %40
%41 = or i64 %28, 2
%42 = getelementptr inbounds double, double* %26, i64 %41
%43 = load double, double* %42
%44 = getelementptr inbounds double*, double** %7, i64 %41
%45 = load double*, double** %44
%46 = getelementptr inbounds double, double* %45, i64 %23
store double %43, double* %46
%47 = or i64 %28, 3
%48 = getelementptr inbounds double, double* %26, i64 %47
%49 = load double, double* %48
%50 = getelementptr inbounds double*, double** %7, i64 %47
%51 = load double*, double** %50
%52 = getelementptr inbounds double, double* %51, i64 %23
store double %49, double* %52
%53 = add nuw nsw i64 %28, 4
%54 = add i64 %29, 4
%55 = icmp eq i64 %54, %20
br i1 %55, label %56, label %27
56: ; preds = %27, %24
%57 = phi i64 [ 0, %24 ], [ %53, %27 ]
br i1 %21, label %69, label %58
58: ; preds = %56, %58
%59 = phi i64 [ %66, %58 ], [ %57, %56 ]
%60 = phi i64 [ %67, %58 ], [ 0, %56 ]
%61 = getelementptr inbounds double, double* %26, i64 %59
%62 = load double, double* %61
%63 = getelementptr inbounds double*, double** %7, i64 %59
%64 = load double*, double** %63
%65 = getelementptr inbounds double, double* %64, i64 %23
store double %62, double* %65
%66 = add nuw nsw i64 %59, 1
%67 = add i64 %60, 1
%68 = icmp eq i64 %67, %18
br i1 %68, label %69, label %58
69: ; preds = %56, %58, %22
%70 = add nuw nsw i64 %23, 1
%71 = icmp eq i64 %70, %15
br i1 %71, label %72, label %22
72: ; preds = %69, %3
store i32 %12, i32* %1
store i32 %10, i32* %2
store double** %7, double*** %0
%73 = call i32 @deallocate_matrix(double*** %4)
ret void
}