#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 /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int inet_pton(int /*<<< orphan*/, char *, unsigned char **);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned long);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ipv4_to_uint(const char *p, unsigned long len, unsigned int *result) {
  unsigned char parsed[4] = {0};
  char buf[16];
  if (len > 16 - 1)
    return 0;
  memset(buf, 0, sizeof(buf));
  memcpy(buf, p, len);
  if (inet_pton(AF_INET, buf, &parsed) == 1) {
    *result =
        ((parsed[0] << 24) | (parsed[1] << 16) | (parsed[2] << 8) | parsed[3]);
    return 1;
  } else
    return 0;
}


x86 assembly (clang, -O0):
<ipv4_to_uint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
cmpq   $0xf,-0x18(%rbp)
jbe    116b <ipv4_to_uint+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    11e3 <ipv4_to_uint+0xb3>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
jne    11dc <ipv4_to_uint+0xac>
movzbl -0x24(%rbp),%ecx
shl    $0x18,%ecx
movzbl -0x23(%rbp),%eax
shl    $0x10,%eax
or     %eax,%ecx
movzbl -0x22(%rbp),%eax
shl    $0x8,%eax
or     %eax,%ecx
movzbl -0x21(%rbp),%eax
or     %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11e3 <ipv4_to_uint+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv4_to_uint>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
movl   $0x0,0xc(%rsp)
xor    %ebp,%ebp
cmp    $0xf,%rsi
ja     11a2 <ipv4_to_uint+0x72>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x10(%rsp),%r12
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %r12,%rsi
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
jne    11a2 <ipv4_to_uint+0x72>
mov    0xc(%rsp),%eax
bswap  %eax
mov    %eax,(%r14)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AF_INET = global i32 0
define i32 @ipv4_to_uint(i8* %0, i64 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca [4 x i8]
%9 = alloca [16 x i8]
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
%10 = bitcast [4 x i8]* %8 to i8*
%11 = load i64, i64* %6
%12 = icmp ugt i64 %11, 15
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 0, i32* %4
br label %47
14: ; preds = %3
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 16)
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%18 = load i8*, i8** %5
%19 = load i64, i64* %6
%20 = call i32 @memcpy(i8* %17, i8* %18, i64 %19)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%23 = bitcast [4 x i8]* %8 to i8**
%24 = call i32 @inet_pton(i32 %21, i8* %22, i8** %23)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %46
26: ; preds = %14
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = shl i32 %29, 24
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 1
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = shl i32 %33, 16
%35 = or i32 %30, %34
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 2
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = shl i32 %38, 8
%40 = or i32 %35, %39
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 3
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = or i32 %40, %43
%45 = load i32*, i32** %7
store i32 %44, i32* %45
store i32 1, i32* %4
br label %47
46: ; preds = %14
store i32 0, i32* %4
br label %47
47: ; preds = %46, %26, %13
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@AF_INET = global i32 0
define i32 @ipv4_to_uint(i8* %0, i64 %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to [4 x i8]*
%6 = alloca [16 x i8]
%7 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%9 = icmp ugt i64 %1, 15
br i1 %9, label %35, label %10
10: ; preds = %3
%11 = call i32 @memset(i8* %8, i32 0, i32 16)
%12 = call i32 @memcpy(i8* %8, i8* %0, i64 %1)
%13 = load i32, i32* @AF_INET
%14 = bitcast i32* %4 to i8**
%15 = call i32 @inet_pton(i32 %13, i8* %8, i8** %14)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %35
17: ; preds = %10
%18 = load i8, i8* %7
%19 = zext i8 %18 to i32
%20 = shl nuw i32 %19, 24
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = shl nuw nsw i32 %23, 16
%25 = or i32 %24, %20
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = shl nuw nsw i32 %28, 8
%30 = or i32 %25, %29
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = or i32 %30, %33
store i32 %34, i32* %2
br label %35
35: ; preds = %10, %3, %17
%36 = phi i32 [ 1, %17 ], [ 0, %3 ], [ 0, %10 ]
ret i32 %36
}