#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 */
struct Mapa {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ mapsolved(struct Mapa *);
int mostPopularColorOnPerimeter(struct Mapa *);
int /*<<< orphan*/ pintamapa(struct Mapa *, int);
int /*<<< orphan*/ printf(char *, ...);
int *realloc(int *, int);
int shortestPathToDiagonal(struct Mapa *);
void bestfirstsearch(struct Mapa *mapa) {
  int reachedBottomRight = 0;
  int *solucao = NULL;
  int lensol = 0;
  while (!mapsolved(mapa)) {
    if (!reachedBottomRight) {
      int shortestPath = shortestPathToDiagonal(mapa);
      if (shortestPath == 0)
        reachedBottomRight = 1;
      else {
        solucao = realloc(solucao, sizeof(int) * (lensol + 1));
        solucao[lensol] = shortestPath;
        pintamapa(mapa, shortestPath);
        lensol++;
      }
    } else {
      int mostSquaresOnPerimeter = mostPopularColorOnPerimeter(mapa);
      pintamapa(mapa, mostSquaresOnPerimeter);
      solucao = realloc(solucao, sizeof(int) * (lensol + 1));
      solucao[lensol] = mostSquaresOnPerimeter;
      lensol++;
    }
  }
  printf("%d\n", lensol);
  for (int i = 0; i < lensol; i++) {
    printf("%d", solucao[i]);
    if (i != lensol - 1)
      printf(" ");
    else
      printf("\n");
  }
  free(solucao);
}


x86 assembly (clang, -O0):
<bestfirstsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <mapsolved@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b0 <bestfirstsearch+0x40>
jmp    126e <bestfirstsearch+0xfe>
cmpl   $0x0,-0xc(%rbp)
jne    121f <bestfirstsearch+0xaf>
mov    -0x8(%rbp),%rdi
call   1040 <shortestPathToDiagonal@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11dc <bestfirstsearch+0x6c>
movl   $0x1,-0xc(%rbp)
jmp    121a <bestfirstsearch+0xaa>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1050 <pintamapa@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1269 <bestfirstsearch+0xf9>
mov    -0x8(%rbp),%rdi
call   1090 <mostPopularColorOnPerimeter@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <pintamapa@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1192 <bestfirstsearch+0x22>
mov    -0x1c(%rbp),%esi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12f0 <bestfirstsearch+0x180>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     12cf <bestfirstsearch+0x15f>
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12dd <bestfirstsearch+0x16d>
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12e2 <bestfirstsearch+0x172>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1286 <bestfirstsearch+0x116>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bestfirstsearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1090 <mapsolved@plt>
test   %eax,%eax
je     11af <bestfirstsearch+0x2f>
lea    0xe62(%rip),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12a8 <bestfirstsearch+0x128>
xor    %ebp,%ebp
xor    %r14d,%r14d
xor    %r12d,%r12d
jmp    1204 <bestfirstsearch+0x84>
nopl   0x0(%rax)
call   10a0 <mostPopularColorOnPerimeter@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <pintamapa@plt>
lea    0x1(%r12),%ebx
lea    0x4(,%r12,4),%esi
mov    %r14,%rdi
call   1080 <realloc@plt>
mov    %rax,%r14
movslq %r12d,%rax
mov    %ebp,(%r14,%rax,4)
mov    $0x1,%ebp
mov    %ebx,%r12d
mov    %r15,%rdi
call   1090 <mapsolved@plt>
test   %eax,%eax
jne    124a <bestfirstsearch+0xca>
mov    %r15,%rdi
test   %ebp,%ebp
jne    11c0 <bestfirstsearch+0x40>
call   1050 <shortestPathToDiagonal@plt>
test   %eax,%eax
je     1243 <bestfirstsearch+0xc3>
mov    %eax,%ebp
lea    0x1(%r12),%ebx
lea    0x4(,%r12,4),%esi
mov    %r14,%rdi
call   1080 <realloc@plt>
mov    %rax,%r14
movslq %r12d,%rax
mov    %ebp,(%r14,%rax,4)
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <pintamapa@plt>
xor    %ebp,%ebp
jmp    11f5 <bestfirstsearch+0x75>
mov    $0x1,%ebp
jmp    11f8 <bestfirstsearch+0x78>
lea    0xdaf(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %r12d,%r12d
jle    12a8 <bestfirstsearch+0x128>
lea    -0x1(%r12),%r13d
mov    %r12d,%ebx
lea    0xd95(%rip),%r15
mov    $0xa,%r12d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbp,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    %rbp,%r13
mov    $0x20,%edi
cmove  %r12d,%edi
call   1040 <putchar@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1280 <bestfirstsearch+0x100>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.Mapa = type { i32 }
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"\0A\00"
define void @bestfirstsearch(%struct.Mapa* %0) {
%2 = alloca %struct.Mapa*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.Mapa* %0, %struct.Mapa** %2
store i32 0, i32* %3
store i32* null, i32** %4
store i32 0, i32* %5
br label %9
9: ; preds = %62, %1
%10 = load %struct.Mapa*, %struct.Mapa** %2
%11 = call i32 @mapsolved(%struct.Mapa* %10)
%12 = icmp ne i32 %11, 0
%13 = xor i1 %12, true
br i1 %13, label %14, label %63
14: ; preds = %9
%15 = load i32, i32* %3
%16 = icmp ne i32 %15, 0
br i1 %16, label %42, label %17
17: ; preds = %14
%18 = load %struct.Mapa*, %struct.Mapa** %2
%19 = call i32 @shortestPathToDiagonal(%struct.Mapa* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 1, i32* %3
br label %41
23: ; preds = %17
%24 = load i32*, i32** %4
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = mul i64 4, %27
%29 = trunc i64 %28 to i32
%30 = call i32* @realloc(i32* %24, i32 %29)
store i32* %30, i32** %4
%31 = load i32, i32* %6
%32 = load i32*, i32** %4
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
store i32 %31, i32* %35
%36 = load %struct.Mapa*, %struct.Mapa** %2
%37 = load i32, i32* %6
%38 = call i32 @pintamapa(%struct.Mapa* %36, i32 %37)
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
br label %41
41: ; preds = %23, %22
br label %62
42: ; preds = %14
%43 = load %struct.Mapa*, %struct.Mapa** %2
%44 = call i32 @mostPopularColorOnPerimeter(%struct.Mapa* %43)
store i32 %44, i32* %7
%45 = load %struct.Mapa*, %struct.Mapa** %2
%46 = load i32, i32* %7
%47 = call i32 @pintamapa(%struct.Mapa* %45, i32 %46)
%48 = load i32*, i32** %4
%49 = load i32, i32* %5
%50 = add nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = mul i64 4, %51
%53 = trunc i64 %52 to i32
%54 = call i32* @realloc(i32* %48, i32 %53)
store i32* %54, i32** %4
%55 = load i32, i32* %7
%56 = load i32*, i32** %4
%57 = load i32, i32* %5
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %56, i64 %58
store i32 %55, i32* %59
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 1
store i32 %61, i32* %5
br label %62
62: ; preds = %42, %41
br label %9
63: ; preds = %9
%64 = load i32, i32* %5
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %64)
store i32 0, i32* %8
br label %66
66: ; preds = %86, %63
%67 = load i32, i32* %8
%68 = load i32, i32* %5
%69 = icmp slt i32 %67, %68
br i1 %69, label %70, label %89
70: ; preds = %66
%71 = load i32*, i32** %4
%72 = load i32, i32* %8
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %74
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %75)
%77 = load i32, i32* %8
%78 = load i32, i32* %5
%79 = sub nsw i32 %78, 1
%80 = icmp ne i32 %77, %79
br i1 %80, label %81, label %83
81: ; preds = %70
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %85
83: ; preds = %70
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %85
85: ; preds = %83, %81
br label %86
86: ; preds = %85
%87 = load i32, i32* %8
%88 = add nsw i32 %87, 1
store i32 %88, i32* %8
br label %66
89: ; preds = %66
%90 = load i32*, i32** %4
%91 = call i32 @free(i32* %90)
ret void
}

LLVM IR (-O2):
%struct.Mapa = type { i32 }
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
define void @bestfirstsearch(%struct.Mapa* %0) {
%2 = tail call i32 @mapsolved(%struct.Mapa* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 0)
br label %42
6: ; preds = %1, %29
%7 = phi i32 [ %32, %29 ], [ 0, %1 ]
%8 = phi i32* [ %31, %29 ], [ null, %1 ]
%9 = phi i32 [ %30, %29 ], [ 0, %1 ]
%10 = icmp eq i32 %7, 0
br i1 %10, label %11, label %21
11: ; preds = %6
%12 = tail call i32 @shortestPathToDiagonal(%struct.Mapa* %0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %29, label %14
14: ; preds = %11
%15 = add nsw i32 %9, 1
%16 = shl i32 %15, 2
%17 = tail call i32* @realloc(i32* %8, i32 %16)
%18 = sext i32 %9 to i64
%19 = getelementptr inbounds i32, i32* %17, i64 %18
store i32 %12, i32* %19
%20 = tail call i32 @pintamapa(%struct.Mapa* %0, i32 %12)
br label %29
21: ; preds = %6
%22 = tail call i32 @mostPopularColorOnPerimeter(%struct.Mapa* %0)
%23 = tail call i32 @pintamapa(%struct.Mapa* %0, i32 %22)
%24 = add nsw i32 %9, 1
%25 = shl i32 %24, 2
%26 = tail call i32* @realloc(i32* %8, i32 %25)
%27 = sext i32 %9 to i64
%28 = getelementptr inbounds i32, i32* %26, i64 %27
store i32 %22, i32* %28
br label %29
29: ; preds = %14, %11, %21
%30 = phi i32 [ %24, %21 ], [ %15, %14 ], [ %9, %11 ]
%31 = phi i32* [ %26, %21 ], [ %17, %14 ], [ %8, %11 ]
%32 = phi i32 [ 1, %21 ], [ 0, %14 ], [ 1, %11 ]
%33 = tail call i32 @mapsolved(%struct.Mapa* %0)
%34 = icmp eq i32 %33, 0
br i1 %34, label %6, label %35
35: ; preds = %29
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %30)
%37 = icmp sgt i32 %30, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = add nsw i32 %30, -1
%40 = zext i32 %39 to i64
%41 = zext i32 %30 to i64
br label %45
42: ; preds = %45, %4, %35
%43 = phi i32* [ null, %4 ], [ %31, %35 ], [ %31, %45 ]
%44 = tail call i32 @free(i32* %43)
ret void
45: ; preds = %38, %45
%46 = phi i64 [ 0, %38 ], [ %53, %45 ]
%47 = getelementptr inbounds i32, i32* %31, i64 %46
%48 = load i32, i32* %47
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = icmp eq i64 %46, %40
%51 = select i1 %50, i32 10, i32 32
%52 = tail call i32 @putchar(i32 %51)
%53 = add nuw nsw i64 %46, 1
%54 = icmp eq i64 %53, %41
br i1 %54, label %42, label %45
}