#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 htons(unsigned int);
unsigned int strtoul(char *, char **, int /*<<< orphan*/);
unsigned name2port(char *n) {
  char *end;
  unsigned port;
  port = strtoul(n, &end, 0);
  if (end == n || *end != 0) {
    port = 1;
    while (*n != 0)
      port *= (*n++ & 0xFF);
    port |= 0x8000;
  }
  return htons(port & 0xFFFF);
}


x86 assembly (clang, -O0):
<name2port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     115c <name2port+0x3c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a1 <name2port+0x81>
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1196 <name2port+0x76>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
and    $0xff,%eax
imul   -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1163 <name2port+0x43>
mov    -0x14(%rbp),%eax
or     $0x8000,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
and    $0xffff,%edi
call   1030 <htons@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name2port>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     1143 <name2port+0x23>
cmpb   $0x0,(%rcx)
je     117d <name2port+0x5d>
mov    (%rbx),%cl
test   %cl,%cl
je     1178 <name2port+0x58>
add    $0x1,%rbx
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl %cl,%ecx
imul   %ecx,%eax
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1160 <name2port+0x40>
or     $0x8000,%eax
jmp    117d <name2port+0x5d>
mov    $0x8001,%eax
movzwl %ax,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <htons@plt>

LLVM IR (-O0):
define i32 @name2port(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strtoul(i8* %5, i8** %3, i32 0)
store i32 %6, i32* %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %2
%9 = icmp eq i8* %7, %8
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %10, %1
store i32 1, i32* %4
br label %16
16: ; preds = %21, %15
%17 = load i8*, i8** %2
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = load i8*, i8** %2
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %2
%24 = load i8, i8* %22
%25 = sext i8 %24 to i32
%26 = and i32 %25, 255
%27 = load i32, i32* %4
%28 = mul i32 %27, %26
store i32 %28, i32* %4
br label %16
29: ; preds = %16
%30 = load i32, i32* %4
%31 = or i32 %30, 32768
store i32 %31, i32* %4
br label %32
32: ; preds = %29, %10
%33 = load i32, i32* %4
%34 = and i32 %33, 65535
%35 = call i32 @htons(i32 %34)
ret i32 %35
}

LLVM IR (-O2):
define i32 @name2port(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i32 @strtoul(i8* %0, i8** %2, i32 0)
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, %0
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %7, %1
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %10, %13
%14 = phi i8 [ %20, %13 ], [ %11, %10 ]
%15 = phi i32 [ %19, %13 ], [ 1, %10 ]
%16 = phi i8* [ %18, %13 ], [ %0, %10 ]
%17 = zext i8 %14 to i32
%18 = getelementptr inbounds i8, i8* %16, i64 1
%19 = mul i32 %15, %17
%20 = load i8, i8* %18
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %13
22: ; preds = %13
%23 = or i32 %19, 32768
br label %24
24: ; preds = %10, %22, %7
%25 = phi i32 [ %4, %7 ], [ 32769, %10 ], [ %23, %22 ]
%26 = and i32 %25, 65535
%27 = tail call i32 @htons(i32 %26)
ret i32 %27
}