#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 */
unsigned int atoi(char *);
int /*<<< orphan*/ free_regexp_matches(char **);
int /*<<< orphan*/ regexp_match_grab(char const *, char const *, int, char **);
short int ipv4_check_ip(const char *ip) {
  char *matches[1 + 4];
  const char *pattern = "^("
                        "[1-2]?[0-9]?[0-9]"
                        ")\\.("
                        "[1-2]?[0-9]?[0-9]"
                        ")\\.("
                        "[1-2]?[0-9]?[0-9]"
                        ")\\.("
                        "[1-2]?[0-9]?[0-9]"
                        ")$";
  int i = 0;
  if (!regexp_match_grab(ip, pattern, 1, matches))
    return -1;
  for (; i <= 3; i++) {
    unsigned int byte = atoi(matches[i]);
    if (byte < 0 || byte > 255) {
      free_regexp_matches(matches);
      return -2;
    }
  }
  free_regexp_matches(matches);
  return 0;
}


x86 assembly (clang, -O0):
<ipv4_check_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xebd(%rip),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    -0x40(%rbp),%rcx
mov    $0x1,%edx
call   1030 <regexp_match_grab@plt>
cmp    $0x0,%eax
jne    1178 <ipv4_check_ip+0x48>
movw   $0xffff,-0x2(%rbp)
jmp    11e5 <ipv4_check_ip+0xb5>
jmp    117d <ipv4_check_ip+0x4d>
cmpl   $0x3,-0x4c(%rbp)
jg     11d6 <ipv4_check_ip+0xa6>
movslq -0x4c(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
call   1040 <atoi@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jb     11af <ipv4_check_ip+0x7f>
cmpl   $0xff,-0x50(%rbp)
jbe    11c3 <ipv4_check_ip+0x93>
lea    -0x40(%rbp),%rdi
call   1050 <free_regexp_matches@plt>
movw   $0xfffe,-0x2(%rbp)
jmp    11e5 <ipv4_check_ip+0xb5>
jmp    11c8 <ipv4_check_ip+0x98>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    117d <ipv4_check_ip+0x4d>
lea    -0x40(%rbp),%rdi
call   1050 <free_regexp_matches@plt>
movw   $0x0,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv4_check_ip>:
push   %rbx
sub    $0x30,%rsp
lea    0xec4(%rip),%rsi
mov    %rsp,%rcx
mov    $0x1,%edx
call   1030 <regexp_match_grab@plt>
test   %eax,%eax
je     119e <ipv4_check_ip+0x6e>
mov    (%rsp),%rdi
call   1040 <atoi@plt>
cmp    $0xff,%eax
ja     1190 <ipv4_check_ip+0x60>
mov    0x8(%rsp),%rdi
call   1040 <atoi@plt>
cmp    $0xff,%eax
ja     1190 <ipv4_check_ip+0x60>
mov    0x10(%rsp),%rdi
call   1040 <atoi@plt>
cmp    $0xff,%eax
ja     1190 <ipv4_check_ip+0x60>
mov    0x18(%rsp),%rdi
call   1040 <atoi@plt>
cmp    $0x100,%eax
jb     11aa <ipv4_check_ip+0x7a>
mov    $0xfffe,%bx
mov    %rsp,%rdi
call   1050 <free_regexp_matches@plt>
jmp    11a2 <ipv4_check_ip+0x72>
mov    $0xffff,%bx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
ret
xor    %ebx,%ebx
jmp    1194 <ipv4_check_ip+0x64>

LLVM IR (-O0):
@.str = [85 x i8] c"^([1-2]?[0-9]?[0-9])\\.([1-2]?[0-9]?[0-9])\\.([1-2]?[0-9]?[0-9])\\.([1-2]?[0-9]?[0-9])$\00"
define signext i16 @ipv4_check_ip(i8* %0) {
%2 = alloca i16
%3 = alloca i8*
%4 = alloca [5 x i8*]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i8** %5
store i32 0, i32* %6
%8 = load i8*, i8** %3
%9 = load i8*, i8** %5
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 0
%11 = call i32 @regexp_match_grab(i8* %8, i8* %9, i32 1, i8** %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %1
store i16 -1, i16* %2
br label %39
14: ; preds = %1
br label %15
15: ; preds = %33, %14
%16 = load i32, i32* %6
%17 = icmp sle i32 %16, 3
br i1 %17, label %18, label %36
18: ; preds = %15
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp ult i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %18
%27 = load i32, i32* %7
%28 = icmp ugt i32 %27, 255
br i1 %28, label %29, label %32
29: ; preds = %26, %18
%30 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 0
%31 = call i32 @free_regexp_matches(i8** %30)
store i16 -2, i16* %2
br label %39
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %15
36: ; preds = %15
%37 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 0
%38 = call i32 @free_regexp_matches(i8** %37)
store i16 0, i16* %2
br label %39
39: ; preds = %36, %29, %13
%40 = load i16, i16* %2
ret i16 %40
}

LLVM IR (-O2):
@.str = [85 x i8] c"^([1-2]?[0-9]?[0-9])\\.([1-2]?[0-9]?[0-9])\\.([1-2]?[0-9]?[0-9])\\.([1-2]?[0-9]?[0-9])$\00"
define signext i16 @ipv4_check_ip(i8* %0) {
%2 = alloca [5 x i8*]
%3 = bitcast [5 x i8*]* %2 to i8*
%4 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
%5 = call i32 @regexp_match_grab(i8* %0, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0), i32 1, i8** %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %30, label %7
7: ; preds = %1
%8 = load i8*, i8** %4
%9 = call i32 @atoi(i8* %8)
%10 = icmp ugt i32 %9, 255
br i1 %10, label %26, label %11
11: ; preds = %7
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @atoi(i8* %13)
%15 = icmp ugt i32 %14, 255
br i1 %15, label %26, label %16
16: ; preds = %11
%17 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
%20 = icmp ugt i32 %19, 255
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
%25 = icmp ugt i32 %24, 255
br i1 %25, label %26, label %27
26: ; preds = %21, %16, %11, %7
br label %27
27: ; preds = %21, %26
%28 = phi i16 [ -2, %26 ], [ 0, %21 ]
%29 = call i32 @free_regexp_matches(i8** %4)
br label %30
30: ; preds = %27, %1
%31 = phi i16 [ -1, %1 ], [ %28, %27 ]
ret i16 %31
}