#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
void escreve_solucao(FILE *fpin, char *argv[], int solucao, int altura,
                     int largura, int matriz[altura][largura],
                     int matriz_solucao[altura][largura]) {
  FILE *fpout;
  char nome[50];
  char *temp;
  char linha[1024];
  strcpy(nome, argv[1]);
  temp = strchr(nome, '.');
  *temp = '\0';
  strcat(nome, ".sol");
  fpout = fopen(nome, "w");
  if (fpout == NULL) {
    printf("error: output file could not be created.\n");
    exit(-1);
  }
  rewind(fpin);
  while (fgets(linha, 1024, fpin) != NULL) {
    fprintf(fpout, "%s", linha);
  }
  int i = 0;
  int j;
  if (solucao == 0) {
    for (i = 0; i < altura; i++) {
      fprintf(fpout, "-");
      for (j = 1; j < largura; j++) {
        fprintf(fpout, ":");
        if (matriz[i][j] == -1) {
          fprintf(fpout, "%c", matriz_solucao[i][j] + 48);
        } else {
          fprintf(fpout, "-");
        }
      }
      fprintf(fpout, "\n");
    }
  }
  fprintf(fpout, "\n");
  fclose(fpout);
  return;
}


x86 assembly (clang, -O0):
<escreve_solucao>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rax,-0x498(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rax,-0x490(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x70(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strchr@plt>
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x70(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   10b0 <strcat@plt>
lea    -0x70(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1248 <escreve_solucao+0xa8>
lea    0xdce(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1070 <rewind@plt>
lea    -0x480(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     128e <escreve_solucao+0xee>
mov    -0x30(%rbp),%rdi
lea    -0x480(%rbp),%rdx
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1251 <escreve_solucao+0xb1>
movl   $0x0,-0x484(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    13bd <escreve_solucao+0x21d>
movl   $0x0,-0x484(%rbp)
mov    -0x484(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    13b8 <escreve_solucao+0x218>
mov    -0x30(%rbp),%rdi
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x488(%rbp)
mov    -0x488(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1392 <escreve_solucao+0x1f2>
mov    -0x30(%rbp),%rdi
lea    0xd45(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x498(%rbp),%rdx
mov    -0x28(%rbp),%rax
movslq -0x484(%rbp),%rcx
imul   %rdx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
movslq -0x488(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
jne    1367 <escreve_solucao+0x1c7>
mov    -0x490(%rbp),%rdx
mov    -0x30(%rbp),%rdi
mov    0x10(%rbp),%rax
movslq -0x484(%rbp),%rcx
imul   %rdx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
movslq -0x488(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
add    $0x30,%edx
lea    0xcdd(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1379 <escreve_solucao+0x1d9>
mov    -0x30(%rbp),%rdi
lea    0xcc2(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    137e <escreve_solucao+0x1de>
mov    -0x488(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x488(%rbp)
jmp    12d7 <escreve_solucao+0x137>
mov    -0x30(%rbp),%rdi
lea    0xc92(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x484(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x484(%rbp)
jmp    12ac <escreve_solucao+0x10c>
jmp    13bd <escreve_solucao+0x21d>
mov    -0x30(%rbp),%rdi
lea    0xc67(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escreve_solucao>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x468,%rsp
mov    %r9,%r15
mov    %r8d,0xc(%rsp)
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %rdi,%r13
mov    0x8(%rsi),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
movb   $0x0,(%rax)
lea    0xe07(%rip),%rsi
mov    %rbp,%rdi
call   10c0 <strcat@plt>
lea    0xdfd(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     133a <escreve_solucao+0x18a>
mov    %rax,%rbp
mov    %r13,%rdi
call   1070 <rewind@plt>
lea    0x60(%rsp),%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1260 <escreve_solucao+0xb0>
lea    0x60(%rsp),%r12
mov    %r12,%rdi
mov    %rbp,%rsi
call   1080 <fputs@plt>
mov    %r12,%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
jne    1240 <escreve_solucao+0x90>
test   %ebx,%ebx
jne    1313 <escreve_solucao+0x163>
test   %r14d,%r14d
jle    1313 <escreve_solucao+0x163>
mov    0xc(%rsp),%ebx
mov    0x4a0(%rsp),%r12
mov    %r14d,%eax
mov    %rax,0x18(%rsp)
lea    0x0(,%rbx,4),%rax
mov    %rax,0x10(%rsp)
xor    %r13d,%r13d
jmp    12c3 <escreve_solucao+0x113>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1090 <fputc@plt>
add    $0x1,%r13
mov    0x10(%rsp),%rax
add    %rax,%r12
add    %rax,%r15
cmp    0x18(%rsp),%r13
je     1313 <escreve_solucao+0x163>
mov    $0x2d,%edi
mov    %rbp,%rsi
call   1090 <fputc@plt>
cmpl   $0x2,0xc(%rsp)
jl     12a0 <escreve_solucao+0xf0>
mov    $0x1,%r14d
jmp    12f1 <escreve_solucao+0x141>
nop
mov    %rbp,%rsi
call   1090 <fputc@plt>
add    $0x1,%r14
cmp    %r14,%rbx
je     12a0 <escreve_solucao+0xf0>
mov    $0x3a,%edi
mov    %rbp,%rsi
call   1090 <fputc@plt>
mov    $0x2d,%edi
cmpl   $0xffffffff,(%r15,%r14,4)
jne    12e0 <escreve_solucao+0x130>
mov    (%r12,%r14,4),%edi
add    $0x30,%edi
jmp    12e0 <escreve_solucao+0x130>
mov    $0xa,%edi
mov    %rbp,%rsi
call   1090 <fputc@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
add    $0x468,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcc6(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c".sol\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [42 x i8] c"error: output file could not be created.\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [2 x i8] c"-\00"
@.str.5 = [2 x i8] c":\00"
@.str.6 = [3 x i8] c"%c\00"
@.str.7 = [2 x i8] c"\0A\00"
define void @escreve_solucao(i32* %0, i8** %1, i32 %2, i32 %3, i32 %4, i32* %5, i32* %6) {
%8 = alloca i32*
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca [50 x i8]
%17 = alloca i8*
%18 = alloca [1024 x i8]
%19 = alloca i32
%20 = alloca i32
store i32* %0, i32** %8
store i8** %1, i8*** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32* %5, i32** %13
store i32* %6, i32** %14
%21 = load i32, i32* %11
%22 = zext i32 %21 to i64
%23 = load i32, i32* %12
%24 = zext i32 %23 to i64
%25 = load i32, i32* %11
%26 = zext i32 %25 to i64
%27 = load i32, i32* %12
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [50 x i8], [50 x i8]* %16, i64 0, i64 0
%30 = load i8**, i8*** %9
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @strcpy(i8* %29, i8* %32)
%34 = getelementptr inbounds [50 x i8], [50 x i8]* %16, i64 0, i64 0
%35 = call i8* @strchr(i8* %34, i8 signext 46)
store i8* %35, i8** %17
%36 = load i8*, i8** %17
store i8 0, i8* %36
%37 = getelementptr inbounds [50 x i8], [50 x i8]* %16, i64 0, i64 0
%38 = call i32 @strcat(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%39 = getelementptr inbounds [50 x i8], [50 x i8]* %16, i64 0, i64 0
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %15
%41 = load i32*, i32** %15
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %7
%44 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %7
%47 = load i32*, i32** %8
%48 = call i32 @rewind(i32* %47)
br label %49
49: ; preds = %54, %46
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %18, i64 0, i64 0
%51 = load i32*, i32** %8
%52 = call i32* @fgets(i8* %50, i32 1024, i32* %51)
%53 = icmp ne i32* %52, null
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = load i32*, i32** %15
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %18, i64 0, i64 0
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %56)
br label %49
58: ; preds = %49
store i32 0, i32* %19
%59 = load i32, i32* %10
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %113
61: ; preds = %58
store i32 0, i32* %19
br label %62
62: ; preds = %109, %61
%63 = load i32, i32* %19
%64 = load i32, i32* %11
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %112
66: ; preds = %62
%67 = load i32*, i32** %15
%68 = call i32 (i32*, i8*, ...) @fprintf(i32* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %20
br label %69
69: ; preds = %103, %66
%70 = load i32, i32* %20
%71 = load i32, i32* %12
%72 = icmp slt i32 %70, %71
br i1 %72, label %73, label %106
73: ; preds = %69
%74 = load i32*, i32** %15
%75 = call i32 (i32*, i8*, ...) @fprintf(i32* %74, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%76 = load i32*, i32** %13
%77 = load i32, i32* %19
%78 = sext i32 %77 to i64
%79 = mul nsw i64 %78, %24
%80 = getelementptr inbounds i32, i32* %76, i64 %79
%81 = load i32, i32* %20
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %80, i64 %82
%84 = load i32, i32* %83
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %99
86: ; preds = %73
%87 = load i32*, i32** %15
%88 = load i32*, i32** %14
%89 = load i32, i32* %19
%90 = sext i32 %89 to i64
%91 = mul nsw i64 %90, %28
%92 = getelementptr inbounds i32, i32* %88, i64 %91
%93 = load i32, i32* %20
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds i32, i32* %92, i64 %94
%96 = load i32, i32* %95
%97 = add nsw i32 %96, 48
%98 = call i32 (i32*, i8*, ...) @fprintf(i32* %87, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %97)
br label %102
99: ; preds = %73
%100 = load i32*, i32** %15
%101 = call i32 (i32*, i8*, ...) @fprintf(i32* %100, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %102
102: ; preds = %99, %86
br label %103
103: ; preds = %102
%104 = load i32, i32* %20
%105 = add nsw i32 %104, 1
store i32 %105, i32* %20
br label %69
106: ; preds = %69
%107 = load i32*, i32** %15
%108 = call i32 (i32*, i8*, ...) @fprintf(i32* %107, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %109
109: ; preds = %106
%110 = load i32, i32* %19
%111 = add nsw i32 %110, 1
store i32 %111, i32* %19
br label %62
112: ; preds = %62
br label %113
113: ; preds = %112, %58
%114 = load i32*, i32** %15
%115 = call i32 (i32*, i8*, ...) @fprintf(i32* %114, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%116 = load i32*, i32** %15
%117 = call i32 @fclose(i32* %116)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c".sol\00"
@.str.1 = [2 x i8] c"w\00"
@str = [41 x i8] c"error: output file could not be created.\00"
define void @escreve_solucao(i32* nocapture %0, i8** nocapture %1, i32 %2, i32 %3, i32 %4, i32* nocapture %5, i32* nocapture %6) {
%8 = alloca [50 x i8]
%9 = alloca [1024 x i8]
%10 = zext i32 %4 to i64
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @strcpy(i8* %11, i8* %14)
%16 = call i8* @strchr(i8* %11, i8 signext 46)
store i8 0, i8* %16
%17 = call i32 @strcat(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %7
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %7
%24 = call i32 @rewind(i32* %0)
%25 = call i32* @fgets(i8* %12, i32 1024, i32* %0)
%26 = icmp eq i32* %25, null
br i1 %26, label %31, label %27
27: ; preds = %23, %27
%28 = call i32 @fputs(i8* %12, i32* %18)
%29 = call i32* @fgets(i8* %12, i32 1024, i32* %0)
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %27
31: ; preds = %27, %23
%32 = icmp eq i32 %2, 0
%33 = icmp sgt i32 %3, 0
%34 = and i1 %32, %33
br i1 %34, label %35, label %64
35: ; preds = %31
%36 = icmp sgt i32 %4, 1
%37 = zext i32 %3 to i64
br label %38
38: ; preds = %35, %60
%39 = phi i64 [ 0, %35 ], [ %62, %60 ]
%40 = call i32 @fputc(i32 45, i32* %18)
br i1 %36, label %41, label %60
41: ; preds = %38
%42 = mul nuw nsw i64 %39, %10
%43 = getelementptr inbounds i32, i32* %5, i64 %42
%44 = getelementptr inbounds i32, i32* %6, i64 %42
br label %45
45: ; preds = %41, %55
%46 = phi i64 [ 1, %41 ], [ %58, %55 ]
%47 = call i32 @fputc(i32 58, i32* %18)
%48 = getelementptr inbounds i32, i32* %43, i64 %46
%49 = load i32, i32* %48
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %45
%52 = getelementptr inbounds i32, i32* %44, i64 %46
%53 = load i32, i32* %52
%54 = add nsw i32 %53, 48
br label %55
55: ; preds = %45, %51
%56 = phi i32 [ %54, %51 ], [ 45, %45 ]
%57 = call i32 @fputc(i32 %56, i32* %18)
%58 = add nuw nsw i64 %46, 1
%59 = icmp eq i64 %58, %10
br i1 %59, label %60, label %45
60: ; preds = %55, %38
%61 = call i32 @fputc(i32 10, i32* %18)
%62 = add nuw nsw i64 %39, 1
%63 = icmp eq i64 %62, %37
br i1 %63, label %64, label %38
64: ; preds = %60, %31
%65 = call i32 @fputc(i32 10, i32* %18)
%66 = call i32 @fclose(i32* %18)
ret void
}