#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 */
scalar_t__ isalnum(unsigned char const);
int mbstowcs(int /*<<< orphan*/, char const *, int /*<<< orphan*/);
int strnlen(char const *, int);
int is_valid_hostname(const char *host) {
  const unsigned char *s;
  if (strnlen(host, 255) - 1 >= 254 || mbstowcs(0, host, 0) == -1)
    return 0;
  for (s = (void *)host; *s >= 0x80 || *s == '.' || *s == '-' || isalnum(*s);
       s++)
    ;
  return !*s;
}


x86 assembly (clang, -O0):
<is_valid_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xff,%esi
call   1050 <strnlen@plt>
sub    $0x1,%eax
cmp    $0xfe,%eax
jge    116e <is_valid_hostname+0x3e>
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
mov    %edx,%edi
call   1030 <mbstowcs@plt>
cmp    $0xffffffff,%eax
jne    117a <is_valid_hostname+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    1214 <is_valid_hostname+0xe4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x80,%ecx
mov    %al,-0x19(%rbp)
jge    11da <is_valid_hostname+0xaa>
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x2e,%ecx
mov    %al,-0x19(%rbp)
je     11da <is_valid_hostname+0xaa>
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x2d,%ecx
mov    %al,-0x19(%rbp)
je     11da <is_valid_hostname+0xaa>
mov    -0x18(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11ea <is_valid_hostname+0xba>
jmp    1200 <is_valid_hostname+0xd0>
jmp    11ef <is_valid_hostname+0xbf>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1182 <is_valid_hostname+0x52>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_hostname>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0xff,%esi
call   1050 <strnlen@plt>
xor    %ebp,%ebp
cmp    $0xfe,%eax
jg     1189 <is_valid_hostname+0x59>
xor    %ebp,%ebp
xor    %edi,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <mbstowcs@plt>
cmp    $0xffffffff,%eax
jne    1164 <is_valid_hostname+0x34>
jmp    1189 <is_valid_hostname+0x59>
xchg   %ax,%ax
add    $0x1,%rbx
movzbl (%rbx),%eax
lea    -0x2d(%rax),%ecx
test   %al,%al
js     1160 <is_valid_hostname+0x30>
cmp    $0x2,%cl
jb     1160 <is_valid_hostname+0x30>
movzbl %al,%edi
call   1040 <isalnum@plt>
test   %rax,%rax
jne    1160 <is_valid_hostname+0x30>
xor    %ebp,%ebp
cmpb   $0x0,(%rbx)
sete   %bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @is_valid_hostname(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @strnlen(i8* %5, i32 255)
%7 = sub nsw i32 %6, 1
%8 = icmp sge i32 %7, 254
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @mbstowcs(i32 0, i8* %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %9, %1
store i32 0, i32* %2
br label %48
14: ; preds = %9
%15 = load i8*, i8** %3
store i8* %15, i8** %4
br label %16
16: ; preds = %39, %14
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = icmp sge i32 %19, 128
br i1 %20, label %36, label %21
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = icmp eq i32 %24, 46
br i1 %25, label %36, label %26
26: ; preds = %21
%27 = load i8*, i8** %4
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = icmp eq i32 %29, 45
br i1 %30, label %36, label %31
31: ; preds = %26
%32 = load i8*, i8** %4
%33 = load i8, i8* %32
%34 = call i64 @isalnum(i8 zeroext %33)
%35 = icmp ne i64 %34, 0
br label %36
36: ; preds = %31, %26, %21, %16
%37 = phi i1 [ true, %26 ], [ true, %21 ], [ true, %16 ], [ %35, %31 ]
br i1 %37, label %38, label %42
38: ; preds = %36
br label %39
39: ; preds = %38
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %4
br label %16
42: ; preds = %36
%43 = load i8*, i8** %4
%44 = load i8, i8* %43
%45 = icmp ne i8 %44, 0
%46 = xor i1 %45, true
%47 = zext i1 %46 to i32
store i32 %47, i32* %2
br label %48
48: ; preds = %42, %13
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
define i32 @is_valid_hostname(i8* %0) {
%2 = tail call i32 @strnlen(i8* %0, i32 255)
%3 = icmp sgt i32 %2, 254
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = tail call i32 @mbstowcs(i32 0, i8* %0, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %24, label %7
7: ; preds = %4, %18
%8 = phi i8* [ %19, %18 ], [ %0, %4 ]
%9 = load i8, i8* %8
%10 = freeze i8 %9
%11 = icmp slt i8 %10, 0
%12 = add i8 %10, -45
%13 = icmp ult i8 %12, 2
%14 = or i1 %11, %13
br i1 %14, label %18, label %15
15: ; preds = %7
%16 = tail call i64 @isalnum(i8 zeroext %10)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %7, %15
%19 = getelementptr inbounds i8, i8* %8, i64 1
br label %7
20: ; preds = %15
%21 = load i8, i8* %8
%22 = icmp eq i8 %21, 0
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %1, %4, %20
%25 = phi i32 [ %23, %20 ], [ 0, %4 ], [ 0, %1 ]
ret i32 %25
}