#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 */

/* Variables and functions */
scalar_t__ malloc(scalar_t__);
scalar_t__ strlen(char *);
char *defangIPaddr(char *address) {
  char *ret = (char *)malloc(strlen(address) + 7);
  char *p = address, *q = ret;
  while (*p != '\0') {
    if (*p == '.') {
      *q++ = '[';
      *q++ = '.';
      *q++ = ']';
    } else {
      *q++ = *p;
    }
    p++;
  }
  *q = *p;
  return ret;
}


x86 assembly (clang, -O0):
<defangIPaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x7,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d8 <defangIPaddr+0xb8>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11b0 <defangIPaddr+0x90>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5b,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x2e,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5d,(%rax)
jmp    11c7 <defangIPaddr+0xa7>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1155 <defangIPaddr+0x35>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<defangIPaddr>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x7(%rax),%rdi
call   1040 <malloc@plt>
mov    %rax,%rcx
jmp    1151 <defangIPaddr+0x31>
nopw   0x0(%rax,%rax,1)
movw   $0x2e5b,(%rcx)
movb   $0x5d,0x2(%rcx)
add    $0x3,%rcx
add    $0x1,%rbx
movzbl (%rbx),%edx
cmp    $0x2e,%dl
je     1140 <defangIPaddr+0x20>
test   %dl,%dl
je     1169 <defangIPaddr+0x49>
mov    %dl,(%rcx)
add    $0x1,%rcx
add    $0x1,%rbx
jmp    1151 <defangIPaddr+0x31>
movb   $0x0,(%rcx)
pop    %rbx
ret

LLVM IR (-O0):
define i8* @defangIPaddr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
%8 = add nsw i64 %7, 7
%9 = call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = load i8*, i8** %2
store i8* %11, i8** %4
%12 = load i8*, i8** %3
store i8* %12, i8** %5
br label %13
13: ; preds = %35, %1
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 46
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %5
store i8 91, i8* %24
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
store i8 46, i8* %26
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
store i8 93, i8* %28
br label %35
30: ; preds = %18
%31 = load i8*, i8** %4
%32 = load i8, i8* %31
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %5
store i8 %32, i8* %33
br label %35
35: ; preds = %30, %23
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %4
br label %13
38: ; preds = %13
%39 = load i8*, i8** %4
%40 = load i8, i8* %39
%41 = load i8*, i8** %5
store i8 %40, i8* %41
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
define i8* @defangIPaddr(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 7
%4 = tail call i64 @malloc(i64 %3)
%5 = inttoptr i64 %4 to i8*
br label %6
6: ; preds = %16, %1
%7 = phi i8* [ %0, %1 ], [ %18, %16 ]
%8 = phi i8* [ %5, %1 ], [ %17, %16 ]
%9 = load i8, i8* %7
switch i8 %9, label %14 [
i8 0, label %19
i8 46, label %10
]
10: ; preds = %6
%11 = getelementptr inbounds i8, i8* %8, i64 1
store i8 91, i8* %8
%12 = getelementptr inbounds i8, i8* %8, i64 2
store i8 46, i8* %11
%13 = getelementptr inbounds i8, i8* %8, i64 3
store i8 93, i8* %12
br label %16
14: ; preds = %6
%15 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %9, i8* %8
br label %16
16: ; preds = %14, %10
%17 = phi i8* [ %13, %10 ], [ %15, %14 ]
%18 = getelementptr inbounds i8, i8* %7, i64 1
br label %6
19: ; preds = %6
store i8 0, i8* %8
ret i8* %5
}