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

/* Variables and functions */
int /*<<< orphan*/ LOG(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ parseLine(char *, unsigned short *, unsigned long *);
scalar_t__ readline(int /*<<< orphan*/ *, char *, int);
int activeSSHSessions(unsigned int *count, unsigned short sshPort) {
  unsigned int cnt = 0;
  FILE *fin;
  char line[2048];
  unsigned long src;
  unsigned short port;
  if (count == NULL) {
    LOG("parameter is NULL\n");
    return -1;
  }
  fin = fopen("/proc/net/tcp", "r");
  if (fin == NULL) {
    LOG("Failed to open proc file\n");
    return -1;
  }
  if (readline(fin, line, sizeof(line))) {
    fclose(fin);
    return -1;
  }
  while (readline(fin, line, sizeof(line)) == 0) {
    if (parseLine(line, &port, &src)) {
      fclose(fin);
      return -1;
    }
    if (port == sshPort && src != 0) {
      (*count)++;
    }
  }
  fclose(fin);
  return 0;
}


x86 assembly (clang, -O0):
<activeSSHSessions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1190 <activeSSHSessions+0x40>
lea    0xe81(%rip),%rdi
call   1050 <LOG@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <activeSSHSessions+0x14c>
lea    0xe7c(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ca <activeSSHSessions+0x7a>
lea    0xe6a(%rip),%rdi
call   1050 <LOG@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <activeSSHSessions+0x14c>
mov    -0x20(%rbp),%rdi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
call   1040 <readline@plt>
cmp    $0x0,%rax
je     11fe <activeSSHSessions+0xae>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <activeSSHSessions+0x14c>
jmp    1203 <activeSSHSessions+0xb3>
mov    -0x20(%rbp),%rdi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
call   1040 <readline@plt>
cmp    $0x0,%rax
jne    128c <activeSSHSessions+0x13c>
lea    -0x820(%rbp),%rdi
lea    -0x82a(%rbp),%rsi
lea    -0x828(%rbp),%rdx
call   1070 <parseLine@plt>
cmp    $0x0,%rax
je     125b <activeSSHSessions+0x10b>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <activeSSHSessions+0x14c>
movzwl -0x82a(%rbp),%eax
movzwl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jne    1287 <activeSSHSessions+0x137>
cmpq   $0x0,-0x828(%rbp)
je     1287 <activeSSHSessions+0x137>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1203 <activeSSHSessions+0xb3>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<activeSSHSessions>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
test   %rdi,%rdi
je     11a8 <activeSSHSessions+0x58>
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xea0(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b1 <activeSSHSessions+0x61>
mov    %rax,%rbp
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
mov    $0x800,%edx
call   1040 <readline@plt>
test   %rax,%rax
je     11d4 <activeSSHSessions+0x84>
mov    %rbp,%rdi
call   1030 <fclose@plt>
jmp    11bd <activeSSHSessions+0x6d>
lea    0xe51(%rip),%rdi
jmp    11b8 <activeSSHSessions+0x68>
lea    0xe6b(%rip),%rdi
call   1050 <LOG@plt>
mov    $0xffffffff,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x800,%edx
call   1040 <readline@plt>
test   %rax,%rax
je     11f7 <activeSSHSessions+0xa7>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11c2 <activeSSHSessions+0x72>
lea    0x10(%rsp),%r15
lea    0x6(%rsp),%r12
lea    0x8(%rsp),%r13
jmp    1225 <activeSSHSessions+0xd5>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x800,%edx
call   1040 <readline@plt>
test   %rax,%rax
jne    11eb <activeSSHSessions+0x9b>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <parseLine@plt>
test   %rax,%rax
jne    119e <activeSSHSessions+0x4e>
cmp    %r14w,0x6(%rsp)
jne    1210 <activeSSHSessions+0xc0>
cmpq   $0x0,0x8(%rsp)
je     1210 <activeSSHSessions+0xc0>
addl   $0x1,(%rbx)
jmp    1210 <activeSSHSessions+0xc0>

LLVM IR (-O0):
@.str = [19 x i8] c"parameter is NULL\0A\00"
@.str.1 = [14 x i8] c"/proc/net/tcp\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [26 x i8] c"Failed to open proc file\0A\00"
define i32 @activeSSHSessions(i32* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i16
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [2048 x i8]
%9 = alloca i64
%10 = alloca i16
store i32* %0, i32** %4
store i16 %1, i16* %5
store i32 0, i32* %6
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @LOG(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
15: ; preds = %2
%16 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @LOG(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
21: ; preds = %15
%22 = load i32*, i32** %7
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%24 = call i64 @readline(i32* %22, i8* %23, i32 2048)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32*, i32** %7
%28 = call i32 @fclose(i32* %27)
store i32 -1, i32* %3
br label %59
29: ; preds = %21
br label %30
30: ; preds = %55, %29
%31 = load i32*, i32** %7
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%33 = call i64 @readline(i32* %31, i8* %32, i32 2048)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %56
35: ; preds = %30
%36 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%37 = call i64 @parseLine(i8* %36, i16* %10, i64* %9)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32*, i32** %7
%41 = call i32 @fclose(i32* %40)
store i32 -1, i32* %3
br label %59
42: ; preds = %35
%43 = load i16, i16* %10
%44 = zext i16 %43 to i32
%45 = load i16, i16* %5
%46 = zext i16 %45 to i32
%47 = icmp eq i32 %44, %46
br i1 %47, label %48, label %55
48: ; preds = %42
%49 = load i64, i64* %9
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32*, i32** %4
%53 = load i32, i32* %52
%54 = add i32 %53, 1
store i32 %54, i32* %52
br label %55
55: ; preds = %51, %48, %42
br label %30
56: ; preds = %30
%57 = load i32*, i32** %7
%58 = call i32 @fclose(i32* %57)
store i32 0, i32* %3
br label %59
59: ; preds = %56, %39, %26, %19, %13
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [19 x i8] c"parameter is NULL\0A\00"
@.str.1 = [14 x i8] c"/proc/net/tcp\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [26 x i8] c"Failed to open proc file\0A\00"
define i32 @activeSSHSessions(i32* %0, i16 zeroext %1) {
%3 = alloca [2048 x i8]
%4 = alloca i64
%5 = alloca i16
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%7 = bitcast i64* %4 to i8*
%8 = bitcast i16* %5 to i8*
%9 = icmp eq i32* %0, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @LOG(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %44
12: ; preds = %2
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @LOG(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %44
17: ; preds = %12
%18 = call i64 @readline(i32* %13, i8* %6, i32 2048)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i64 @readline(i32* %13, i8* %6, i32 2048)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %42
23: ; preds = %17
%24 = call i32 @fclose(i32* %13)
br label %44
25: ; preds = %20, %39
%26 = call i64 @parseLine(i8* %6, i16* %5, i64* %4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @fclose(i32* %13)
br label %44
30: ; preds = %25
%31 = load i16, i16* %5
%32 = icmp eq i16 %31, %1
%33 = load i64, i64* %4
%34 = icmp ne i64 %33, 0
%35 = select i1 %32, i1 %34, i1 false
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i32, i32* %0
%38 = add i32 %37, 1
store i32 %38, i32* %0
br label %39
39: ; preds = %36, %30
%40 = call i64 @readline(i32* %13, i8* %6, i32 2048)
%41 = icmp eq i64 %40, 0
br i1 %41, label %25, label %42
42: ; preds = %39, %20
%43 = call i32 @fclose(i32* %13)
br label %44
44: ; preds = %42, %28, %23, %15, %10
%45 = phi i32 [ -1, %10 ], [ -1, %15 ], [ -1, %23 ], [ -1, %28 ], [ 0, %42 ]
ret i32 %45
}