#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, ...);
int sscanf(char const *, char *, unsigned int *, unsigned int *, unsigned int *,
           unsigned int *);
int string_ip_subnet(const char *addr, char *subnet) {
  unsigned bytes[4];
  int fields;
  fields =
      sscanf(addr, "%u.%u.%u.%u", &bytes[0], &bytes[1], &bytes[2], &bytes[3]);
  if (fields != 4)
    return 0;
  if (bytes[0] < 128) {
    sprintf(subnet, "%u", bytes[0]);
  } else if (bytes[0] < 192) {
    sprintf(subnet, "%u.%u", bytes[0], bytes[1]);
  } else {
    sprintf(subnet, "%u.%u.%u", bytes[0], bytes[1], bytes[2]);
  }
  return 1;
}


x86 assembly (clang, -O0):
<string_ip_subnet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%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    0xea9(%rip),%rsi
call   1030 <sscanf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
je     1175 <string_ip_subnet+0x55>
movl   $0x0,-0x4(%rbp)
jmp    11ee <string_ip_subnet+0xce>
cmpl   $0x80,-0x30(%rbp)
jae    119c <string_ip_subnet+0x7c>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    11e7 <string_ip_subnet+0xc7>
cmpl   $0xc0,-0x30(%rbp)
jae    11c6 <string_ip_subnet+0xa6>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    11e2 <string_ip_subnet+0xc2>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%r8d
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    11e7 <string_ip_subnet+0xc7>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_ip_subnet>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0x4(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
lea    0xec2(%rip),%rsi
mov    %rsp,%rdx
call   1030 <sscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x4,%ecx
jne    11a4 <string_ip_subnet+0x84>
mov    (%rsp),%edx
cmp    $0x7f,%edx
ja     116a <string_ip_subnet+0x4a>
lea    0xeab(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    119f <string_ip_subnet+0x7f>
mov    0x4(%rsp),%ecx
cmp    $0xbf,%edx
ja     1189 <string_ip_subnet+0x69>
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    119f <string_ip_subnet+0x7f>
mov    0x8(%rsp),%r8d
lea    0xe6e(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"%u.%u.%u.%u\00"
@.str.1 = [3 x i8] c"%u\00"
@.str.2 = [6 x i8] c"%u.%u\00"
@.str.3 = [9 x i8] c"%u.%u.%u\00"
define i32 @string_ip_subnet(i8* nocapture %0, i8* nocapture writeonly %1) {
%3 = alloca [4 x i32]
%4 = bitcast [4 x i32]* %3 to i8*
%5 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%7 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%8 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
%9 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* %5, i32* %6, i32* %7, i32* %8)
%10 = icmp eq i32 %9, 4
br i1 %10, label %11, label %24
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp ult i32 %12, 128
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %24
16: ; preds = %11
%17 = icmp ult i32 %12, 192
%18 = load i32, i32* %6
br i1 %17, label %19, label %21
19: ; preds = %16
%20 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %18)
br label %24
21: ; preds = %16
%22 = load i32, i32* %7
%23 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %12, i32 %18, i32 %22)
br label %24
24: ; preds = %14, %21, %19, %2
%25 = phi i32 [ 0, %2 ], [ 1, %19 ], [ 1, %21 ], [ 1, %14 ]
ret i32 %25
}