#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
void *malloc(int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char **parse_targets_file(FILE *f_hosts) {
  char **hostnames;
  char buffer[1024];
  int i = 0;
  hostnames = malloc(sizeof(char *) * (1024 + 1));
  while (fgets(buffer, sizeof buffer, f_hosts) != NULL) {
    if (i >= 1024) {
      fprintf(stderr,
              "Warning: Stopped reading hostnames from file after %d entries\n",
              1024);
      break;
    }
    if (buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == ' ') {
      continue;
    }
    hostnames[i] = malloc(strlen(buffer) + 1);
    strncpy(hostnames[i], buffer, strlen(buffer) + 1);
    hostnames[i][strlen(buffer) - 1] = '\0';
    i++;
  }
  hostnames[i] = NULL;
  return hostnames;
}


x86 assembly (clang, -O0):
<parse_targets_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x414(%rbp)
mov    $0x2008,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12b3 <parse_targets_file+0x163>
cmpl   $0x400,-0x414(%rbp)
jl     11c5 <parse_targets_file+0x75>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    $0x400,%edx
call   1060 <fprintf@plt>
jmp    12b3 <parse_targets_file+0x163>
movsbl -0x410(%rbp),%eax
cmp    $0x23,%eax
je     11f5 <parse_targets_file+0xa5>
movsbl -0x410(%rbp),%eax
cmp    $0xa,%eax
je     11f5 <parse_targets_file+0xa5>
movsbl -0x410(%rbp),%eax
cmp    $0x20,%eax
jne    11fa <parse_targets_file+0xaa>
jmp    1177 <parse_targets_file+0x27>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x414(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x414(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x430(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x430(%rbp),%rdi
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x414(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x420(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    1177 <parse_targets_file+0x27>
mov    -0x10(%rbp),%rax
movslq -0x414(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_targets_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
mov    $0x2008,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     125c <parse_targets_file+0x10c>
mov    %rsp,%r15
movabs $0x900000400,%rbp
cmp    $0x400,%r13
je     123c <parse_targets_file+0xec>
nopw   0x0(%rax,%rax,1)
movzbl (%rsp),%eax
cmp    $0x23,%rax
ja     11e0 <parse_targets_file+0x90>
bt     %rax,%rbp
jae    11e0 <parse_targets_file+0x90>
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <parse_targets_file+0x60>
jmp    125c <parse_targets_file+0x10c>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,(%r14,%r13,8)
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
add    $0x1,%r13
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    119a <parse_targets_file+0x4a>
jmp    125c <parse_targets_file+0x10c>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    $0x400,%edx
call   1060 <fprintf@plt>
mov    $0x400,%r13d
movslq %r13d,%rax
movq   $0x0,(%r14,%rax,8)
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [63 x i8] c"Warning: Stopped reading hostnames from file after %d entries\0A\00"
define i8** @parse_targets_file(i32* %0) {
%2 = alloca i32*
%3 = alloca i8**
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %5
%6 = call i8* @malloc(i32 8200)
%7 = bitcast i8* %6 to i8**
store i8** %7, i8*** %3
br label %8
8: ; preds = %35, %34, %1
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = load i32*, i32** %2
%11 = call i32* @fgets(i8* %9, i32 1024, i32* %10)
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %66
13: ; preds = %8
%14 = load i32, i32* %5
%15 = icmp sge i32 %14, 1024
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 1024)
br label %66
19: ; preds = %13
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 35
br i1 %23, label %34, label %24
24: ; preds = %19
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %34, label %29
29: ; preds = %24
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 32
br i1 %33, label %34, label %35
34: ; preds = %29, %24, %19
br label %8
35: ; preds = %29
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = add nsw i32 %37, 1
%39 = call i8* @malloc(i32 %38)
%40 = load i8**, i8*** %3
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %40, i64 %42
store i8* %39, i8** %43
%44 = load i8**, i8*** %3
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
%48 = load i8*, i8** %47
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = add nsw i32 %51, 1
%53 = call i32 @strncpy(i8* %48, i8* %49, i32 %52)
%54 = load i8**, i8*** %3
%55 = load i32, i32* %5
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8*, i8** %54, i64 %56
%58 = load i8*, i8** %57
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%60 = call i32 @strlen(i8* %59)
%61 = sub nsw i32 %60, 1
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %58, i64 %62
store i8 0, i8* %63
%64 = load i32, i32* %5
%65 = add nsw i32 %64, 1
store i32 %65, i32* %5
br label %8
66: ; preds = %16, %8
%67 = load i8**, i8*** %3
%68 = load i32, i32* %5
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8*, i8** %67, i64 %69
store i8* null, i8** %70
%71 = load i8**, i8*** %3
ret i8** %71
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [63 x i8] c"Warning: Stopped reading hostnames from file after %d entries\0A\00"
define noalias i8** @parse_targets_file(i32* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call dereferenceable_or_null(8200) i8* @malloc(i32 8200)
%5 = bitcast i8* %4 to i8**
%6 = call i32* @fgets(i8* %3, i32 1024, i32* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %34, label %8
8: ; preds = %1, %19
%9 = phi i64 [ %31, %19 ], [ 0, %1 ]
%10 = icmp eq i64 %9, 1024
br i1 %10, label %13, label %11
11: ; preds = %8, %16
%12 = load i8, i8* %3
switch i8 %12, label %19 [
i8 35, label %16
i8 32, label %16
i8 10, label %16
]
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 1024)
br label %34
16: ; preds = %11, %11, %11
%17 = call i32* @fgets(i8* %3, i32 1024, i32* %0)
%18 = icmp eq i32* %17, null
br i1 %18, label %34, label %11
19: ; preds = %11
%20 = call i32 @strlen(i8* %3)
%21 = add nsw i32 %20, 1
%22 = call i8* @malloc(i32 %21)
%23 = getelementptr inbounds i8*, i8** %5, i64 %9
store i8* %22, i8** %23
%24 = call i32 @strlen(i8* %3)
%25 = add nsw i32 %24, 1
%26 = call i32 @strncpy(i8* %22, i8* %3, i32 %25)
%27 = call i32 @strlen(i8* %3)
%28 = add nsw i32 %27, -1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %22, i64 %29
store i8 0, i8* %30
%31 = add nuw nsw i64 %9, 1
%32 = call i32* @fgets(i8* %3, i32 1024, i32* %0)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %8
34: ; preds = %19, %16, %1, %13
%35 = phi i64 [ 1024, %13 ], [ 0, %1 ], [ %9, %16 ], [ %31, %19 ]
%36 = shl i64 %35, 32
%37 = ashr exact i64 %36, 32
%38 = getelementptr inbounds i8*, i8** %5, i64 %37
store i8* null, i8** %38
ret i8** %5
}