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

/* Variables and functions */
int atoi(char *);
char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ strncasecmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
unsigned int guessPort() {
  int port = 0;
  FILE *fp = fopen("/etc/ssh/sshd_config", "r");
  if (fp != NULL) {
    char *line = NULL;
    size_t len = 0;
    ssize_t read = 0;
    while ((read = getline(&line, &len, fp)) != -1) {
      if (strncasecmp("Port ", line, 5) == 0) {
        char *sport = calloc(6, sizeof(char));
        strncpy(sport, line + 5, read - 6);
        int portguess = atoi(sport);
        free(sport);
        if (portguess > 1024) {
          port = portguess;
        }
      }
    }
    free(line);
    fclose(fp);
  }
  if (port < 1) {
    port = 2222;
  }
  return port;
}


x86 assembly (clang, -O0):
<guessPort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1272 <guessPort+0xf2>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1260 <guessPort+0xe0>
mov    -0x18(%rbp),%rsi
lea    0xe27(%rip),%rdi
mov    $0x5,%edx
call   1070 <strncasecmp@plt>
cmp    $0x0,%rax
jne    125b <guessPort+0xdb>
mov    $0x6,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x5,%rsi
mov    -0x24(%rbp),%edx
sub    $0x6,%edx
call   1040 <strncpy@plt>
mov    -0x30(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x400,-0x34(%rbp)
jle    1256 <guessPort+0xd6>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125b <guessPort+0xdb>
jmp    11c8 <guessPort+0x48>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x1,-0x4(%rbp)
jge    1283 <guessPort+0x103>
movl   $0x8ae,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guessPort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe6b(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1291 <guessPort+0x111>
mov    %rax,%r12
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     1278 <guessPort+0xf8>
mov    %eax,%ebp
xor    %r13d,%r13d
lea    0xe34(%rip),%rbx
mov    %rsp,%r15
mov    %r12,0x10(%rsp)
jmp    1207 <guessPort+0x87>
nopl   (%rax)
mov    %r15,%rdi
lea    0x8(%rsp),%rsi
mov    %r12,%rdx
call   10a0 <getline@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     127b <guessPort+0xfb>
mov    (%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x5,%edx
call   1070 <strncasecmp@plt>
test   %rax,%rax
jne    11f0 <guessPort+0x70>
mov    $0x6,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %r13d,%r12d
mov    %r15,%r13
mov    %rax,%r15
add    $0x5,%r14
add    $0xfffffffa,%ebp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
mov    %r15,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %r13,%r15
mov    %r12d,%r13d
mov    0x10(%rsp),%r12
call   1030 <free@plt>
cmp    $0x401,%ebp
cmovge %ebp,%r13d
jmp    11f0 <guessPort+0x70>
xor    %r13d,%r13d
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
test   %r13d,%r13d
jg     1297 <guessPort+0x117>
mov    $0x8ae,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/etc/ssh/sshd_config\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"Port \00"
define i32 @guessPort() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %43
11: ; preds = %0
store i8* null, i8** %3
store i64 0, i64* %4
store i32 0, i32* %5
br label %12
12: ; preds = %37, %11
%13 = load i32*, i32** %2
%14 = call i32 @getline(i8** %3, i64* %4, i32* %13)
store i32 %14, i32* %5
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %38
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = call i64 @strncasecmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %17, i32 5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %16
%21 = call i8* @calloc(i32 6, i32 1)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 5
%25 = load i32, i32* %5
%26 = sub nsw i32 %25, 6
%27 = call i32 @strncpy(i8* %22, i8* %24, i32 %26)
%28 = load i8*, i8** %6
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %7
%30 = load i8*, i8** %6
%31 = call i32 @free(i8* %30)
%32 = load i32, i32* %7
%33 = icmp sgt i32 %32, 1024
br i1 %33, label %34, label %36
34: ; preds = %20
%35 = load i32, i32* %7
store i32 %35, i32* %1
br label %36
36: ; preds = %34, %20
br label %37
37: ; preds = %36, %16
br label %12
38: ; preds = %12
%39 = load i8*, i8** %3
%40 = call i32 @free(i8* %39)
%41 = load i32*, i32** %2
%42 = call i32 @fclose(i32* %41)
br label %43
43: ; preds = %38, %0
%44 = load i32, i32* %1
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 2222, i32* %1
br label %47
47: ; preds = %46, %43
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@.str = [21 x i8] c"/etc/ssh/sshd_config\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"Port \00"
define i32 @guessPort() {
%1 = alloca i8*
%2 = alloca i64
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %35, label %5
5: ; preds = %0
%6 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%7 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%8 = call i32 @getline(i8** %1, i64* %2, i32* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %29, label %10
10: ; preds = %5, %25
%11 = phi i32 [ %27, %25 ], [ %8, %5 ]
%12 = phi i32 [ %26, %25 ], [ 0, %5 ]
%13 = load i8*, i8** %1
%14 = call i64 @strncasecmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %10
%17 = call dereferenceable_or_null(6) i8* @calloc(i32 6, i32 1)
%18 = getelementptr inbounds i8, i8* %13, i64 5
%19 = add nsw i32 %11, -6
%20 = call i32 @strncpy(i8* %17, i8* %18, i32 %19)
%21 = call i32 @atoi(i8* %17)
%22 = call i32 @free(i8* %17)
%23 = icmp sgt i32 %21, 1024
%24 = select i1 %23, i32 %21, i32 %12
br label %25
25: ; preds = %16, %10
%26 = phi i32 [ %24, %16 ], [ %12, %10 ]
%27 = call i32 @getline(i8** %1, i64* %2, i32* %3)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %10
29: ; preds = %25, %5
%30 = phi i32 [ 0, %5 ], [ %26, %25 ]
%31 = load i8*, i8** %1
%32 = call i32 @free(i8* %31)
%33 = call i32 @fclose(i32* %3)
%34 = icmp slt i32 %30, 1
br i1 %34, label %35, label %36
35: ; preds = %0, %29
br label %36
36: ; preds = %29, %35
%37 = phi i32 [ 2222, %35 ], [ %30, %29 ]
ret i32 %37
}