#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 */
struct protoent {
  scalar_t__ p_proto;
};

/* Variables and functions */
int atoi(char const *);
struct protoent *getprotobyname(char const *);
scalar_t__ re_match(char const *, char *);
scalar_t__ strcmp(char const *, char *);
int validate_protocol(const char *str) {
  if (!str)
    return 0;
  if (strcmp(str, "all") == 0)
    return 1;
  if (strcmp(str, "ip") == 0 || strcmp(str, "0") == 0)
    return 1;
  if (re_match(str, "^[0-9]+$")) {
    int val = atoi(str);
    if (val >= 1 && val <= 255)
      return 1;
  }
  struct protoent *p;
  p = getprotobyname(str);
  if (!p)
    return 0;
  if (p->p_proto) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<validate_protocol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <validate_protocol+0x23>
movl   $0x0,-0x4(%rbp)
jmp    125c <validate_protocol+0x11c>
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1189 <validate_protocol+0x49>
movl   $0x1,-0x4(%rbp)
jmp    125c <validate_protocol+0x11c>
mov    -0x10(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11bd <validate_protocol+0x7d>
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11c9 <validate_protocol+0x89>
movl   $0x1,-0x4(%rbp)
jmp    125c <validate_protocol+0x11c>
mov    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1030 <re_match@plt>
cmp    $0x0,%rax
je     1217 <validate_protocol+0xd7>
mov    -0x10(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     1212 <validate_protocol+0xd2>
cmpl   $0xff,-0x14(%rbp)
jg     1212 <validate_protocol+0xd2>
movl   $0x1,-0x4(%rbp)
jmp    125c <validate_protocol+0x11c>
jmp    1217 <validate_protocol+0xd7>
mov    -0x10(%rbp),%rdi
call   1050 <getprotobyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123b <validate_protocol+0xfb>
movl   $0x0,-0x4(%rbp)
jmp    125c <validate_protocol+0x11c>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1255 <validate_protocol+0x115>
movl   $0x1,-0x4(%rbp)
jmp    125c <validate_protocol+0x11c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_protocol>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rdi,%rdi
je     11d3 <validate_protocol+0x93>
mov    %rdi,%rbx
lea    0xea8(%rip),%rsi
call   1040 <strcmp@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     11d3 <validate_protocol+0x93>
lea    0xe96(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11d3 <validate_protocol+0x93>
lea    0xe85(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11d3 <validate_protocol+0x93>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
call   1030 <re_match@plt>
test   %rax,%rax
je     11b7 <validate_protocol+0x77>
mov    %rbx,%rdi
call   1060 <atoi@plt>
add    $0xffffff00,%eax
cmp    $0xffffff00,%eax
ja     11d3 <validate_protocol+0x93>
mov    %rbx,%rdi
call   1050 <getprotobyname@plt>
mov    $0x0,%ebp
test   %rax,%rax
je     11d3 <validate_protocol+0x93>
xor    %ebp,%ebp
cmpq   $0x0,(%rax)
setne  %bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.protoent = type { i64 }
@.str = [4 x i8] c"all\00"
@.str.1 = [3 x i8] c"ip\00"
@.str.2 = [2 x i8] c"0\00"
@.str.3 = [9 x i8] c"^[0-9]+$\00"
define i32 @validate_protocol(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.protoent*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i32 0, i32* %2
br label %50
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 1, i32* %2
br label %50
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18, %14
store i32 1, i32* %2
br label %50
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = call i64 @re_match(i8* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp sge i32 %30, 1
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* %4
%34 = icmp sle i32 %33, 255
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 1, i32* %2
br label %50
36: ; preds = %32, %27
br label %37
37: ; preds = %36, %23
%38 = load i8*, i8** %3
%39 = call %struct.protoent* @getprotobyname(i8* %38)
store %struct.protoent* %39, %struct.protoent** %5
%40 = load %struct.protoent*, %struct.protoent** %5
%41 = icmp ne %struct.protoent* %40, null
br i1 %41, label %43, label %42
42: ; preds = %37
store i32 0, i32* %2
br label %50
43: ; preds = %37
%44 = load %struct.protoent*, %struct.protoent** %5
%45 = getelementptr inbounds %struct.protoent, %struct.protoent* %44, i32 0, i32 0
%46 = load i64, i64* %45
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 1, i32* %2
br label %50
49: ; preds = %43
store i32 0, i32* %2
br label %50
50: ; preds = %49, %48, %42, %35, %22, %13, %8
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.protoent = type { i64 }
@.str = [4 x i8] c"all\00"
@.str.1 = [3 x i8] c"ip\00"
@.str.2 = [2 x i8] c"0\00"
@.str.3 = [9 x i8] c"^[0-9]+$\00"
define i32 @validate_protocol(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %27, label %3
3: ; preds = %1
%4 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %27, label %6
6: ; preds = %3
%7 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %27, label %9
9: ; preds = %6
%10 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %9
%13 = tail call i64 @re_match(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = tail call i32 @atoi(i8* %0)
%17 = add i32 %16, -256
%18 = icmp ult i32 %17, -255
br i1 %18, label %19, label %27
19: ; preds = %15, %12
%20 = tail call %struct.protoent* @getprotobyname(i8* %0)
%21 = icmp eq %struct.protoent* %20, null
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = getelementptr inbounds %struct.protoent, %struct.protoent* %20, i64 0, i32 0
%24 = load i64, i64* %23
%25 = icmp ne i64 %24, 0
%26 = zext i1 %25 to i32
br label %27
27: ; preds = %15, %19, %22, %6, %9, %3, %1
%28 = phi i32 [ 1, %15 ], [ 0, %1 ], [ 1, %3 ], [ 1, %9 ], [ 1, %6 ], [ 0, %19 ], [ %26, %22 ]
ret i32 %28
}