#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 */
typedef int /*<<< orphan*/ puzzle_copy;

/* Variables and functions */
short dfs(short *, short, int /*<<< orphan*/, char);
int /*<<< orphan*/ is_solvable(short *);
scalar_t__ is_solved(short *);
short manhattan_dist(short *);
int /*<<< orphan*/ memcpy(short *, short *, int);
short solve_puzzle(short *puzzle, short limit) {
  short max_cost, moves;
  short puzzle_copy[16];
  memcpy(puzzle_copy, puzzle, sizeof(puzzle_copy));
  if (is_solved(puzzle))
    return 0;
  if (!is_solvable(puzzle))
    return -1;
  max_cost = manhattan_dist(puzzle);
  while (max_cost <= limit) {
    memcpy(puzzle_copy, puzzle, sizeof(puzzle_copy));
    moves = dfs(puzzle_copy, max_cost, 0, ' ');
    if (moves < 0)
      if (max_cost == limit)
        break;
      else
        max_cost = (((limit) < (max_cost + 5)) ? (limit) : (max_cost + 5));
    else
      return moves;
  }
  return -1;
}


x86 assembly (clang, -O0):
<solve_puzzle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1060 <is_solved@plt>
cmp    $0x0,%rax
je     1193 <solve_puzzle+0x43>
movw   $0x0,-0x2(%rbp)
jmp    1266 <solve_puzzle+0x116>
mov    -0x10(%rbp),%rdi
call   1040 <is_solvable@plt>
cmp    $0x0,%eax
jne    11b0 <solve_puzzle+0x60>
movw   $0xffff,-0x2(%rbp)
jmp    1266 <solve_puzzle+0x116>
mov    -0x10(%rbp),%rdi
call   1030 <manhattan_dist@plt>
mov    %ax,-0x14(%rbp)
movswl -0x14(%rbp),%eax
movswl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jg     1260 <solve_puzzle+0x110>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
lea    -0x40(%rbp),%rdi
xor    %edx,%edx
mov    $0x20,%ecx
movswl -0x14(%rbp),%esi
call   1070 <dfs@plt>
mov    %ax,-0x16(%rbp)
movswl -0x16(%rbp),%eax
cmp    $0x0,%eax
jge    124e <solve_puzzle+0xfe>
movswl -0x14(%rbp),%eax
movswl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jne    1219 <solve_puzzle+0xc9>
jmp    1260 <solve_puzzle+0x110>
movswl -0x12(%rbp),%eax
movswl -0x14(%rbp),%ecx
add    $0x5,%ecx
cmp    %ecx,%eax
jge    1238 <solve_puzzle+0xe8>
movswl -0x12(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    1242 <solve_puzzle+0xf2>
movswl -0x14(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %ax,-0x14(%rbp)
jmp    125b <solve_puzzle+0x10b>
mov    -0x16(%rbp),%ax
mov    %ax,-0x2(%rbp)
jmp    1266 <solve_puzzle+0x116>
jmp    11bd <solve_puzzle+0x6d>
movw   $0xffff,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<solve_puzzle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r12d
mov    %rdi,%r13
mov    %rsp,%rdi
mov    %r13,%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    %r13,%rdi
call   1060 <is_solved@plt>
test   %rax,%rax
je     1194 <solve_puzzle+0x44>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   1040 <is_solvable@plt>
mov    $0xffff,%bx
test   %eax,%eax
je     1183 <solve_puzzle+0x33>
mov    %r13,%rdi
call   1030 <manhattan_dist@plt>
cmp    %r12w,%ax
jg     1183 <solve_puzzle+0x33>
mov    %eax,%ebp
movswl %r12w,%r14d
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
movswl %bp,%ebx
mov    %r15,%rdi
mov    %r13,%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %edx,%edx
mov    $0x20,%ecx
call   1070 <dfs@plt>
test   %ax,%ax
jns    120a <solve_puzzle+0xba>
cmp    %r12w,%bp
je     1201 <solve_puzzle+0xb1>
add    $0x5,%ebx
cmp    %r14d,%ebx
cmovg  %r14d,%ebx
mov    %ebx,%ebp
cmp    %r12w,%bx
jle    11c0 <solve_puzzle+0x70>
mov    $0xffff,%bx
jmp    1183 <solve_puzzle+0x33>
mov    %eax,%ebx
jmp    1183 <solve_puzzle+0x33>

LLVM IR (-O0):
define signext i16 @solve_puzzle(i16* %0, i16 signext %1) {
%3 = alloca i16
%4 = alloca i16*
%5 = alloca i16
%6 = alloca i16
%7 = alloca i16
%8 = alloca [16 x i16]
store i16* %0, i16** %4
store i16 %1, i16* %5
%9 = getelementptr inbounds [16 x i16], [16 x i16]* %8, i64 0, i64 0
%10 = load i16*, i16** %4
%11 = call i32 @memcpy(i16* %9, i16* %10, i32 32)
%12 = load i16*, i16** %4
%13 = call i64 @is_solved(i16* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i16 0, i16* %3
br label %69
16: ; preds = %2
%17 = load i16*, i16** %4
%18 = call i32 @is_solvable(i16* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %16
store i16 -1, i16* %3
br label %69
21: ; preds = %16
%22 = load i16*, i16** %4
%23 = call signext i16 @manhattan_dist(i16* %22)
store i16 %23, i16* %6
br label %24
24: ; preds = %67, %21
%25 = load i16, i16* %6
%26 = sext i16 %25 to i32
%27 = load i16, i16* %5
%28 = sext i16 %27 to i32
%29 = icmp sle i32 %26, %28
br i1 %29, label %30, label %68
30: ; preds = %24
%31 = getelementptr inbounds [16 x i16], [16 x i16]* %8, i64 0, i64 0
%32 = load i16*, i16** %4
%33 = call i32 @memcpy(i16* %31, i16* %32, i32 32)
%34 = getelementptr inbounds [16 x i16], [16 x i16]* %8, i64 0, i64 0
%35 = load i16, i16* %6
%36 = call signext i16 @dfs(i16* %34, i16 signext %35, i32 0, i8 signext 32)
store i16 %36, i16* %7
%37 = load i16, i16* %7
%38 = sext i16 %37 to i32
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %65
40: ; preds = %30
%41 = load i16, i16* %6
%42 = sext i16 %41 to i32
%43 = load i16, i16* %5
%44 = sext i16 %43 to i32
%45 = icmp eq i32 %42, %44
br i1 %45, label %46, label %47
46: ; preds = %40
br label %68
47: ; preds = %40
%48 = load i16, i16* %5
%49 = sext i16 %48 to i32
%50 = load i16, i16* %6
%51 = sext i16 %50 to i32
%52 = add nsw i32 %51, 5
%53 = icmp slt i32 %49, %52
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = load i16, i16* %5
%56 = sext i16 %55 to i32
br label %61
57: ; preds = %47
%58 = load i16, i16* %6
%59 = sext i16 %58 to i32
%60 = add nsw i32 %59, 5
br label %61
61: ; preds = %57, %54
%62 = phi i32 [ %56, %54 ], [ %60, %57 ]
%63 = trunc i32 %62 to i16
store i16 %63, i16* %6
br label %64
64: ; preds = %61
br label %67
65: ; preds = %30
%66 = load i16, i16* %7
store i16 %66, i16* %3
br label %69
67: ; preds = %64
br label %24
68: ; preds = %46, %24
store i16 -1, i16* %3
br label %69
69: ; preds = %68, %65, %20, %15
%70 = load i16, i16* %3
ret i16 %70
}

LLVM IR (-O2):
define signext i16 @solve_puzzle(i16* %0, i16 signext %1) {
%3 = alloca [16 x i16]
%4 = bitcast [16 x i16]* %3 to i8*
%5 = getelementptr inbounds [16 x i16], [16 x i16]* %3, i64 0, i64 0
%6 = call i32 @memcpy(i16* %5, i16* %0, i32 32)
%7 = call i64 @is_solved(i16* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %2
%10 = call i32 @is_solvable(i16* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %30, label %12
12: ; preds = %9
%13 = call signext i16 @manhattan_dist(i16* %0)
%14 = sext i16 %1 to i32
%15 = icmp sgt i16 %13, %1
br i1 %15, label %30, label %16
16: ; preds = %12, %24
%17 = phi i16 [ %28, %24 ], [ %13, %12 ]
%18 = sext i16 %17 to i32
%19 = call i32 @memcpy(i16* %5, i16* %0, i32 32)
%20 = call signext i16 @dfs(i16* %5, i16 signext %17, i32 0, i8 signext 32)
%21 = icmp slt i16 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = icmp eq i16 %17, %1
br i1 %23, label %30, label %24
24: ; preds = %22
%25 = add nsw i32 %18, 5
%26 = icmp sgt i32 %25, %14
%27 = select i1 %26, i32 %14, i32 %25
%28 = trunc i32 %27 to i16
%29 = icmp sgt i16 %28, %1
br i1 %29, label %30, label %16
30: ; preds = %16, %22, %24, %12, %9, %2
%31 = phi i16 [ 0, %2 ], [ -1, %9 ], [ -1, %12 ], [ %20, %16 ], [ -1, %22 ], [ -1, %24 ]
ret i16 %31
}