#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
void *malloc(int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int read_file_list(char *filename, char ***file_list) {
  FILE *fp;
  char buf[256];
  int n_files = 0;
  int res = 0;
  fp = fopen(filename, "r");
  if (!fp) {
    fprintf(stderr, "Couldn't open '%s' for reading\n", filename);
    return -1;
  }
  while (fgets(buf, 256, fp)) {
    n_files++;
  }
  *file_list = malloc(sizeof(**file_list) * n_files);
  if (!*file_list) {
    fprintf(stderr, "Couldn't allocate space for input files\n");
    res = -1;
    goto done;
  }
  fseek(fp, 0, SEEK_SET);
  n_files = 0;
  while (fgets(buf, 256, fp)) {
    char *infile = malloc(strlen(buf));
    ;
    (*file_list)[n_files] = infile;
    strncpy(infile, buf, strlen(buf) - 1);
    infile[strlen(buf) - 1] = '\0';
    n_files++;
  }
done:
  fclose(fp);
  return res < 0 ? res : n_files;
}


x86 assembly (clang, -O0):
<read_file_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x124(%rbp)
movl   $0x0,-0x128(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ed <read_file_list+0x6d>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <read_file_list+0x227>
jmp    11f2 <read_file_list+0x72>
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1225 <read_file_list+0xa5>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    11f2 <read_file_list+0x72>
movslq -0x124(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1275 <read_file_list+0xf5>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x128(%rbp)
ff ff ff
jmp    136b <read_file_list+0x1eb>
mov    -0x20(%rbp),%rdi
mov    0x2d58(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
movl   $0x0,-0x124(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1366 <read_file_list+0x1e6>
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x124(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x130(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x140(%rbp)
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x148(%rbp),%rdi
mov    -0x140(%rbp),%rsi
mov    %eax,%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x130(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x138(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    1293 <read_file_list+0x113>
jmp    136b <read_file_list+0x1eb>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x128(%rbp)
jge    1392 <read_file_list+0x212>
mov    -0x128(%rbp),%eax
mov    %eax,-0x14c(%rbp)
jmp    139e <read_file_list+0x21e>
mov    -0x124(%rbp),%eax
mov    %eax,-0x14c(%rbp)
mov    -0x14c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_list>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe63(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1279 <read_file_list+0xf9>
mov    %rax,%r15
mov    $0xfffffff8,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
add    $0x8,%ebx
test   %rax,%rax
jne    11c0 <read_file_list+0x40>
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     129b <read_file_list+0x11b>
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
xor    %r13d,%r13d
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12b8 <read_file_list+0x138>
xor    %r13d,%r13d
mov    %rsp,%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    (%r14),%rax
mov    %rbx,(%rax,%r13,8)
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    -0x1(%rax),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strncpy@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
add    $0x1,%r13
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1220 <read_file_list+0xa0>
jmp    12b8 <read_file_list+0x138>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%r13d
jmp    12c0 <read_file_list+0x140>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%r13d
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r13d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Couldn't open '%s' for reading\0A\00"
@.str.2 = [41 x i8] c"Couldn't allocate space for input files\0A\00"
@SEEK_SET = global i32 0
define i32 @read_file_list(i8* %0, i8*** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8***
%6 = alloca i32*
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i8*** %1, i8**** %5
store i32 0, i32* %8
store i32 0, i32* %9
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %87
19: ; preds = %2
br label %20
20: ; preds = %25, %19
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %6
%23 = call i64 @fgets(i8* %21, i32 256, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
store i32 %27, i32* %8
br label %20
28: ; preds = %20
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = mul i64 8, %30
%32 = trunc i64 %31 to i32
%33 = call i8* @malloc(i32 %32)
%34 = bitcast i8* %33 to i8**
%35 = load i8***, i8**** %5
store i8** %34, i8*** %35
%36 = load i8***, i8**** %5
%37 = load i8**, i8*** %36
%38 = icmp ne i8** %37, null
br i1 %38, label %42, label %39
39: ; preds = %28
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %9
br label %76
42: ; preds = %28
%43 = load i32*, i32** %6
%44 = load i32, i32* @SEEK_SET
%45 = call i32 @fseek(i32* %43, i32 0, i32 %44)
store i32 0, i32* %8
br label %46
46: ; preds = %51, %42
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%48 = load i32*, i32** %6
%49 = call i64 @fgets(i8* %47, i32 256, i32* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %75
51: ; preds = %46
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%53 = call i32 @strlen(i8* %52)
%54 = call i8* @malloc(i32 %53)
store i8* %54, i8** %10
%55 = load i8*, i8** %10
%56 = load i8***, i8**** %5
%57 = load i8**, i8*** %56
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8*, i8** %57, i64 %59
store i8* %55, i8** %60
%61 = load i8*, i8** %10
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = sub nsw i32 %64, 1
%66 = call i32 @strncpy(i8* %61, i8* %62, i32 %65)
%67 = load i8*, i8** %10
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%69 = call i32 @strlen(i8* %68)
%70 = sub nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %67, i64 %71
store i8 0, i8* %72
%73 = load i32, i32* %8
%74 = add nsw i32 %73, 1
store i32 %74, i32* %8
br label %46
75: ; preds = %46
br label %76
76: ; preds = %75, %39
%77 = load i32*, i32** %6
%78 = call i32 @fclose(i32* %77)
%79 = load i32, i32* %9
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %76
%82 = load i32, i32* %9
br label %85
83: ; preds = %76
%84 = load i32, i32* %8
br label %85
85: ; preds = %83, %81
%86 = phi i32 [ %82, %81 ], [ %84, %83 ]
store i32 %86, i32* %3
br label %87
87: ; preds = %85, %15
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Couldn't open '%s' for reading\0A\00"
@.str.2 = [41 x i8] c"Couldn't allocate space for input files\0A\00"
@SEEK_SET = global i32 0
define i32 @read_file_list(i8* %0, i8*** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %49
10: ; preds = %2, %10
%11 = phi i32 [ %14, %10 ], [ 0, %2 ]
%12 = call i64 @fgets(i8* %4, i32 256, i32* %5)
%13 = icmp eq i64 %12, 0
%14 = add nuw nsw i32 %11, 1
br i1 %13, label %15, label %10
15: ; preds = %10
%16 = shl i32 %11, 3
%17 = call i8* @malloc(i32 %16)
%18 = bitcast i8*** %1 to i8**
store i8* %17, i8** %18
%19 = icmp eq i8* %17, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %46
23: ; preds = %15
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %5, i32 0, i32 %24)
%26 = call i64 @fgets(i8* %4, i32 256, i32* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %46, label %28
28: ; preds = %23, %28
%29 = phi i64 [ %41, %28 ], [ 0, %23 ]
%30 = call i32 @strlen(i8* %4)
%31 = call i8* @malloc(i32 %30)
%32 = load i8**, i8*** %1
%33 = getelementptr inbounds i8*, i8** %32, i64 %29
store i8* %31, i8** %33
%34 = call i32 @strlen(i8* %4)
%35 = add nsw i32 %34, -1
%36 = call i32 @strncpy(i8* %31, i8* %4, i32 %35)
%37 = call i32 @strlen(i8* %4)
%38 = add nsw i32 %37, -1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %31, i64 %39
store i8 0, i8* %40
%41 = add nuw i64 %29, 1
%42 = call i64 @fgets(i8* %4, i32 256, i32* %5)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %28
44: ; preds = %28
%45 = trunc i64 %41 to i32
br label %46
46: ; preds = %44, %23, %20
%47 = phi i32 [ -1, %20 ], [ 0, %23 ], [ %45, %44 ]
%48 = call i32 @fclose(i32* %5)
br label %49
49: ; preds = %46, %7
%50 = phi i32 [ %47, %46 ], [ -1, %7 ]
ret i32 %50
}