#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 scalar_t__ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int atoi(char *);
size_t strcspn(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int parse_udprelay(const char *opt, size_t optlen, char *addr, size_t addrlen,
                   uint16_t *port) {
  int rc = 1;
  size_t n;
  int pval;
  const char *SEP = ":%~+-^";
  const int MAX_PORT = 65535;
  char s[512];
  assert(opt && addr && addrlen && port);
  (void)strncpy(s, opt, 512);
  s[512 - 1] = '\0';
  do {
    n = strcspn(s, SEP);
    if (!n || n >= optlen)
      break;
    s[n] = '\0';
    strncpy(addr, s, addrlen);
    addr[addrlen - 1] = '\0';
    ++n;
    pval = atoi(s + n);
    if ((pval > 0) && (pval < MAX_PORT)) {
      *port = (uint16_t)pval;
    } else {
      rc = 3;
      break;
    }
    rc = 0;
  } while (0);
  return rc;
}


x86 assembly (clang, -O0):
<parse_udprelay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x48(%rbp)
movl   $0xffff,-0x4c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x251(%rbp)
je     11bf <parse_udprelay+0x7f>
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x251(%rbp)
je     11bf <parse_udprelay+0x7f>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x251(%rbp)
je     11bf <parse_udprelay+0x7f>
cmpq   $0x0,-0x28(%rbp)
setne  %al
mov    %al,-0x251(%rbp)
mov    -0x251(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x250(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x51(%rbp)
lea    -0x250(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1040 <strcspn@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1215 <parse_udprelay+0xd5>
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     121a <parse_udprelay+0xda>
jmp    12a3 <parse_udprelay+0x163>
mov    -0x38(%rbp),%rax
movb   $0x0,-0x250(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
lea    -0x250(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
lea    -0x250(%rbp),%rdi
add    -0x38(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    1290 <parse_udprelay+0x150>
cmpl   $0xffff,-0x3c(%rbp)
jge    1290 <parse_udprelay+0x150>
movslq -0x3c(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
jmp    129c <parse_udprelay+0x15c>
movl   $0x3,-0x2c(%rbp)
jmp    12a3 <parse_udprelay+0x163>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_udprelay>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%rbx
test   %rdi,%rdi
setne  %al
test   %rdx,%rdx
setne  %cl
and    %al,%cl
test   %r15,%r15
setne  %al
test   %r8,%r8
setne  %dl
and    %al,%dl
and    %cl,%dl
movzbl %dl,%edi
call   1050 <assert@plt>
mov    %rsp,%rbp
mov    $0x200,%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
movb   $0x0,0x1ff(%rsp)
lea    0xe58(%rip),%rsi
mov    %rbp,%rdi
call   1040 <strcspn@plt>
mov    %rax,%rbx
mov    $0x1,%eax
test   %rbx,%rbx
je     1200 <parse_udprelay+0xc0>
cmp    %r12,%rbx
jae    1200 <parse_udprelay+0xc0>
movb   $0x0,(%rsp,%rbx,1)
mov    %rsp,%rsi
mov    %r13,%rdi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r15,%r13,1)
lea    (%rsp,%rbx,1),%rdi
add    $0x1,%rdi
call   1060 <atoi@plt>
mov    %eax,%ecx
lea    -0x1(%rcx),%edx
mov    $0x3,%eax
cmp    $0xfffd,%edx
ja     1200 <parse_udprelay+0xc0>
mov    %ecx,%eax
mov    %rax,(%r14)
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c":%~+-^\00"
define i32 @parse_udprelay(i8* %0, i64 %1, i8* %2, i64 %3, i64* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64*
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
%16 = alloca [512 x i8]
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store i64* %4, i64** %10
store i32 1, i32* %11
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %14
store i32 65535, i32* %15
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %28
19: ; preds = %5
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i64, i64* %9
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i64*, i64** %10
%27 = icmp ne i64* %26, null
br label %28
28: ; preds = %25, %22, %19, %5
%29 = phi i1 [ false, %22 ], [ false, %19 ], [ false, %5 ], [ %27, %25 ]
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%33 = load i8*, i8** %6
%34 = call i32 @strncpy(i8* %32, i8* %33, i64 512)
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 511
store i8 0, i8* %35
br label %36
36: ; preds = %28
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%38 = load i8*, i8** %14
%39 = call i64 @strcspn(i8* %37, i8* %38)
store i64 %39, i64* %12
%40 = load i64, i64* %12
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i64, i64* %12
%44 = load i64, i64* %7
%45 = icmp uge i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42, %36
br label %75
47: ; preds = %42
%48 = load i64, i64* %12
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %8
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%52 = load i64, i64* %9
%53 = call i32 @strncpy(i8* %50, i8* %51, i64 %52)
%54 = load i8*, i8** %8
%55 = load i64, i64* %9
%56 = sub i64 %55, 1
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 0, i8* %57
%58 = load i64, i64* %12
%59 = add i64 %58, 1
store i64 %59, i64* %12
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%61 = load i64, i64* %12
%62 = getelementptr inbounds i8, i8* %60, i64 %61
%63 = call i32 @atoi(i8* %62)
store i32 %63, i32* %13
%64 = load i32, i32* %13
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %47
%67 = load i32, i32* %13
%68 = icmp slt i32 %67, 65535
br i1 %68, label %69, label %73
69: ; preds = %66
%70 = load i32, i32* %13
%71 = sext i32 %70 to i64
%72 = load i64*, i64** %10
store i64 %71, i64* %72
br label %74
73: ; preds = %66, %47
store i32 3, i32* %11
br label %75
74: ; preds = %69
store i32 0, i32* %11
br label %75
75: ; preds = %74, %73, %46
%76 = load i32, i32* %11
ret i32 %76
}

LLVM IR (-O2):
@.str = [7 x i8] c":%~+-^\00"
define i32 @parse_udprelay(i8* %0, i64 %1, i8* %2, i64 %3, i64* writeonly %4) {
%6 = alloca [512 x i8]
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%8 = icmp ne i8* %0, null
%9 = icmp ne i8* %2, null
%10 = and i1 %8, %9
%11 = icmp ne i64 %3, 0
%12 = and i1 %10, %11
%13 = icmp ne i64* %4, null
%14 = and i1 %12, %13
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = call i32 @strncpy(i8* %7, i8* %0, i64 512)
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 511
store i8 0, i8* %18
%19 = call i64 @strcspn(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = icmp ne i64 %19, 0
%21 = icmp ult i64 %19, %1
%22 = and i1 %20, %21
br i1 %22, label %23, label %35
23: ; preds = %5
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %19
store i8 0, i8* %24
%25 = call i32 @strncpy(i8* %2, i8* %7, i64 %3)
%26 = add i64 %3, -1
%27 = getelementptr inbounds i8, i8* %2, i64 %26
store i8 0, i8* %27
%28 = add nuw i64 %19, 1
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %28
%30 = call i32 @atoi(i8* %29)
%31 = add i32 %30, -1
%32 = icmp ult i32 %31, 65534
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = zext i32 %30 to i64
store i64 %34, i64* %4
br label %35
35: ; preds = %23, %5, %33
%36 = phi i32 [ 0, %33 ], [ 1, %5 ], [ 3, %23 ]
ret i32 %36
}