#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 *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strrchr(char *, char);
void criar_ficheiro(char nome_entrada[20]) {
  FILE *ficheiro_entrada;
  FILE *ficheiro_saida;
  char linha[1024];
  char nome_saida[20];
  char *temp;
  int linhas_em_branco = 0;
  strcpy(nome_saida, nome_entrada);
  temp = strrchr(nome_saida, '.');
  strcpy(temp, ".cfg");
  ficheiro_entrada = fopen(nome_entrada, "r");
  if (ficheiro_entrada == NULL) {
    printf("error: input file could not be opened.\n");
    exit(-1);
  }
  ficheiro_saida = fopen(nome_saida, "w");
  if (ficheiro_saida == NULL) {
    printf("error: output file could not be created.\n");
    exit(-1);
  }
  while (fgets(linha, 1024, ficheiro_entrada) != NULL && linhas_em_branco < 2) {
    fprintf(ficheiro_saida, "%s", linha);
    if (linha[0] == '\n') {
      linhas_em_branco++;
    }
  }
  fclose(ficheiro_entrada);
  fclose(ficheiro_saida);
  return;
}


x86 assembly (clang, -O0):
<criar_ficheiro>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x44c(%rbp)
lea    -0x440(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x440(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x448(%rbp)
mov    -0x448(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1209 <criar_ficheiro+0x89>
lea    0xe0d(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x440(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1241 <criar_ficheiro+0xc1>
lea    0xdff(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    1246 <criar_ficheiro+0xc6>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x44d(%rbp)
je     1280 <criar_ficheiro+0x100>
cmpl   $0x2,-0x44c(%rbp)
setl   %al
mov    %al,-0x44d(%rbp)
mov    -0x44d(%rbp),%al
test   $0x1,%al
jne    1293 <criar_ficheiro+0x113>
jmp    12ce <criar_ficheiro+0x14e>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xdb6(%rip),%rsi
call   1080 <fprintf@plt>
movsbl -0x420(%rbp),%eax
cmp    $0xa,%eax
jne    12c9 <criar_ficheiro+0x149>
mov    -0x44c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44c(%rbp)
jmp    1246 <criar_ficheiro+0xc6>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<criar_ficheiro>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rdi,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
lea    0xe4a(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
lea    0xe40(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1277 <criar_ficheiro+0xf7>
mov    %rax,%r14
lea    0xe27(%rip),%rsi
mov    %rsp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1280 <criar_ficheiro+0x100>
mov    %rax,%r15
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1255 <criar_ficheiro+0xd5>
xor    %ebp,%ebp
lea    0x20(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <fputs@plt>
xor    %ebx,%ebx
cmpb   $0xa,0x20(%rsp)
sete   %r13b
mov    %r12,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1255 <criar_ficheiro+0xd5>
mov    %r13b,%bl
add    %ebx,%ebp
cmp    $0x2,%ebp
jb     1220 <criar_ficheiro+0xa0>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
jmp    1287 <criar_ficheiro+0x107>
lea    0xd82(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c".cfg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [40 x i8] c"error: input file could not be opened.\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [42 x i8] c"error: output file could not be created.\0A\00"
@.str.5 = [3 x i8] c"%s\00"
define void @criar_ficheiro(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca [20 x i8]
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %8
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %2
%11 = call i32 @strcpy(i8* %9, i8* %10)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%13 = call i8* @strrchr(i8* %12, i8 signext 46)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = load i8*, i8** %2
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %3
%18 = load i32*, i32** %3
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %1
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %25, i32** %4
%26 = load i32*, i32** %4
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %23
br label %32
32: ; preds = %53, %31
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%34 = load i32*, i32** %3
%35 = call i32* @fgets(i8* %33, i32 1024, i32* %34)
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 2
br label %40
40: ; preds = %37, %32
%41 = phi i1 [ false, %32 ], [ %39, %37 ]
br i1 %41, label %42, label %54
42: ; preds = %40
%43 = load i32*, i32** %4
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%45 = call i32 @fprintf(i32* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %44)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 10
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %53
53: ; preds = %50, %42
br label %32
54: ; preds = %40
%55 = load i32*, i32** %3
%56 = call i32 @fclose(i32* %55)
%57 = load i32*, i32** %4
%58 = call i32 @fclose(i32* %57)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c".cfg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@str = [41 x i8] c"error: output file could not be created.\00"
@str.6 = [39 x i8] c"error: input file could not be opened.\00"
define void @criar_ficheiro(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* %0)
%7 = call i8* @strrchr(i8* %5, i8 signext 46)
%8 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.6, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %1
%15 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = call i32* @fgets(i8* %4, i32 1024, i32* %9)
%19 = icmp eq i32* %18, null
br i1 %19, label %34, label %23
20: ; preds = %14
%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 = %17, %23
%24 = phi i32 [ %29, %23 ], [ 0, %17 ]
%25 = call i32 @fputs(i8* %4, i32* %15)
%26 = load i8, i8* %4
%27 = icmp eq i8 %26, 10
%28 = zext i1 %27 to i32
%29 = add nuw nsw i32 %24, %28
%30 = call i32* @fgets(i8* %4, i32 1024, i32* %9)
%31 = icmp ne i32* %30, null
%32 = icmp ult i32 %29, 2
%33 = select i1 %31, i1 %32, i1 false
br i1 %33, label %23, label %34
34: ; preds = %23, %17
%35 = call i32 @fclose(i32* %9)
%36 = call i32 @fclose(i32* %15)
ret void
}