#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 */

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ isdigit(char);
int strlen(char *);
int get_port_num(char *str_port_num) {
  int port_num = 0;
  int i = 0;
  for (i = 0; i < strlen(str_port_num); i++) {
    if (!isdigit(str_port_num[i])) {
      return -1;
    }
  }
  port_num = atoi(str_port_num);
  if ((port_num > 0) && (port_num < 65535)) {
    return port_num;
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_port_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    11a6 <get_port_num+0x76>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1193 <get_port_num+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <get_port_num+0xab>
jmp    1198 <get_port_num+0x68>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1151 <get_port_num+0x21>
mov    -0x10(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11d4 <get_port_num+0xa4>
cmpl   $0xffff,-0x14(%rbp)
jge    11d4 <get_port_num+0xa4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11db <get_port_num+0xab>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_port_num>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %eax,%eax
jle    1171 <get_port_num+0x41>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r14,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     118d <get_port_num+0x5d>
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1150 <get_port_num+0x20>
mov    %r14,%rdi
call   1050 <atoi@plt>
mov    %eax,%ecx
lea    -0x1(%rcx),%eax
cmp    $0xfffe,%eax
mov    $0xffffffff,%eax
cmovb  %ecx,%eax
jmp    1192 <get_port_num+0x62>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @get_port_num(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %5
br label %6
6: ; preds = %21, %1
%7 = load i32, i32* %5
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = icmp slt i32 %7, %9
br i1 %10, label %11, label %24
11: ; preds = %6
%12 = load i8*, i8** %3
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8, i8* %15
%17 = call i32 @isdigit(i8 signext %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %11
store i32 -1, i32* %2
br label %35
20: ; preds = %11
br label %21
21: ; preds = %20
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
store i32 %23, i32* %5
br label %6
24: ; preds = %6
%25 = load i8*, i8** %3
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 65535
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %35
34: ; preds = %29, %24
store i32 -1, i32* %2
br label %35
35: ; preds = %34, %32, %19
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
define i32 @get_port_num(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %8, label %15
4: ; preds = %8
%5 = tail call i32 @strlen(i8* %0)
%6 = sext i32 %5 to i64
%7 = icmp slt i64 %14, %6
br i1 %7, label %8, label %15
8: ; preds = %1, %4
%9 = phi i64 [ %14, %4 ], [ 0, %1 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i32 @isdigit(i8 signext %11)
%13 = icmp eq i32 %12, 0
%14 = add nuw nsw i64 %9, 1
br i1 %13, label %20, label %4
15: ; preds = %4, %1
%16 = tail call i32 @atoi(i8* %0)
%17 = add i32 %16, -1
%18 = icmp ult i32 %17, 65534
%19 = select i1 %18, i32 %16, i32 -1
br label %20
20: ; preds = %8, %15
%21 = phi i32 [ %19, %15 ], [ -1, %8 ]
ret i32 %21
}