#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*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
char *malloc(int);
char **realloc(char **, int);
char **carregaPalavras(char *ficheiro, int *tamanho) {
  FILE *file = fopen(ficheiro, "r");
  *tamanho = 0;
  char **wordArr = 0;
  for (;;) {
    char *word = malloc(sizeof(*word) * 50);
    if (fscanf(file, " %s", word) < 1) {
      free(word);
      break;
    }
    ++*tamanho;
    wordArr = realloc(wordArr, sizeof(*wordArr) * (*tamanho));
    wordArr[*tamanho - 1] = word;
  }
  return wordArr;
}


x86 assembly (clang, -O0):
<carregaPalavras>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x20(%rbp)
mov    $0x32,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe5f(%rip),%rsi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jge    11bf <carregaPalavras+0x6f>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1201 <carregaPalavras+0xb1>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1186 <carregaPalavras+0x36>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<carregaPalavras>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
movl   $0x0,(%r15)
mov    $0x32,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
lea    0xe7c(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
call   1040 <fscanf@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    11e2 <carregaPalavras+0x92>
lea    0xe64(%rip),%r13
xchg   %ax,%ax
mov    (%r15),%eax
lea    0x1(%rax),%ecx
mov    %ecx,(%r15)
lea    0x8(,%rax,8),%esi
mov    %rbx,%rdi
call   1060 <realloc@plt>
mov    %rax,%rbx
movslq (%r15),%rax
mov    %r12,-0x8(%rbx,%rax,8)
mov    $0x32,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
call   1040 <fscanf@plt>
test   %eax,%eax
jg     11a0 <carregaPalavras+0x50>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [4 x i8] c" %s\00"
define i8** @carregaPalavras(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %4
store i32 0, i32* %10
store i8** null, i8*** %6
br label %11
11: ; preds = %20, %2
%12 = call i8* @malloc(i32 50)
store i8* %12, i8** %7
%13 = load i32*, i32** %5
%14 = load i8*, i8** %7
%15 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i8*, i8** %7
%19 = call i32 @free(i8* %18)
br label %38
20: ; preds = %11
%21 = load i32*, i32** %4
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 1
store i32 %23, i32* %21
%24 = load i8**, i8*** %6
%25 = load i32*, i32** %4
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
%28 = mul i64 8, %27
%29 = trunc i64 %28 to i32
%30 = call i8** @realloc(i8** %24, i32 %29)
store i8** %30, i8*** %6
%31 = load i8*, i8** %7
%32 = load i8**, i8*** %6
%33 = load i32*, i32** %4
%34 = load i32, i32* %33
%35 = sub nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %32, i64 %36
store i8* %31, i8** %37
br label %11
38: ; preds = %17
%39 = load i8**, i8*** %6
ret i8** %39
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [4 x i8] c" %s\00"
define i8** @carregaPalavras(i8* nocapture %0, i32* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
%4 = tail call dereferenceable_or_null(50) i8* @malloc(i32 50)
%5 = tail call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%6 = icmp slt i32 %5, 1
br i1 %6, label %21, label %7
7: ; preds = %2, %7
%8 = phi i8* [ %18, %7 ], [ %4, %2 ]
%9 = phi i8** [ %13, %7 ], [ null, %2 ]
%10 = load i32, i32* %1
%11 = add nsw i32 %10, 1
store i32 %11, i32* %1
%12 = shl i32 %11, 3
%13 = tail call i8** @realloc(i8** %9, i32 %12)
%14 = load i32, i32* %1
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8*, i8** %13, i64 %16
store i8* %8, i8** %17
%18 = tail call dereferenceable_or_null(50) i8* @malloc(i32 50)
%19 = tail call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %7
21: ; preds = %7, %2
%22 = phi i8** [ null, %2 ], [ %13, %7 ]
%23 = phi i8* [ %4, %2 ], [ %18, %7 ]
%24 = tail call i32 @free(i8* %23)
ret i8** %22
}