#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*/ sprintf(char *, char *, unsigned int, unsigned int,
                           unsigned int, unsigned int);
int sscanf(char const *, char *, unsigned int *, unsigned int *, unsigned int *,
           unsigned int *);
scalar_t__ strcmp(char *, char const *);
int is_valid_ip(const char *ip_str) {
  unsigned int n1, n2, n3, n4;
  if (sscanf(ip_str, "%u.%u.%u.%u", &n1, &n2, &n3, &n4) != 4)
    return 0;
  if ((n1 != 0) && (n1 <= 255) && (n2 <= 255) && (n3 <= 255) && (n4 <= 255)) {
    char buf[64];
    sprintf(buf, "%u.%u.%u.%u", n1, n2, n3, n4);
    if (strcmp(buf, ip_str))
      return 0;
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_valid_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb9(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
lea    -0x20(%rbp),%r9
call   1040 <sscanf@plt>
cmp    $0x4,%eax
je     1171 <is_valid_ip+0x41>
movl   $0x0,-0x4(%rbp)
jmp    1203 <is_valid_ip+0xd3>
cmpl   $0x0,-0x14(%rbp)
je     11fc <is_valid_ip+0xcc>
cmpl   $0xff,-0x14(%rbp)
ja     11fc <is_valid_ip+0xcc>
cmpl   $0xff,-0x18(%rbp)
ja     11fc <is_valid_ip+0xcc>
cmpl   $0xff,-0x1c(%rbp)
ja     11fc <is_valid_ip+0xcc>
cmpl   $0xff,-0x20(%rbp)
ja     11fc <is_valid_ip+0xcc>
lea    -0x60(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
lea    0xe38(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     11f0 <is_valid_ip+0xc0>
movl   $0x0,-0x4(%rbp)
jmp    1203 <is_valid_ip+0xd3>
movl   $0x1,-0x4(%rbp)
jmp    1203 <is_valid_ip+0xd3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_ip>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%rbx
lea    0xebf(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x4(%rsp),%r8
mov    %rsp,%r9
call   1040 <sscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x4,%ecx
jne    11c1 <is_valid_ip+0x91>
mov    0xc(%rsp),%edx
lea    -0x1(%rdx),%ecx
cmp    $0xfe,%ecx
ja     11c1 <is_valid_ip+0x91>
mov    0x8(%rsp),%ecx
cmp    $0xff,%ecx
ja     11c1 <is_valid_ip+0x91>
mov    0x4(%rsp),%r8d
cmp    $0xff,%r8d
ja     11c1 <is_valid_ip+0x91>
mov    (%rsp),%r9d
cmp    $0xff,%r9d
ja     11c1 <is_valid_ip+0x91>
lea    0xe62(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i32 @is_valid_ip(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %4, i32* %5, i32* %6, i32* %7)
%11 = icmp ne i32 %10, 4
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %42
13: ; preds = %1
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %41
16: ; preds = %13
%17 = load i32, i32* %4
%18 = icmp ule i32 %17, 255
br i1 %18, label %19, label %41
19: ; preds = %16
%20 = load i32, i32* %5
%21 = icmp ule i32 %20, 255
br i1 %21, label %22, label %41
22: ; preds = %19
%23 = load i32, i32* %6
%24 = icmp ule i32 %23, 255
br i1 %24, label %25, label %41
25: ; preds = %22
%26 = load i32, i32* %7
%27 = icmp ule i32 %26, 255
br i1 %27, label %28, label %41
28: ; preds = %25
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* %4
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = load i32, i32* %7
%34 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %31, i32 %32, i32 %33)
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%36 = load i8*, i8** %3
%37 = call i64 @strcmp(i8* %35, i8* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %28
store i32 0, i32* %2
br label %42
40: ; preds = %28
store i32 1, i32* %2
br label %42
41: ; preds = %25, %22, %19, %16, %13
store i32 0, i32* %2
br label %42
42: ; preds = %41, %40, %39, %12
%43 = load i32, i32* %2
ret i32 %43
}

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