#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 const *);
int /*<<< orphan*/ isdigit(char const);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *config_parse_single_port(const char *buf) {
  int test = 0;
  int len = strlen(buf) + 1;
  char *ret;
  int i;
  for (i = 0; i < (len - 1); i++) {
    if (!isdigit(buf[i])) {
      return NULL;
    }
  }
  test = atoi(buf);
  if (test > 65535 || test < 1) {
    return NULL;
  }
  ret = malloc(sizeof(char) * len);
  memcpy(ret, buf, len);
  return ret;
}


x86 assembly (clang, -O0):
<config_parse_single_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11c4 <config_parse_single_port+0x74>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    11b1 <config_parse_single_port+0x61>
movq   $0x0,-0x8(%rbp)
jmp    121f <config_parse_single_port+0xcf>
jmp    11b6 <config_parse_single_port+0x66>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1179 <config_parse_single_port+0x29>
mov    -0x10(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffff,-0x14(%rbp)
jg     11e7 <config_parse_single_port+0x97>
cmpl   $0x1,-0x14(%rbp)
jge    11f4 <config_parse_single_port+0xa4>
movq   $0x0,-0x8(%rbp)
jmp    121f <config_parse_single_port+0xcf>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_parse_single_port>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    1187 <config_parse_single_port+0x37>
mov    %r15d,%ebp
xor    %r14d,%r14d
xor    %ebx,%ebx
nop
movsbl (%r12,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11bd <config_parse_single_port+0x6d>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <config_parse_single_port+0x20>
mov    %r12,%rdi
call   1070 <atoi@plt>
add    $0xffff0000,%eax
cmp    $0xffff0001,%eax
jae    11a0 <config_parse_single_port+0x50>
xor    %r14d,%r14d
jmp    11bd <config_parse_single_port+0x6d>
add    $0x1,%r15d
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @config_parse_single_port(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
store i32 %10, i32* %5
store i32 0, i32* %7
br label %11
11: ; preds = %26, %1
%12 = load i32, i32* %7
%13 = load i32, i32* %5
%14 = sub nsw i32 %13, 1
%15 = icmp slt i32 %12, %14
br i1 %15, label %16, label %29
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = call i32 @isdigit(i8 signext %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %16
store i8* null, i8** %2
br label %49
25: ; preds = %16
br label %26
26: ; preds = %25
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %11
29: ; preds = %11
%30 = load i8*, i8** %3
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp sgt i32 %32, 65535
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i32, i32* %4
%36 = icmp slt i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %34, %29
store i8* null, i8** %2
br label %49
38: ; preds = %34
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = mul i64 1, %40
%42 = trunc i64 %41 to i32
%43 = call i8* @malloc(i32 %42)
store i8* %43, i8** %6
%44 = load i8*, i8** %6
%45 = load i8*, i8** %3
%46 = load i32, i32* %5
%47 = call i32 @memcpy(i8* %44, i8* %45, i32 %46)
%48 = load i8*, i8** %6
store i8* %48, i8** %2
br label %49
49: ; preds = %38, %37, %24
%50 = load i8*, i8** %2
ret i8* %50
}

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