#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 /*<<< orphan*/ re_match(char const *, char *);
int sscanf(char const *, char *, unsigned int *, unsigned int *, unsigned int *,
           unsigned int *);
scalar_t__ strlen(char const *);
int validate_ipv4(const char *str) {
  if (!str)
    return 0;
  unsigned int a[4];
  if (strlen(str) == 0) {
    return 0;
  }
  if (!re_match(str, "^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$"))
    return 0;
  if (sscanf(str, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]) != 4)
    return 0;
  int i;
  for (i = 0; i < 4; i++) {
    if (a[i] > 255)
      return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<validate_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1153 <validate_ipv4+0x23>
movl   $0x0,-0x4(%rbp)
jmp    1221 <validate_ipv4+0xf1>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1172 <validate_ipv4+0x42>
movl   $0x0,-0x4(%rbp)
jmp    1221 <validate_ipv4+0xf1>
mov    -0x10(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1030 <re_match@plt>
cmp    $0x0,%eax
jne    1197 <validate_ipv4+0x67>
movl   $0x0,-0x4(%rbp)
jmp    1221 <validate_ipv4+0xf1>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    -0x20(%rbp),%rcx
add    $0x4,%rcx
lea    -0x20(%rbp),%r8
add    $0x8,%r8
lea    -0x20(%rbp),%r9
add    $0xc,%r9
lea    0xe63(%rip),%rsi
call   1050 <sscanf@plt>
cmp    $0x4,%eax
je     11d8 <validate_ipv4+0xa8>
movl   $0x0,-0x4(%rbp)
jmp    1221 <validate_ipv4+0xf1>
movl   $0x0,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
jge    121a <validate_ipv4+0xea>
movslq -0x24(%rbp),%rax
cmpl   $0xff,-0x20(%rbp,%rax,4)
jbe    1207 <validate_ipv4+0xd7>
movl   $0x0,-0x4(%rbp)
jmp    1221 <validate_ipv4+0xf1>
jmp    120c <validate_ipv4+0xdc>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11df <validate_ipv4+0xaf>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_ipv4>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     11a7 <validate_ipv4+0x87>
mov    %rdi,%rbx
xor    %ebp,%ebp
cmpb   $0x0,(%rdi)
je     11a9 <validate_ipv4+0x89>
lea    0xec4(%rip),%rsi
mov    %rbx,%rdi
call   1030 <re_match@plt>
test   %eax,%eax
je     11a9 <validate_ipv4+0x89>
lea    0x4(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
lea    0xec3(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1040 <sscanf@plt>
cmp    $0x4,%eax
sete   %al
cmpl   $0x100,(%rsp)
setb   %cl
and    %al,%cl
cmpl   $0x100,0x4(%rsp)
setb   %al
cmpl   $0x100,0x8(%rsp)
setb   %dl
and    %al,%dl
and    %cl,%dl
cmpl   $0x100,0xc(%rsp)
setb   %al
and    %dl,%al
movzbl %al,%ebp
jmp    11a9 <validate_ipv4+0x89>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$\00"
@.str.1 = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @validate_ipv4(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [4 x i32]
%5 = alloca i32
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 %44
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 0, i32* %2
br label %44
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = call i32 @re_match(i8* %15, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %14
store i32 0, i32* %2
br label %44
19: ; preds = %14
%20 = load i8*, i8** %3
%21 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%22 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 1
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 2
%24 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
%25 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %21, i32* %22, i32* %23, i32* %24)
%26 = icmp ne i32 %25, 4
br i1 %26, label %27, label %28
27: ; preds = %19
store i32 0, i32* %2
br label %44
28: ; preds = %19
store i32 0, i32* %5
br label %29
29: ; preds = %40, %28
%30 = load i32, i32* %5
%31 = icmp slt i32 %30, 4
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = icmp ugt i32 %36, 255
br i1 %37, label %38, label %39
38: ; preds = %32
store i32 0, i32* %2
br label %44
39: ; preds = %32
br label %40
40: ; preds = %39
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 1
store i32 %42, i32* %5
br label %29
43: ; preds = %29
store i32 1, i32* %2
br label %44
44: ; preds = %43, %38, %27, %18, %13, %8
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@.str = [33 x i8] c"^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$\00"
@.str.1 = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @validate_ipv4(i8* %0) {
%2 = alloca [4 x i32]
%3 = icmp eq i8* %0, null
br i1 %3, label %33, label %4
4: ; preds = %1
%5 = bitcast [4 x i32]* %2 to i8*
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %31, label %8
8: ; preds = %4
%9 = tail call i32 @re_match(i8* %0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %31, label %11
11: ; preds = %8
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%14 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%16 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %12, i32* %13, i32* %14, i32* %15)
%17 = icmp eq i32 %16, 4
%18 = load i32, i32* %12
%19 = icmp ult i32 %18, 256
%20 = select i1 %17, i1 %19, i1 false
%21 = load i32, i32* %13
%22 = icmp ult i32 %21, 256
%23 = select i1 %20, i1 %22, i1 false
%24 = load i32, i32* %14
%25 = icmp ult i32 %24, 256
%26 = select i1 %23, i1 %25, i1 false
%27 = load i32, i32* %15
%28 = icmp ult i32 %27, 256
%29 = select i1 %26, i1 %28, i1 false
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %11, %8, %4
%32 = phi i32 [ 0, %4 ], [ 0, %8 ], [ %30, %11 ]
br label %33
33: ; preds = %1, %31
%34 = phi i32 [ %32, %31 ], [ 0, %1 ]
ret i32 %34
}