#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 */
scalar_t__ atoi(char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strtok(char *, char *);
uint32_t addr_to_bin(const char *addr) {
  uint32_t from = 0;
  char *buffer = malloc((strlen(addr) + 1) * sizeof(char));
  char *tmp = NULL;
  uint32_t t = 0;
  strcpy(buffer, addr);
  tmp = buffer;
  tmp = strtok(tmp, ".");
  while (tmp != NULL) {
    t = (uint32_t)atoi(tmp);
    from <<= 8;
    from |= t;
    tmp = strtok(NULL, ".");
  }
  free(buffer);
  return from;
}


x86 assembly (clang, -O0):
<addr_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     120a <addr_to_bin+0xaa>
mov    -0x20(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0xc(%rbp)
mov    -0x24(%rbp),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe04(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11c8 <addr_to_bin+0x68>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr_to_bin>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe76(%rip),%rsi
mov    %r14,%rdi
call   1070 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11bc <addr_to_bin+0x5c>
lea    0xe60(%rip),%r15
mov    %rax,%rdi
call   1080 <atoi@plt>
shl    $0x8,%ebx
or     %eax,%ebx
xor    %edi,%edi
mov    %r15,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
jne    11a0 <addr_to_bin+0x40>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i32 @addr_to_bin(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = mul i64 %10, 1
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %4
store i8* null, i8** %5
store i32 0, i32* %6
%14 = load i8*, i8** %4
%15 = load i8*, i8** %2
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = load i8*, i8** %4
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = call i8* @strtok(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %5
br label %20
20: ; preds = %23, %1
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = call i64 @atoi(i8* %24)
%26 = trunc i64 %25 to i32
store i32 %26, i32* %6
%27 = load i32, i32* %3
%28 = shl i32 %27, 8
store i32 %28, i32* %3
%29 = load i32, i32* %6
%30 = load i32, i32* %3
%31 = or i32 %30, %29
store i32 %31, i32* %3
%32 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %32, i8** %5
br label %20
33: ; preds = %20
%34 = load i8*, i8** %4
%35 = call i32 @free(i8* %34)
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i32 @addr_to_bin(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = tail call i32 @strcpy(i8* %4, i8* %0)
%6 = tail call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %17, label %8
8: ; preds = %1, %8
%9 = phi i8* [ %15, %8 ], [ %6, %1 ]
%10 = phi i32 [ %14, %8 ], [ 0, %1 ]
%11 = tail call i64 @atoi(i8* %9)
%12 = trunc i64 %11 to i32
%13 = shl i32 %10, 8
%14 = or i32 %13, %12
%15 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %8
17: ; preds = %8, %1
%18 = phi i32 [ 0, %1 ], [ %14, %8 ]
%19 = tail call i32 @free(i8* %4)
ret i32 %18
}