#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*/ uint32_t;
typedef scalar_t__ uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ *malloc(int);
int ptg_file_lines(char const *);
void ptg_read_port_file(const char *filename, int *lncnt, uint16_t **ports) {
  FILE *fp;
  char lnbuf[128];
  int i = 0;
  *lncnt = ptg_file_lines(filename);
  if (*lncnt <= 0) {
    *ports = NULL;
    return;
  }
  *ports = malloc((*lncnt) * sizeof(uint32_t));
  fp = fopen(filename, "r");
  if (fp == NULL) {
    *lncnt = 0;
    *ports = NULL;
    return;
  }
  while (fgets(lnbuf, 127, fp) != NULL) {
    (*ports)[i] = (uint16_t)atoi(lnbuf);
    i++;
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<ptg_read_port_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0xa4(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <ptg_file_lines@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jg     11af <ptg_read_port_file+0x4f>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
jmp    1266 <ptg_read_port_file+0x106>
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1204 <ptg_read_port_file+0xa4>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
jmp    1266 <ptg_read_port_file+0x106>
jmp    1209 <ptg_read_port_file+0xa9>
lea    -0xa0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x7f,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     125d <ptg_read_port_file+0xfd>
lea    -0xa0(%rbp),%rdi
call   1080 <atoi@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0xa4(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    1209 <ptg_read_port_file+0xa9>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptg_read_port_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <ptg_file_lines@plt>
mov    %eax,(%r15)
test   %eax,%eax
jle    1209 <ptg_read_port_file+0xa9>
shl    $0x2,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1202 <ptg_read_port_file+0xa2>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x7f,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11f8 <ptg_read_port_file+0x98>
xor    %ebx,%ebx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1080 <atoi@plt>
mov    (%r14),%rcx
mov    %rax,(%rcx,%rbx,1)
mov    %r15,%rdi
mov    $0x7f,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
add    $0x8,%rbx
test   %rax,%rax
jne    11d0 <ptg_read_port_file+0x70>
mov    %r12,%rdi
call   1040 <fclose@plt>
jmp    1210 <ptg_read_port_file+0xb0>
movl   $0x0,(%r15)
movq   $0x0,(%r14)
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define void @ptg_read_port_file(i8* %0, i32* %1, i64** %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64**
%7 = alloca i32*
%8 = alloca [128 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64** %2, i64*** %6
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = call i32 @ptg_file_lines(i8* %10)
%12 = load i32*, i32** %5
store i32 %11, i32* %12
%13 = load i32*, i32** %5
%14 = load i32, i32* %13
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i64**, i64*** %6
store i64* null, i64** %17
br label %52
18: ; preds = %3
%19 = load i32*, i32** %5
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 4
%23 = trunc i64 %22 to i32
%24 = call i64* @malloc(i32 %23)
%25 = load i64**, i64*** %6
store i64* %24, i64** %25
%26 = load i8*, i8** %4
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %7
%28 = load i32*, i32** %7
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = load i32*, i32** %5
store i32 0, i32* %31
%32 = load i64**, i64*** %6
store i64* null, i64** %32
br label %52
33: ; preds = %18
br label %34
34: ; preds = %39, %33
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%36 = load i32*, i32** %7
%37 = call i32* @fgets(i8* %35, i32 127, i32* %36)
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %49
39: ; preds = %34
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%41 = call i64 @atoi(i8* %40)
%42 = load i64**, i64*** %6
%43 = load i64*, i64** %42
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i64, i64* %43, i64 %45
store i64 %41, i64* %46
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 1
store i32 %48, i32* %9
br label %34
49: ; preds = %34
%50 = load i32*, i32** %7
%51 = call i32 @fclose(i32* %50)
br label %52
52: ; preds = %49, %30, %16
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define void @ptg_read_port_file(i8* %0, i32* nocapture writeonly %1, i64** nocapture %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @ptg_file_lines(i8* %0)
store i32 %6, i32* %1
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %9
8: ; preds = %3
store i64* null, i64** %2
br label %28
9: ; preds = %3
%10 = shl i32 %6, 2
%11 = tail call i64* @malloc(i32 %10)
store i64* %11, i64** %2
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = call i32* @fgets(i8* %5, i32 127, i32* %12)
%16 = icmp eq i32* %15, null
br i1 %16, label %26, label %18
17: ; preds = %9
store i32 0, i32* %1
store i64* null, i64** %2
br label %28
18: ; preds = %14, %18
%19 = phi i64 [ %23, %18 ], [ 0, %14 ]
%20 = call i64 @atoi(i8* %5)
%21 = load i64*, i64** %2
%22 = getelementptr inbounds i64, i64* %21, i64 %19
store i64 %20, i64* %22
%23 = add nuw i64 %19, 1
%24 = call i32* @fgets(i8* %5, i32 127, i32* %12)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %18
26: ; preds = %18, %14
%27 = call i32 @fclose(i32* %12)
br label %28
28: ; preds = %26, %17, %8
ret void
}