#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok_csv(char *, char *, char **);
int strtoul(char *, int /*<<< orphan*/ *, int);
uint32_t str2ipaddr(char *str) {
  uint32_t ipaddr = 0;
  char buffer[17];
  char *s, *r;
  int i;
  strncpy(buffer, str, 17);
  buffer[16] = 0;
  s = strtok_csv(buffer, ".", &r);
  for (i = 3; i >= 0; --i) {
    if (s == NULL) {
      return 0x00000000;
    }
    ipaddr += (strtoul(s, NULL, 10) % 255) << (i * 8);
    s = strtok_csv(NULL, ".", &r);
  }
  return ipaddr;
}


x86 assembly (clang, -O0):
<str2ipaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x11,%edx
call   1040 <strncpy@plt>
movb   $0x0,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe9c(%rip),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <strtok_csv@plt>
mov    %rax,-0x38(%rbp)
movl   $0x3,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jl     11e9 <str2ipaddr+0xb9>
cmpq   $0x0,-0x38(%rbp)
jne    1199 <str2ipaddr+0x69>
movl   $0x0,-0x4(%rbp)
jmp    11ef <str2ipaddr+0xbf>
mov    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    -0x44(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe32(%rip),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <strtok_csv@plt>
mov    %rax,-0x38(%rbp)
mov    -0x44(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
jmp    1178 <str2ipaddr+0x48>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2ipaddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x11,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x20(%rsp)
lea    0xea5(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1030 <strtok_csv@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     12b5 <str2ipaddr+0x185>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %eax,%r14d
lea    0xe74(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1030 <strtok_csv@plt>
test   %rax,%rax
je     12b5 <str2ipaddr+0x185>
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %eax,%r15d
lea    0xe44(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1030 <strtok_csv@plt>
test   %rax,%rax
je     12b5 <str2ipaddr+0x185>
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %eax,%ebp
lea    0xe15(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1030 <strtok_csv@plt>
test   %rax,%rax
je     12b5 <str2ipaddr+0x185>
movslq %ebp,%rdx
imul   $0xffffffff80808081,%rdx,%rcx
shr    $0x20,%rcx
add    %edx,%ecx
mov    %ecx,%esi
shr    $0x1f,%esi
shr    $0x7,%ecx
add    %esi,%ecx
mov    %ecx,%esi
shl    $0x8,%esi
sub    %esi,%ecx
add    %edx,%ecx
shl    $0x8,%ecx
movslq %r15d,%rsi
imul   $0xffffffff80808081,%rsi,%rdx
shr    $0x20,%rdx
add    %esi,%edx
mov    %edx,%edi
shr    $0x1f,%edi
shr    $0x7,%edx
add    %edi,%edx
mov    %edx,%edi
shl    $0x8,%edi
sub    %edi,%edx
add    %esi,%edx
shl    $0x10,%edx
movslq %r14d,%rsi
imul   $0xffffffff80808081,%rsi,%rbp
shr    $0x20,%rbp
add    %esi,%ebp
mov    %ebp,%edi
shr    $0x1f,%edi
shr    $0x7,%ebp
add    %edi,%ebp
add    %esi,%ebp
shl    $0x18,%ebp
add    %edx,%ebp
add    %ecx,%ebp
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoul@plt>
cltq
imul   $0xffffffff80808081,%rax,%rbx
shr    $0x20,%rbx
add    %eax,%ebx
mov    %ebx,%ecx
shr    $0x1f,%ecx
sar    $0x7,%ebx
add    %ecx,%ebx
mov    %ebx,%ecx
shl    $0x8,%ecx
sub    %ecx,%ebx
add    %eax,%ebx
add    %ebp,%ebx
lea    0xd57(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1030 <strtok_csv@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i32 @str2ipaddr(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [17 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%9 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @strncpy(i8* %9, i8* %10, i32 17)
%12 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 16
store i8 0, i8* %12
%13 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%14 = call i8* @strtok_csv(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %14, i8** %6
store i32 3, i32* %8
br label %15
15: ; preds = %32, %1
%16 = load i32, i32* %8
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %35
18: ; preds = %15
%19 = load i8*, i8** %6
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 0, i32* %2
br label %37
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = call i32 @strtoul(i8* %23, i32* null, i32 10)
%25 = srem i32 %24, 255
%26 = load i32, i32* %8
%27 = mul nsw i32 %26, 8
%28 = shl i32 %25, %27
%29 = load i32, i32* %4
%30 = add nsw i32 %29, %28
store i32 %30, i32* %4
%31 = call i8* @strtok_csv(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %31, i8** %6
br label %32
32: ; preds = %22
%33 = load i32, i32* %8
%34 = add nsw i32 %33, -1
store i32 %34, i32* %8
br label %15
35: ; preds = %15
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %21
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i32 @str2ipaddr(i8* %0) {
%2 = alloca [17 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
%6 = call i32 @strncpy(i8* %4, i8* %0, i32 17)
%7 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 16
store i8 0, i8* %7
%8 = call i8* @strtok_csv(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%9 = icmp eq i8* %8, null
br i1 %9, label %35, label %10
10: ; preds = %1
%11 = call i32 @strtoul(i8* nocapture %8, i32* null, i32 10)
%12 = call i8* @strtok_csv(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%13 = icmp eq i8* %12, null
br i1 %13, label %35, label %14
14: ; preds = %10
%15 = call i32 @strtoul(i8* nocapture %12, i32* null, i32 10)
%16 = call i8* @strtok_csv(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%17 = icmp eq i8* %16, null
br i1 %17, label %35, label %18
18: ; preds = %14
%19 = call i32 @strtoul(i8* nocapture %16, i32* null, i32 10)
%20 = call i8* @strtok_csv(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%21 = icmp eq i8* %20, null
br i1 %21, label %35, label %22
22: ; preds = %18
%23 = srem i32 %19, 255
%24 = shl nsw i32 %23, 8
%25 = srem i32 %15, 255
%26 = shl nsw i32 %25, 16
%27 = srem i32 %11, 255
%28 = shl i32 %27, 24
%29 = add nsw i32 %26, %28
%30 = add nsw i32 %24, %29
%31 = call i32 @strtoul(i8* nocapture %20, i32* null, i32 10)
%32 = srem i32 %31, 255
%33 = add nsw i32 %32, %30
%34 = call i8* @strtok_csv(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
br label %35
35: ; preds = %22, %18, %14, %10, %1
%36 = phi i32 [ 0, %1 ], [ 0, %10 ], [ 0, %14 ], [ 0, %18 ], [ %33, %22 ]
ret i32 %36
}