#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 + (pch - digits);
      if (new > 255)
        return 0;
      *tp = 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     12a3 <inet_pton4+0x183>
mov    -0x24(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1030 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     1233 <inet_pton4+0x113>
mov    -0x40(%rbp),%rax
imul   $0xa,(%rax),%eax
cltq
mov    -0x48(%rbp),%rcx
lea    0xe3a(%rip),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %eax,-0x4c(%rbp)
cmpl   $0xff,-0x4c(%rbp)
jle    11ef <inet_pton4+0xcf>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e4 <inet_pton4+0x1c4>
mov    -0x4c(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x1c(%rbp)
jne    122e <inet_pton4+0x10e>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x4,%eax
jle    1227 <inet_pton4+0x107>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e4 <inet_pton4+0x1c4>
movl   $0x1,-0x1c(%rbp)
jmp    129e <inet_pton4+0x17e>
cmpl   $0x2e,-0x24(%rbp)
jne    1286 <inet_pton4+0x166>
cmpl   $0x0,-0x1c(%rbp)
je     1286 <inet_pton4+0x166>
cmpl   $0x4,-0x20(%rbp)
jne    1264 <inet_pton4+0x144>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e4 <inet_pton4+0x1c4>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x40(%rbp)
movl   $0x0,0x4(%rax)
movl   $0x0,-0x1c(%rbp)
jmp    1299 <inet_pton4+0x179>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e4 <inet_pton4+0x1c4>
jmp    129e <inet_pton4+0x17e>
jmp    1177 <inet_pton4+0x57>
cmpl   $0x4,-0x20(%rbp)
jge    12c0 <inet_pton4+0x1a0>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12e4 <inet_pton4+0x1c4>
mov    -0x58(%rbp),%rsi
mov    -0x18(%rbp),%rdi
mov    0x2d09(%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 = %69, %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 %70
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 %54
29: ; preds = %25
%30 = load i32*, i32** %11
%31 = load i32, i32* %30
%32 = mul nsw i32 %31, 10
%33 = sext i32 %32 to i64
%34 = load i8*, i8** %12
%35 = ptrtoint i8* %34 to i64
%36 = sub i64 %35, ptrtoint ([11 x i8]* @inet_pton4.digits to i64)
%37 = add nsw i64 %33, %36
%38 = trunc i64 %37 to i32
store i32 %38, i32* %13
%39 = load i32, i32* %13
%40 = icmp sgt i32 %39, 255
br i1 %40, label %41, label %42
41: ; preds = %29
store i32 0, i32* %3
store i32 1, i32* %14
br label %78
42: ; preds = %29
%43 = load i32, i32* %13
%44 = load i32*, i32** %11
store i32 %43, i32* %44
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
br i1 %46, label %53, label %47
47: ; preds = %42
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
%50 = icmp sgt i32 %49, 4
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 0, i32* %3
store i32 1, i32* %14
br label %78
52: ; preds = %47
store i32 1, i32* %6
br label %53
53: ; preds = %52, %42
br label %69
54: ; preds = %25
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, 46
br i1 %56, label %57, label %67
57: ; preds = %54
%58 = load i32, i32* %6
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %67
60: ; preds = %57
%61 = load i32, i32* %7
%62 = icmp eq i32 %61, 4
br i1 %62, label %63, label %64
63: ; preds = %60
store i32 0, i32* %3
store i32 1, i32* %14
br label %78
64: ; preds = %60
%65 = load i32*, i32** %11
%66 = getelementptr inbounds i32, i32* %65, i32 1
store i32* %66, i32** %11
store i32 0, i32* %66
store i32 0, i32* %6
br label %68
67: ; preds = %57, %54
store i32 0, i32* %3
store i32 1, i32* %14
br label %78
68: ; preds = %64
br label %69
69: ; preds = %68, %53
br label %19
70: ; preds = %19
%71 = load i32, i32* %7
%72 = icmp slt i32 %71, 4
br i1 %72, label %73, label %74
73: ; preds = %70
store i32 0, i32* %3
store i32 1, i32* %14
br label %78
74: ; preds = %70
%75 = load i32*, i32** %5
%76 = load i32, i32* @NS_INADDRSZ
%77 = call i32 @memcpy(i32* %75, i32* %18, i32 %76)
store i32 1, i32* %3
store i32 1, i32* %14
br label %78
78: ; preds = %74, %73, %67, %63, %51, %41
%79 = load i8*, i8** %9
%80 = load i32, i32* %3
ret i32 %80
}

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 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
}