#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 u_int;
typedef int u_char;

/* Variables and functions */
int NS_INADDRSZ;
int /*<<< orphan*/ memcpy(int *, int *, int);
char *strchr(char const *, int);
int inet_pton4(const char *src, u_char *dst) {
  static const char digits[] = "0123456789";
  int saw_digit, octets, ch;
  u_char tmp[NS_INADDRSZ], *tp;
  saw_digit = 0;
  octets = 0;
  *(tp = tmp) = 0;
  while ((ch = *src++) != '\0') {
    const char *pch;
    if ((pch = strchr(digits, ch)) != NULL) {
      u_int new = *tp * 10 + (u_int)(pch - digits);
      if (new > 255)
        return (0);
      *tp = (u_char) new;
      if (!saw_digit) {
        if (++octets > 4)
          return (0);
        saw_digit = 1;
      }
    } else if (ch == '.' && saw_digit) {
      if (octets == 4)
        return (0);
      *++tp = 0;
      saw_digit = 0;
    } else
      return (0);
  }
  if (octets < 4)
    return (0);
  memcpy(dst, tmp, NS_INADDRSZ);
  return (1);
}


x86 assembly (clang, -O0):
<inet_pton4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
lea    0xf(,%rcx,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x58(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x38(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    %rax,-0x40(%rbp)
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     12a0 <inet_pton4+0x180>
mov    -0x24(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1030 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     1230 <inet_pton4+0x110>
mov    -0x40(%rbp),%rax
imul   $0xa,(%rax),%eax
mov    -0x48(%rbp),%rcx
lea    0xe3c(%rip),%rdx
sub    %rdx,%rcx
add    %ecx,%eax
mov    %eax,-0x4c(%rbp)
cmpl   $0xff,-0x4c(%rbp)
jle    11ec <inet_pton4+0xcc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e1 <inet_pton4+0x1c1>
mov    -0x4c(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x1c(%rbp)
jne    122b <inet_pton4+0x10b>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x4,%eax
jle    1224 <inet_pton4+0x104>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e1 <inet_pton4+0x1c1>
movl   $0x1,-0x1c(%rbp)
jmp    129b <inet_pton4+0x17b>
cmpl   $0x2e,-0x24(%rbp)
jne    1283 <inet_pton4+0x163>
cmpl   $0x0,-0x1c(%rbp)
je     1283 <inet_pton4+0x163>
cmpl   $0x4,-0x20(%rbp)
jne    1261 <inet_pton4+0x141>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e1 <inet_pton4+0x1c1>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x40(%rbp)
movl   $0x0,0x4(%rax)
movl   $0x0,-0x1c(%rbp)
jmp    1296 <inet_pton4+0x176>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e1 <inet_pton4+0x1c1>
jmp    129b <inet_pton4+0x17b>
jmp    1177 <inet_pton4+0x57>
cmpl   $0x4,-0x20(%rbp)
jge    12bd <inet_pton4+0x19d>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e1 <inet_pton4+0x1c1>
mov    -0x58(%rbp),%rsi
mov    -0x18(%rbp),%rdi
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_pton4>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rcx,%rdx
sub    %rax,%rdx
mov    %rdx,%rsp
neg    %rax
movl   $0x0,(%rcx,%rax,1)
mov    (%rdi),%r12b
test   %r12b,%r12b
je     1239 <inet_pton4+0x119>
mov    %rdi,%rbx
mov    %rsi,-0x48(%rbp)
add    $0x1,%rbx
xor    %r14d,%r14d
lea    0xe83(%rip),%rdi
mov    %rdx,-0x40(%rbp)
mov    %rdx,%r15
mov    %rdx,-0x38(%rbp)
xor    %r13d,%r13d
movl   $0x0,-0x2c(%rbp)
jmp    11cb <inet_pton4+0xab>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    -0x38(%rbp),%rax
movl   $0x0,0x4(%rax)
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
xor    %r14d,%r14d
xor    %ecx,%ecx
movzbl (%rbx),%r12d
add    $0x1,%rbx
mov    %ecx,%r13d
test   %r12b,%r12b
je     123d <inet_pton4+0x11d>
movsbl %r12b,%esi
mov    $0xb,%edx
mov    %rdi,%r15
call   1030 <memchr@plt>
test   %rax,%rax
je     1220 <inet_pton4+0x100>
lea    (%r14,%r14,4),%ecx
sub    %r15d,%eax
lea    (%rax,%rcx,2),%r14d
xor    %eax,%eax
cmp    $0xff,%r14d
jg     1260 <inet_pton4+0x140>
mov    %r15,%rdi
mov    -0x38(%rbp),%rcx
mov    %r14d,(%rcx)
mov    $0x1,%ecx
test   %r13d,%r13d
jne    11bb <inet_pton4+0x9b>
mov    -0x2c(%rbp),%edx
cmp    $0x3,%edx
jg     1260 <inet_pton4+0x140>
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
jmp    11bb <inet_pton4+0x9b>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
test   %r13d,%r13d
je     1260 <inet_pton4+0x140>
cmp    $0x2e,%r12b
jne    1260 <inet_pton4+0x140>
cmpl   $0x4,-0x2c(%rbp)
jne    11a0 <inet_pton4+0x80>
jmp    1260 <inet_pton4+0x140>
xor    %eax,%eax
jmp    1260 <inet_pton4+0x140>
xor    %eax,%eax
cmpl   $0x4,-0x2c(%rbp)
jl     1260 <inet_pton4+0x140>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1040 <memcpy@plt>
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@inet_pton4.digits = internal constant [11 x i8] c"0123456789\00"
@NS_INADDRSZ = global i32 0
define i32 @inet_pton4(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%15 = load i32, i32* @NS_INADDRSZ
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %9
%18 = alloca i32, i64 %16
store i64 %16, i64* %10
store i32 0, i32* %6
store i32 0, i32* %7
store i32* %18, i32** %11
store i32 0, i32* %18
br label %19
19: ; preds = %68, %2
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
%22 = load i8, i8* %20
%23 = sext i8 %22 to i32
store i32 %23, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %69
25: ; preds = %19
%26 = load i32, i32* %8
%27 = call i8* @strchr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @inet_pton4.digits, i64 0, i64 0), i32 %26)
store i8* %27, i8** %12
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %53
29: ; preds = %25
%30 = load i32*, i32** %11
%31 = load i32, i32* %30
%32 = mul nsw i32 %31, 10
%33 = load i8*, i8** %12
%34 = ptrtoint i8* %33 to i64
%35 = sub i64 %34, ptrtoint ([11 x i8]* @inet_pton4.digits to i64)
%36 = trunc i64 %35 to i32
%37 = add nsw i32 %32, %36
store i32 %37, i32* %13
%38 = load i32, i32* %13
%39 = icmp sgt i32 %38, 255
br i1 %39, label %40, label %41
40: ; preds = %29
store i32 0, i32* %3
store i32 1, i32* %14
br label %77
41: ; preds = %29
%42 = load i32, i32* %13
%43 = load i32*, i32** %11
store i32 %42, i32* %43
%44 = load i32, i32* %6
%45 = icmp ne i32 %44, 0
br i1 %45, label %52, label %46
46: ; preds = %41
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
%49 = icmp sgt i32 %48, 4
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 0, i32* %3
store i32 1, i32* %14
br label %77
51: ; preds = %46
store i32 1, i32* %6
br label %52
52: ; preds = %51, %41
br label %68
53: ; preds = %25
%54 = load i32, i32* %8
%55 = icmp eq i32 %54, 46
br i1 %55, label %56, label %66
56: ; preds = %53
%57 = load i32, i32* %6
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %66
59: ; preds = %56
%60 = load i32, i32* %7
%61 = icmp eq i32 %60, 4
br i1 %61, label %62, label %63
62: ; preds = %59
store i32 0, i32* %3
store i32 1, i32* %14
br label %77
63: ; preds = %59
%64 = load i32*, i32** %11
%65 = getelementptr inbounds i32, i32* %64, i32 1
store i32* %65, i32** %11
store i32 0, i32* %65
store i32 0, i32* %6
br label %67
66: ; preds = %56, %53
store i32 0, i32* %3
store i32 1, i32* %14
br label %77
67: ; preds = %63
br label %68
68: ; preds = %67, %52
br label %19
69: ; preds = %19
%70 = load i32, i32* %7
%71 = icmp slt i32 %70, 4
br i1 %71, label %72, label %73
72: ; preds = %69
store i32 0, i32* %3
store i32 1, i32* %14
br label %77
73: ; preds = %69
%74 = load i32*, i32** %5
%75 = load i32, i32* @NS_INADDRSZ
%76 = call i32 @memcpy(i32* %74, i32* %18, i32 %75)
store i32 1, i32* %3
store i32 1, i32* %14
br label %77
77: ; preds = %73, %72, %66, %62, %50, %40
%78 = load i8*, i8** %9
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
@inet_pton4.digits = internal constant [11 x i8] c"0123456789\00"
@NS_INADDRSZ = global i32 0
define i32 @inet_pton4(i8* nocapture %0, i32* %1) {
%3 = load i32, i32* @NS_INADDRSZ
%4 = zext i32 %3 to i64
%5 = alloca i32, i64 %4
store i32 0, i32* %5
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %51, label %8
8: ; preds = %2, %39
%9 = phi i32 [ %40, %39 ], [ 0, %2 ]
%10 = phi i8 [ %44, %39 ], [ %6, %2 ]
%11 = phi i8* [ %15, %39 ], [ %0, %2 ]
%12 = phi i32* [ %43, %39 ], [ %5, %2 ]
%13 = phi i32 [ %42, %39 ], [ 0, %2 ]
%14 = phi i32 [ %41, %39 ], [ 0, %2 ]
%15 = getelementptr inbounds i8, i8* %11, i64 1
%16 = sext i8 %10 to i32
%17 = tail call i8* @memchr(i8* dereferenceable(11) getelementptr inbounds ([11 x i8], [11 x i8]* @inet_pton4.digits, i64 0, i64 0), i32 %16, i64 11)
%18 = icmp eq i8* %17, null
br i1 %18, label %31, label %19
19: ; preds = %8
%20 = mul nsw i32 %9, 10
%21 = ptrtoint i8* %17 to i64
%22 = trunc i64 %21 to i32
%23 = sub i32 %22, ptrtoint ([11 x i8]* @inet_pton4.digits to i32)
%24 = add nsw i32 %20, %23
%25 = icmp sgt i32 %24, 255
br i1 %25, label %51, label %26
26: ; preds = %19
store i32 %24, i32* %12
%27 = icmp eq i32 %13, 0
br i1 %27, label %28, label %39
28: ; preds = %26
%29 = add nsw i32 %14, 1
%30 = icmp sgt i32 %14, 3
br i1 %30, label %51, label %39
31: ; preds = %8
%32 = icmp ne i8 %10, 46
%33 = icmp eq i32 %13, 0
%34 = or i1 %33, %32
%35 = icmp eq i32 %14, 4
%36 = select i1 %34, i1 true, i1 %35
br i1 %36, label %51, label %37
37: ; preds = %31
%38 = getelementptr inbounds i32, i32* %12, i64 1
store i32 0, i32* %38
br label %39
39: ; preds = %37, %26, %28
%40 = phi i32 [ 0, %37 ], [ %24, %26 ], [ %24, %28 ]
%41 = phi i32 [ %14, %37 ], [ %14, %26 ], [ %29, %28 ]
%42 = phi i32 [ 0, %37 ], [ 1, %26 ], [ 1, %28 ]
%43 = phi i32* [ %38, %37 ], [ %12, %26 ], [ %12, %28 ]
%44 = load i8, i8* %15
%45 = icmp eq i8 %44, 0
br i1 %45, label %46, label %8
46: ; preds = %39
%47 = icmp slt i32 %41, 4
br i1 %47, label %51, label %48
48: ; preds = %46
%49 = load i32, i32* @NS_INADDRSZ
%50 = call i32 @memcpy(i32* %1, i32* %5, i32 %49)
br label %51
51: ; preds = %28, %19, %31, %2, %46, %48
%52 = phi i32 [ 1, %48 ], [ 0, %46 ], [ 0, %2 ], [ 0, %31 ], [ 0, %19 ], [ 0, %28 ]
ret i32 %52
}