#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 sscanf(char *, char *, unsigned int *, ...);
int /*<<< orphan*/ strspn(char *, char *);
int asc2ipaddr(char *str, unsigned *add) {
  int rc;
  unsigned vals[4];
  str += strspn(str, "0xX");
  rc = sscanf(str, "%u.%u.%u.%u", vals, vals + 1, vals + 2, vals + 3);
  switch (rc) {
  case 0:
  case 1:
    rc = sscanf(str, "%x", vals);
    if (rc != 1)
      break;
    *add = vals[0];
    return 0;
  default:
    break;
  case 4:
    *add = (vals[0] & 0xff) << 24 | (vals[1] & 0xff) << 16 |
           (vals[2] & 0xff) << 8 | (vals[3] & 0xff);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<asc2ipaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xec5(%rip),%rsi
call   1030 <strspn@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2c(%rbp),%rcx
lea    -0x28(%rbp),%r8
lea    -0x24(%rbp),%r9
lea    0xe9d(%rip),%rsi
xor    %eax,%eax
lea    -0x30(%rbp),%rdx
call   1040 <sscanf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x2,%eax
jb     119a <asc2ipaddr+0x7a>
jmp    1189 <asc2ipaddr+0x69>
mov    -0x34(%rbp),%eax
sub    $0x4,%eax
je     11dc <asc2ipaddr+0xbc>
jmp    11d7 <asc2ipaddr+0xb7>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1040 <sscanf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
je     11c2 <asc2ipaddr+0xa2>
jmp    121e <asc2ipaddr+0xfe>
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1225 <asc2ipaddr+0x105>
jmp    121e <asc2ipaddr+0xfe>
mov    -0x30(%rbp),%ecx
and    $0xff,%ecx
shl    $0x18,%ecx
mov    -0x2c(%rbp),%eax
and    $0xff,%eax
shl    $0x10,%eax
or     %eax,%ecx
mov    -0x28(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
or     %eax,%ecx
mov    -0x24(%rbp),%eax
and    $0xff,%eax
or     %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1225 <asc2ipaddr+0x105>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<asc2ipaddr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xecb(%rip),%rsi
call   1030 <strspn@plt>
movslq %eax,%rbx
add    %rbp,%rbx
lea    0x4(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
lea    0xeae(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sscanf@plt>
mov    $0xffffffff,%ebp
cmp    $0x2,%eax
jae    118b <asc2ipaddr+0x6b>
lea    0xe9c(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11ba <asc2ipaddr+0x9a>
mov    (%rsp),%eax
jmp    11b5 <asc2ipaddr+0x95>
cmp    $0x4,%eax
jne    11ba <asc2ipaddr+0x9a>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
shl    $0x18,%eax
movzbl %cl,%ecx
shl    $0x10,%ecx
or     %eax,%ecx
mov    0x8(%rsp),%eax
shl    $0x8,%eax
movzwl %ax,%edx
or     %ecx,%edx
movzbl 0xc(%rsp),%eax
or     %edx,%eax
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"0xX\00"
@.str.1 = [12 x i8] c"%u.%u.%u.%u\00"
@.str.2 = [3 x i8] c"%x\00"
define i32 @asc2ipaddr(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca [4 x i32]
%4 = bitcast [4 x i32]* %3 to i8*
%5 = tail call i32 @strspn(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%11 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
%12 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %8, i32* %9, i32* %10, i32* %11)
switch i32 %12, label %34 [
i32 0, label %13
i32 1, label %13
i32 4, label %18
]
13: ; preds = %2, %2
%14 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = load i32, i32* %8
br label %32
18: ; preds = %2
%19 = load i32, i32* %8
%20 = shl i32 %19, 24
%21 = load i32, i32* %9
%22 = shl i32 %21, 16
%23 = and i32 %22, 16711680
%24 = or i32 %23, %20
%25 = load i32, i32* %10
%26 = shl i32 %25, 8
%27 = and i32 %26, 65280
%28 = or i32 %24, %27
%29 = load i32, i32* %11
%30 = and i32 %29, 255
%31 = or i32 %28, %30
br label %32
32: ; preds = %16, %18
%33 = phi i32 [ %31, %18 ], [ %17, %16 ]
store i32 %33, i32* %1
br label %34
34: ; preds = %32, %13, %2
%35 = phi i32 [ -1, %2 ], [ -1, %13 ], [ 0, %32 ]
ret i32 %35
}