#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 */
typedef int /*<<< orphan*/ regex_t;
typedef int /*<<< orphan*/ msgbuf;

/* Variables and functions */
int /*<<< orphan*/ REG_EXTENDED;
int REG_NOMATCH;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int regcomp(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ regerror(int, int /*<<< orphan*/ *, char *, int);
int regexec(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/,
            int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int is_valid_ip(const char *addr) {
  char *regex_src = "^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\."
                    "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\."
                    "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\."
                    "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$";
  regex_t regex;
  int reti;
  char msgbuf[100];
  int result;
  reti = regcomp(&regex, regex_src, REG_EXTENDED);
  if (reti) {
    fprintf(stderr, "Could not compile regex\n");
    exit(1);
  }
  reti = regexec(&regex, addr, 0, NULL, 0);
  if (!reti)
    result = 1;
  else if (reti == REG_NOMATCH)
    result = 0;
  else {
    regerror(reti, &regex, msgbuf, sizeof(msgbuf));
    fprintf(stderr, "Regex match failed: %s\n", msgbuf);
    exit(1);
  }
  regfree(&regex);
  return result;
}


x86 assembly (clang, -O0):
<is_valid_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rdi
call   1040 <regcomp@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11be <is_valid_ip+0x5e>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xf10(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rsi
lea    -0x14(%rbp),%rdi
xor    %r8d,%r8d
xor    %eax,%eax
mov    %eax,%ecx
mov    %r8d,%edx
call   1070 <regexec@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11f1 <is_valid_ip+0x91>
movl   $0x1,-0x84(%rbp)
jmp    1251 <is_valid_ip+0xf1>
mov    -0x18(%rbp),%eax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%eax
jne    1212 <is_valid_ip+0xb2>
movl   $0x0,-0x84(%rbp)
jmp    124c <is_valid_ip+0xec>
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rdx
lea    -0x14(%rbp),%rsi
mov    $0x64,%ecx
call   1060 <regerror@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rdx
lea    0xe9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1251 <is_valid_ip+0xf1>
lea    -0x14(%rbp),%rdi
call   1030 <regfree@plt>
mov    -0x84(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_ip>:
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rdi,%rbx
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
lea    0xe86(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1040 <regcomp@plt>
test   %eax,%eax
jne    1206 <is_valid_ip+0xa6>
xor    %r14d,%r14d
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <regexec@plt>
test   %eax,%eax
je     11eb <is_valid_ip+0x8b>
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%eax
je     11f1 <is_valid_ip+0x91>
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rdx
mov    $0x64,%ecx
call   1060 <regerror@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xeff(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x1,%r14d
lea    0xc(%rsp),%rdi
call   1030 <regfree@plt>
mov    %r14d,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xea7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [189 x i8] c"^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$\00"
@REG_EXTENDED = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not compile regex\0A\00"
@REG_NOMATCH = global i32 0
@.str.2 = [24 x i8] c"Regex match failed: %s\0A\00"
define i32 @is_valid_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca i32
store i8* %0, i8** %2
store i8* getelementptr inbounds ([189 x i8], [189 x i8]* @.str, i64 0, i64 0), i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @REG_EXTENDED
%10 = call i32 @regcomp(i32* %4, i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = load i8*, i8** %2
%19 = call i32 @regexec(i32* %4, i8* %18, i32 0, i32* null, i32 0)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %17
store i32 1, i32* %7
br label %37
23: ; preds = %17
%24 = load i32, i32* %5
%25 = load i32, i32* @REG_NOMATCH
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 0, i32* %7
br label %36
28: ; preds = %23
%29 = load i32, i32* %5
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%31 = call i32 @regerror(i32 %29, i32* %4, i8* %30, i32 100)
%32 = load i32, i32* @stderr
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
br label %37
37: ; preds = %36, %22
%38 = call i32 @regfree(i32* %4)
%39 = load i32, i32* %7
ret i32 %39
}

LLVM IR (-O2):
@.str = [189 x i8] c"^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$\00"
@REG_EXTENDED = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not compile regex\0A\00"
@REG_NOMATCH = global i32 0
@.str.2 = [24 x i8] c"Regex match failed: %s\0A\00"
define i32 @is_valid_ip(i8* %0) {
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @REG_EXTENDED
%7 = call i32 @regcomp(i32* %2, i8* getelementptr inbounds ([189 x i8], [189 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = call i32 @regexec(i32* %2, i8* %0, i32 0, i32* null, i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = load i32, i32* @REG_NOMATCH
%18 = icmp eq i32 %14, %17
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = call i32 @regerror(i32 %14, i32* %2, i8* %5, i32 100)
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %16, %13
%25 = phi i32 [ 1, %13 ], [ 0, %16 ]
%26 = call i32 @regfree(i32* %2)
ret i32 %25
}