#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 /*<<< orphan*/ strcpy(char *, char *);
int submask_prefixtomask(int prefixlen, char *ip_str) {
  unsigned int ip_num = 0;
  int i = 0;
  int j = 0;
  if (ip_str == NULL)
    return -1;
  if (prefixlen > 32)
    return -1;
  if (prefixlen == 8)
    strcpy(ip_str, "255.0.0.0");
  if (prefixlen == 16)
    strcpy(ip_str, "255.255.0.0");
  if (prefixlen == 24)
    strcpy(ip_str, "255.255.255.0");
  if (prefixlen == 32)
    strcpy(ip_str, "255.255.255.255");
  for (i = prefixlen, j = 31; i > 0; i--, j--) {
    ip_num += (1 << j);
  }
  sprintf(ip_str, "%hhu.%hhu.%hhu.%hhu", (ip_num >> 24) & 0xff,
          (ip_num >> 16) & 0xff, (ip_num >> 8) & 0xff, ip_num & 0xff);
  return 0;
}


x86 assembly (clang, -O0):
<submask_prefixtomask>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115b <submask_prefixtomask+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <submask_prefixtomask+0x140>
cmpl   $0x20,-0x8(%rbp)
jle    1171 <submask_prefixtomask+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <submask_prefixtomask+0x140>
cmpl   $0x8,-0x8(%rbp)
jne    118b <submask_prefixtomask+0x6b>
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1030 <strcpy@plt>
cmpl   $0x10,-0x8(%rbp)
jne    11a5 <submask_prefixtomask+0x85>
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1030 <strcpy@plt>
cmpl   $0x18,-0x8(%rbp)
jne    11bf <submask_prefixtomask+0x9f>
mov    -0x10(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1030 <strcpy@plt>
cmpl   $0x20,-0x8(%rbp)
jne    11d9 <submask_prefixtomask+0xb9>
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1f,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1217 <submask_prefixtomask+0xf7>
mov    -0x1c(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e6 <submask_prefixtomask+0xc6>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
shr    $0x18,%edx
and    $0xff,%edx
mov    -0x14(%rbp),%ecx
shr    $0x10,%ecx
and    $0xff,%ecx
mov    -0x14(%rbp),%r8d
shr    $0x8,%r8d
and    $0xff,%r8d
mov    -0x14(%rbp),%r9d
and    $0xff,%r9d
lea    0xde0(%rip),%rsi
call   1040 <sprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<submask_prefixtomask>:
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%eax
cmp    $0x20,%edi
jg     121f <submask_prefixtomask+0xff>
mov    %rsi,%r14
test   %rsi,%rsi
je     121f <submask_prefixtomask+0xff>
mov    %edi,%ebx
lea    -0x8(%rbx),%eax
rol    $0x1d,%eax
cmp    $0x3,%eax
ja     11c0 <submask_prefixtomask+0xa0>
lea    0xeae(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xeae(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
cmp    $0x10,%ebx
je     11c8 <submask_prefixtomask+0xa8>
cmp    $0x18,%ebx
je     1194 <submask_prefixtomask+0x74>
cmp    $0x20,%ebx
je     11b1 <submask_prefixtomask+0x91>
jmp    11c0 <submask_prefixtomask+0xa0>
lea    0xe98(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
cmp    $0x20,%ebx
je     11b1 <submask_prefixtomask+0x91>
cmp    $0x18,%ebx
jne    11c0 <submask_prefixtomask+0xa0>
lea    0xe8b(%rip),%rsi
jmp    11cf <submask_prefixtomask+0xaf>
lea    0xe82(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
cmp    $0x20,%ebx
jne    11c0 <submask_prefixtomask+0xa0>
lea    0xe7c(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
test   %ebx,%ebx
jg     11d7 <submask_prefixtomask+0xb7>
xor    %eax,%eax
jmp    11f7 <submask_prefixtomask+0xd7>
lea    0xe4b(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
mov    $0x1f,%ecx
xchg   %ax,%ax
mov    $0x1,%edx
shl    %cl,%edx
add    %edx,%eax
lea    (%rbx,%rcx,1),%edx
add    $0xffffffe1,%edx
add    $0xffffffff,%ecx
cmp    $0x1,%edx
ja     11e0 <submask_prefixtomask+0xc0>
mov    %eax,%edx
shr    $0x18,%edx
mov    %eax,%ecx
shr    $0x10,%ecx
movzbl %cl,%ecx
movzbl %ah,%ebx
movzbl %al,%r9d
lea    0xe32(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%r8d
call   1040 <sprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"255.0.0.0\00"
@.str.1 = [12 x i8] c"255.255.0.0\00"
@.str.2 = [14 x i8] c"255.255.255.0\00"
@.str.3 = [16 x i8] c"255.255.255.255\00"
@.str.4 = [20 x i8] c"%hhu.%hhu.%hhu.%hhu\00"
define i32 @submask_prefixtomask(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %69
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 32
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 -1, i32* %3
br label %69
16: ; preds = %12
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 8
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i8*, i8** %5
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %16
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 16
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i8*, i8** %5
%27 = call i32 @strcpy(i8* %26, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %25, %22
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 24
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i8*, i8** %5
%33 = call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %31, %28
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, 32
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i8*, i8** %5
%39 = call i32 @strcpy(i8* %38, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %37, %34
%41 = load i32, i32* %4
store i32 %41, i32* %7
store i32 31, i32* %8
br label %42
42: ; preds = %50, %40
%43 = load i32, i32* %7
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %42
%46 = load i32, i32* %8
%47 = shl i32 1, %46
%48 = load i32, i32* %6
%49 = add i32 %48, %47
store i32 %49, i32* %6
br label %50
50: ; preds = %45
%51 = load i32, i32* %7
%52 = add nsw i32 %51, -1
store i32 %52, i32* %7
%53 = load i32, i32* %8
%54 = add nsw i32 %53, -1
store i32 %54, i32* %8
br label %42
55: ; preds = %42
%56 = load i8*, i8** %5
%57 = load i32, i32* %6
%58 = lshr i32 %57, 24
%59 = and i32 %58, 255
%60 = load i32, i32* %6
%61 = lshr i32 %60, 16
%62 = and i32 %61, 255
%63 = load i32, i32* %6
%64 = lshr i32 %63, 8
%65 = and i32 %64, 255
%66 = load i32, i32* %6
%67 = and i32 %66, 255
%68 = call i32 @sprintf(i8* %56, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %59, i32 %62, i32 %65, i32 %67)
store i32 0, i32* %3
br label %69
69: ; preds = %55, %15, %11
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str = [10 x i8] c"255.0.0.0\00"
@.str.1 = [12 x i8] c"255.255.0.0\00"
@.str.2 = [14 x i8] c"255.255.255.0\00"
@.str.3 = [16 x i8] c"255.255.255.255\00"
@.str.4 = [20 x i8] c"%hhu.%hhu.%hhu.%hhu\00"
define i32 @submask_prefixtomask(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
%4 = icmp sgt i32 %0, 32
%5 = or i1 %4, %3
br i1 %5, label %43, label %6
6: ; preds = %2
%7 = add i32 %0, -8
%8 = tail call i32 @llvm.fshl.i32(i32 %7, i32 %7, i32 29)
switch i32 %8, label %19 [
i32 0, label %9
i32 1, label %11
i32 2, label %14
i32 3, label %17
]
9: ; preds = %6
%10 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
switch i32 %0, label %19 [
i32 16, label %21
i32 24, label %13
i32 32, label %17
]
11: ; preds = %6
%12 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
switch i32 %0, label %19 [
i32 24, label %13
i32 32, label %17
]
13: ; preds = %9, %11
br label %21
14: ; preds = %6
%15 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32 %0, 32
br i1 %16, label %17, label %19
17: ; preds = %6, %9, %11, %14
%18 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %6, %9, %11, %17, %14
%20 = icmp sgt i32 %0, 0
br i1 %20, label %24, label %34
21: ; preds = %9, %13
%22 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %13 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %9 ]
%23 = tail call i32 @strcpy(i8* %1, i8* %22)
br label %24
24: ; preds = %21, %19
br label %25
25: ; preds = %24, %25
%26 = phi i32 [ %32, %25 ], [ 31, %24 ]
%27 = phi i32 [ %31, %25 ], [ %0, %24 ]
%28 = phi i32 [ %30, %25 ], [ 0, %24 ]
%29 = shl nuw i32 1, %26
%30 = add i32 %29, %28
%31 = add nsw i32 %27, -1
%32 = add nsw i32 %26, -1
%33 = icmp ugt i32 %27, 1
br i1 %33, label %25, label %34
34: ; preds = %25, %19
%35 = phi i32 [ 0, %19 ], [ %30, %25 ]
%36 = lshr i32 %35, 24
%37 = lshr i32 %35, 16
%38 = and i32 %37, 255
%39 = lshr i32 %35, 8
%40 = and i32 %39, 255
%41 = and i32 %35, 255
%42 = tail call i32 @sprintf(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %36, i32 %38, i32 %40, i32 %41)
br label %43
43: ; preds = %2, %34
%44 = phi i32 [ 0, %34 ], [ -1, %2 ]
ret i32 %44
}