#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 sscanf(char const *, char *, int *, char *);
int port_parse(const char pstr[], int err) {
  int port;
  char c;
  if (pstr && sscanf(pstr, "%d%c", &port, &c) == 1 && port >= 0 &&
      port <= 65535) {
    return port;
  } else {
    return err;
  }
}


x86 assembly (clang, -O0):
<port_parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116d <port_parse+0x5d>
mov    -0x10(%rbp),%rdi
lea    0xecb(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x19(%rbp),%rcx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    116d <port_parse+0x5d>
cmpl   $0x0,-0x18(%rbp)
jl     116d <port_parse+0x5d>
cmpl   $0xffff,-0x18(%rbp)
jg     116d <port_parse+0x5d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1173 <port_parse+0x63>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<port_parse>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
test   %rdi,%rdi
je     1146 <port_parse+0x36>
lea    0xedd(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0xb(%rsp),%rcx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    1146 <port_parse+0x36>
mov    0xc(%rsp),%eax
test   %eax,%eax
js     1146 <port_parse+0x36>
cmp    $0x10000,%eax
jl     1148 <port_parse+0x38>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%d%c\00"
define i32 @port_parse(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %22
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %6, i8* %7)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = load i32, i32* %6
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i32, i32* %6
%19 = icmp sle i32 %18, 65535
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32, i32* %6
store i32 %21, i32* %3
br label %24
22: ; preds = %17, %14, %10, %2
%23 = load i32, i32* %5
store i32 %23, i32* %3
br label %24
24: ; preds = %22, %20
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@.str = [5 x i8] c"%d%c\00"
define i32 @port_parse(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8
%5 = bitcast i32* %3 to i8*
%6 = icmp eq i8* %0, null
br i1 %6, label %15, label %7
7: ; preds = %2
%8 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %3, i8* %4)
%9 = icmp eq i32 %8, 1
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, -1
%12 = select i1 %9, i1 %11, i1 false
%13 = icmp slt i32 %10, 65536
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %16, label %15
15: ; preds = %7, %2
br label %16
16: ; preds = %7, %15
%17 = phi i32 [ %1, %15 ], [ %10, %7 ]
ret i32 %17
}