#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 */
scalar_t__ alocaMatriz(int, int);
int /*<<< orphan*/ copiaMatriz(int **, int **, int, int);
int /*<<< orphan*/ desalocaMatriz(int **, int);
int mediana(int **, int, int);
int /*<<< orphan*/ printf(char *);
int mediana3x3(int **matriz, int linhas, int colunas) {
  if (linhas > 1 && colunas > 1) {
    int i, j;
    int **matrizTemp = NULL;
    matrizTemp = (int **)alocaMatriz(linhas, colunas);
    if (!matrizTemp) {
      printf("Nao foi possivel alocar");
      return 0;
    }
    copiaMatriz(matriz, matrizTemp, linhas, colunas);
    for (i = 1; i < linhas - 1; i++) {
      for (j = 1; j < (colunas - 1); j++) {
        matriz[i][j] = mediana(matrizTemp, i, j);
      }
    }
    desalocaMatriz(matrizTemp, linhas);
    return 1;
  } else {
    printf("Nao eh possivel calcular a mediana 3x3");
    return 0;
  }
}


x86 assembly (clang, -O0):
<mediana3x3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    1250 <mediana3x3+0x100>
cmpl   $0x1,-0x18(%rbp)
jle    1250 <mediana3x3+0x100>
movq   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <alocaMatriz@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b0 <mediana3x3+0x60>
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1263 <mediana3x3+0x113>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1070 <copiaMatriz@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1238 <mediana3x3+0xe8>
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1225 <mediana3x3+0xd5>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1060 <mediana@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11e2 <mediana3x3+0x92>
jmp    122a <mediana3x3+0xda>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ca <mediana3x3+0x7a>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <desalocaMatriz@plt>
movl   $0x1,-0x4(%rbp)
jmp    1263 <mediana3x3+0x113>
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mediana3x3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%esi
jl     11fe <mediana3x3+0xae>
mov    %edx,%ebp
cmp    $0x2,%edx
jl     11fe <mediana3x3+0xae>
mov    %esi,%ebx
mov    %rdi,%r12
mov    %esi,%edi
mov    %ebp,%esi
call   1030 <alocaMatriz@plt>
test   %rax,%rax
je     121b <mediana3x3+0xcb>
mov    %rax,%r13
mov    %r12,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1070 <copiaMatriz@plt>
mov    %rbx,0x8(%rsp)
cmp    $0x3,%ebx
jb     1207 <mediana3x3+0xb7>
mov    0x8(%rsp),%rax
add    $0xffffffff,%eax
mov    %rax,0x10(%rsp)
lea    -0x1(%rbp),%r15d
mov    $0x1,%ebx
jmp    11ce <mediana3x3+0x7e>
nopl   (%rax)
add    $0x1,%rbx
cmp    0x10(%rsp),%rbx
mov    %r14,%rbp
je     1207 <mediana3x3+0xb7>
mov    %rbp,%r14
cmp    $0x3,%ebp
jb     11c0 <mediana3x3+0x70>
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <mediana@plt>
mov    (%r12,%rbx,8),%rcx
mov    %eax,(%rcx,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%r15
jne    11e0 <mediana3x3+0x90>
jmp    11c0 <mediana3x3+0x70>
lea    0xe13(%rip),%rdi
jmp    1222 <mediana3x3+0xd2>
mov    %r13,%rdi
mov    0x8(%rsp),%rsi
call   1050 <desalocaMatriz@plt>
mov    $0x1,%eax
jmp    1229 <mediana3x3+0xd9>
lea    0xdde(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Nao foi possivel alocar\00"
@.str.1 = [39 x i8] c"Nao eh possivel calcular a mediana 3x3\00"
define i32 @mediana3x3(i32** %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32**
store i32** %0, i32*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i32, i32* %6
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %66
13: ; preds = %3
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %66
16: ; preds = %13
store i32** null, i32*** %10
%17 = load i32, i32* %6
%18 = load i32, i32* %7
%19 = call i64 @alocaMatriz(i32 %17, i32 %18)
%20 = inttoptr i64 %19 to i32**
store i32** %20, i32*** %10
%21 = load i32**, i32*** %10
%22 = icmp ne i32** %21, null
br i1 %22, label %25, label %23
23: ; preds = %16
%24 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %68
25: ; preds = %16
%26 = load i32**, i32*** %5
%27 = load i32**, i32*** %10
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i32 @copiaMatriz(i32** %26, i32** %27, i32 %28, i32 %29)
store i32 1, i32* %8
br label %31
31: ; preds = %59, %25
%32 = load i32, i32* %8
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, 1
%35 = icmp slt i32 %32, %34
br i1 %35, label %36, label %62
36: ; preds = %31
store i32 1, i32* %9
br label %37
37: ; preds = %55, %36
%38 = load i32, i32* %9
%39 = load i32, i32* %7
%40 = sub nsw i32 %39, 1
%41 = icmp slt i32 %38, %40
br i1 %41, label %42, label %58
42: ; preds = %37
%43 = load i32**, i32*** %10
%44 = load i32, i32* %8
%45 = load i32, i32* %9
%46 = call i32 @mediana(i32** %43, i32 %44, i32 %45)
%47 = load i32**, i32*** %5
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32*, i32** %47, i64 %49
%51 = load i32*, i32** %50
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
store i32 %46, i32* %54
br label %55
55: ; preds = %42
%56 = load i32, i32* %9
%57 = add nsw i32 %56, 1
store i32 %57, i32* %9
br label %37
58: ; preds = %37
br label %59
59: ; preds = %58
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
br label %31
62: ; preds = %31
%63 = load i32**, i32*** %10
%64 = load i32, i32* %6
%65 = call i32 @desalocaMatriz(i32** %63, i32 %64)
store i32 1, i32* %4
br label %68
66: ; preds = %13, %3
%67 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %68
68: ; preds = %66, %62, %23
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@.str = [24 x i8] c"Nao foi possivel alocar\00"
@.str.1 = [39 x i8] c"Nao eh possivel calcular a mediana 3x3\00"
define i32 @mediana3x3(i32** %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %1, 1
%5 = icmp sgt i32 %2, 1
%6 = and i1 %4, %5
br i1 %6, label %7, label %40
7: ; preds = %3
%8 = tail call i64 @alocaMatriz(i32 %1, i32 %2)
%9 = inttoptr i64 %8 to i32**
%10 = icmp eq i64 %8, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %42
13: ; preds = %7
%14 = tail call i32 @copiaMatriz(i32** %0, i32** %9, i32 %1, i32 %2)
%15 = icmp ugt i32 %1, 2
br i1 %15, label %16, label %38
16: ; preds = %13
%17 = add nsw i32 %1, -1
%18 = add nsw i32 %2, -1
%19 = icmp ugt i32 %2, 2
%20 = zext i32 %17 to i64
%21 = zext i32 %18 to i64
br label %22
22: ; preds = %16, %35
%23 = phi i64 [ 1, %16 ], [ %36, %35 ]
br i1 %19, label %24, label %35
24: ; preds = %22
%25 = getelementptr inbounds i32*, i32** %0, i64 %23
%26 = trunc i64 %23 to i32
br label %27
27: ; preds = %24, %27
%28 = phi i64 [ 1, %24 ], [ %33, %27 ]
%29 = trunc i64 %28 to i32
%30 = tail call i32 @mediana(i32** %9, i32 %26, i32 %29)
%31 = load i32*, i32** %25
%32 = getelementptr inbounds i32, i32* %31, i64 %28
store i32 %30, i32* %32
%33 = add nuw nsw i64 %28, 1
%34 = icmp eq i64 %33, %21
br i1 %34, label %35, label %27
35: ; preds = %27, %22
%36 = add nuw nsw i64 %23, 1
%37 = icmp eq i64 %36, %20
br i1 %37, label %38, label %22
38: ; preds = %35, %13
%39 = tail call i32 @desalocaMatriz(i32** %9, i32 %1)
br label %42
40: ; preds = %3
%41 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %11, %38, %40
%43 = phi i32 [ 0, %40 ], [ 1, %38 ], [ 0, %11 ]
ret i32 %43
}