#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int sscanf(char const *, char *, unsigned int *, unsigned int *, unsigned int *,
           unsigned int *, unsigned int *);
int /*<<< orphan*/ stderr;
int valid_ipv4(const char *str) {
  int i;
  unsigned int a[4], plen;
  uint32_t addr;
  if (sscanf(str, "%u.%u.%u.%u/%u", &a[0], &a[1], &a[2], &a[3], &plen) != 5)
    goto bad_addr;
  addr = 0;
  for (i = 0; i < 4; i++) {
    if (a[i] > 255)
      goto bad_addr;
    addr <<= 8;
    addr |= a[i];
  }
  if (plen == 0 || plen > 32) {
    fprintf(stderr, "Invalid prefix len %d for IP\n", plen);
    return 0;
  }
  if (plen < 31) {
    uint32_t net_mask = ~0 << (32 - plen);
    uint32_t broadcast = (addr & net_mask) | (~0 & ~net_mask);
    if ((addr & net_mask) == addr) {
      fprintf(stderr, "Can not assign network address as IP address\n");
      return 0;
    }
    if (addr == broadcast) {
      fprintf(stderr, "Can not assign broadcast address as IP address\n");
      return 0;
    }
  }
  return 1;
bad_addr:
  fprintf(stderr, "Invalid IPv4 address/prefix\n");
  return 0;
}


x86 assembly (clang, -O0):
<valid_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    -0x30(%rbp),%rcx
add    $0x4,%rcx
lea    -0x30(%rbp),%r8
add    $0x8,%r8
lea    -0x30(%rbp),%r9
add    $0xc,%r9
lea    0xead(%rip),%rsi
lea    -0x34(%rbp),%rax
mov    %rax,(%rsp)
call   1040 <sscanf@plt>
cmp    $0x5,%eax
je     116e <valid_ipv4+0x4e>
jmp    12a1 <valid_ipv4+0x181>
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    11c2 <valid_ipv4+0xa2>
movslq -0x14(%rbp),%rax
cmpl   $0xff,-0x30(%rbp,%rax,4)
jbe    119d <valid_ipv4+0x7d>
jmp    12a1 <valid_ipv4+0x181>
mov    -0x38(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x38(%rbp)
movslq -0x14(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117c <valid_ipv4+0x5c>
cmpl   $0x0,-0x34(%rbp)
je     11d6 <valid_ipv4+0xb6>
cmpl   $0x20,-0x34(%rbp)
jbe    11fc <valid_ipv4+0xdc>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bf <valid_ipv4+0x19f>
cmpl   $0x1f,-0x34(%rbp)
jae    1295 <valid_ipv4+0x175>
mov    $0x20,%ecx
sub    -0x34(%rbp),%ecx
mov    $0xffffffff,%eax
shl    %cl,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
and    -0x3c(%rbp),%eax
mov    -0x3c(%rbp),%ecx
xor    $0xffffffff,%ecx
and    $0xffffffff,%ecx
or     %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%eax
and    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
jne    1261 <valid_ipv4+0x141>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bf <valid_ipv4+0x19f>
mov    -0x38(%rbp),%eax
cmp    -0x40(%rbp),%eax
jne    1290 <valid_ipv4+0x170>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bf <valid_ipv4+0x19f>
jmp    1295 <valid_ipv4+0x175>
movl   $0x1,-0x4(%rbp)
jmp    12bf <valid_ipv4+0x19f>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_ipv4>:
push   %rbx
sub    $0x30,%rsp
lea    0x14(%rsp),%rcx
lea    0x18(%rsp),%r8
lea    0x1c(%rsp),%r9
lea    0x2c(%rsp),%rax
mov    %rax,(%rsp)
lea    0xebc(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1040 <sscanf@plt>
cmp    $0x5,%eax
jne    1182 <valid_ipv4+0x62>
mov    0x10(%rsp),%ecx
cmp    $0xff,%ecx
ja     1182 <valid_ipv4+0x62>
mov    0x14(%rsp),%edi
cmp    $0xff,%edi
ja     1182 <valid_ipv4+0x62>
mov    0x18(%rsp),%esi
cmp    $0xff,%esi
ja     1182 <valid_ipv4+0x62>
mov    0x1c(%rsp),%eax
cmp    $0xff,%eax
jbe    11a3 <valid_ipv4+0x83>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xef9(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
ret
mov    0x2c(%rsp),%edx
lea    -0x21(%rdx),%ebx
cmp    $0xffffffdf,%ebx
ja     11ca <valid_ipv4+0xaa>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    119b <valid_ipv4+0x7b>
mov    $0x1,%ebx
cmp    $0x1e,%edx
ja     119b <valid_ipv4+0x7b>
shl    $0x10,%ecx
shl    $0x8,%edi
or     %ecx,%edi
or     %edi,%esi
neg    %dl
mov    $0xffffffff,%edi
mov    %edx,%ecx
shl    %cl,%edi
shl    $0x8,%esi
or     %esi,%eax
not    %edi
test   %edi,%eax
jne    1206 <valid_ipv4+0xe6>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
jmp    1192 <valid_ipv4+0x72>
not    %eax
test   %edi,%eax
jne    119b <valid_ipv4+0x7b>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
jmp    1192 <valid_ipv4+0x72>

LLVM IR (-O0):
@.str = [15 x i8] c"%u.%u.%u.%u/%u\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Invalid prefix len %d for IP\0A\00"
@.str.2 = [46 x i8] c"Can not assign network address as IP address\0A\00"
@.str.3 = [48 x i8] c"Can not assign broadcast address as IP address\0A\00"
@.str.4 = [29 x i8] c"Invalid IPv4 address/prefix\0A\00"
define i32 @valid_ipv4(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [4 x i32]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 0
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 1
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 2
%14 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 3
%15 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32* %11, i32* %12, i32* %13, i32* %14, i32* %6)
%16 = icmp ne i32 %15, 5
br i1 %16, label %17, label %18
17: ; preds = %1
br label %82
18: ; preds = %1
store i32 0, i32* %7
store i32 0, i32* %4
br label %19
19: ; preds = %38, %18
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 4
br i1 %21, label %22, label %41
22: ; preds = %19
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 %24
%26 = load i32, i32* %25
%27 = icmp ugt i32 %26, 255
br i1 %27, label %28, label %29
28: ; preds = %22
br label %82
29: ; preds = %22
%30 = load i32, i32* %7
%31 = shl i32 %30, 8
store i32 %31, i32* %7
%32 = load i32, i32* %4
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 %33
%35 = load i32, i32* %34
%36 = load i32, i32* %7
%37 = or i32 %36, %35
store i32 %37, i32* %7
br label %38
38: ; preds = %29
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %19
41: ; preds = %19
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = load i32, i32* %6
%46 = icmp ugt i32 %45, 32
br i1 %46, label %47, label %51
47: ; preds = %44, %41
%48 = load i32, i32* @stderr
%49 = load i32, i32* %6
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %49)
store i32 0, i32* %2
br label %85
51: ; preds = %44
%52 = load i32, i32* %6
%53 = icmp ult i32 %52, 31
br i1 %53, label %54, label %81
54: ; preds = %51
%55 = load i32, i32* %6
%56 = sub i32 32, %55
%57 = shl i32 -1, %56
store i32 %57, i32* %8
%58 = load i32, i32* %7
%59 = load i32, i32* %8
%60 = and i32 %58, %59
%61 = load i32, i32* %8
%62 = xor i32 %61, -1
%63 = and i32 -1, %62
%64 = or i32 %60, %63
store i32 %64, i32* %9
%65 = load i32, i32* %7
%66 = load i32, i32* %8
%67 = and i32 %65, %66
%68 = load i32, i32* %7
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %73
70: ; preds = %54
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %85
73: ; preds = %54
%74 = load i32, i32* %7
%75 = load i32, i32* %9
%76 = icmp eq i32 %74, %75
br i1 %76, label %77, label %80
77: ; preds = %73
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %85
80: ; preds = %73
br label %81
81: ; preds = %80, %51
store i32 1, i32* %2
br label %85
82: ; preds = %28, %17
%83 = load i32, i32* @stderr
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %85
85: ; preds = %82, %81, %77, %70, %47
%86 = load i32, i32* %2
ret i32 %86
}

LLVM IR (-O2):
@.str = [15 x i8] c"%u.%u.%u.%u/%u\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Invalid prefix len %d for IP\0A\00"
@.str.2 = [46 x i8] c"Can not assign network address as IP address\0A\00"
@.str.3 = [48 x i8] c"Can not assign broadcast address as IP address\0A\00"
@.str.4 = [29 x i8] c"Invalid IPv4 address/prefix\0A\00"
define i32 @valid_ipv4(i8* nocapture %0) {
%2 = alloca [4 x i32]
%3 = alloca i32
%4 = bitcast [4 x i32]* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%7 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%8 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%10 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32* %6, i32* %7, i32* %8, i32* %9, i32* %3)
%11 = icmp eq i32 %10, 5
br i1 %11, label %12, label %54
12: ; preds = %1
%13 = load i32, i32* %6
%14 = icmp ugt i32 %13, 255
br i1 %14, label %54, label %15
15: ; preds = %12
%16 = load i32, i32* %7
%17 = icmp ugt i32 %16, 255
br i1 %17, label %54, label %18
18: ; preds = %15
%19 = load i32, i32* %8
%20 = icmp ugt i32 %19, 255
br i1 %20, label %54, label %21
21: ; preds = %18
%22 = load i32, i32* %9
%23 = icmp ugt i32 %22, 255
br i1 %23, label %54, label %24
24: ; preds = %21
%25 = shl nuw nsw i32 %13, 16
%26 = shl nuw nsw i32 %16, 8
%27 = or i32 %25, %26
%28 = or i32 %19, %27
%29 = shl i32 %28, 8
%30 = or i32 %22, %29
%31 = load i32, i32* %3
%32 = add i32 %31, -33
%33 = icmp ult i32 %32, -32
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %57
37: ; preds = %24
%38 = icmp ult i32 %31, 31
br i1 %38, label %39, label %57
39: ; preds = %37
%40 = sub nuw nsw i32 32, %31
%41 = shl nsw i32 -1, %40
%42 = and i32 %41, %30
%43 = icmp eq i32 %42, %30
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
br label %57
47: ; preds = %39
%48 = xor i32 %41, -1
%49 = or i32 %30, %48
%50 = icmp eq i32 %30, %49
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
br label %57
54: ; preds = %12, %15, %18, %21, %1
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %37, %47, %44, %51, %54, %34
%58 = phi i32 [ 0, %54 ], [ 0, %34 ], [ 0, %51 ], [ 0, %44 ], [ 1, %47 ], [ 1, %37 ]
ret i32 %58
}