#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 */
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, char *);
char *strchr(char *, char);
int strlen(char *);
int reverse_ip(char *host, char *reverse) {
  char *period;
  int offset, len;
  printf("reversing %s\n", host);
  period = strchr(host, '.');
  if (!period) {
    len = strlen(host);
    memcpy(reverse, host, len);
    return (len);
  } else {
    len = period - host;
    offset = reverse_ip(host + len + 1, reverse);
    reverse[offset++] = '.';
    memcpy(reverse + offset, host, len);
    reverse[offset + len] = 0;
    return (offset + len);
  }
}


x86 assembly (clang, -O0):
<reverse_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe95(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b4 <reverse_ip+0x64>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1224 <reverse_ip+0xd4>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1070 <reverse_ip@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq %ecx,%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    -0x28(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_ip>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe9c(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11bf <reverse_ip+0x6f>
sub    %rbx,%rax
movslq %eax,%r15
lea    (%rbx,%r15,1),%rdi
add    $0x1,%rdi
mov    %r14,%rsi
call   1070 <reverse_ip@plt>
movslq %eax,%rbp
movb   $0x2e,(%r14,%rbp,1)
lea    (%r14,%rbp,1),%rdi
add    $0x1,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <memcpy@plt>
add    %r15d,%ebp
add    $0x1,%ebp
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
jmp    11d6 <reverse_ip+0x86>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"reversing %s\0A\00"
define i32 @reverse_ip(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %4
%12 = call i8* @strchr(i8* %11, i8 signext 46)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %23, label %15
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %8
%18 = load i8*, i8** %5
%19 = load i8*, i8** %4
%20 = load i32, i32* %8
%21 = call i32 @memcpy(i8* %18, i8* %19, i32 %20)
%22 = load i32, i32* %8
store i32 %22, i32* %3
br label %58
23: ; preds = %2
%24 = load i8*, i8** %6
%25 = load i8*, i8** %4
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
store i32 %29, i32* %8
%30 = load i8*, i8** %4
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i8*, i8** %5
%36 = call i32 @reverse_ip(i8* %34, i8* %35)
store i32 %36, i32* %7
%37 = load i8*, i8** %5
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 46, i8* %41
%42 = load i8*, i8** %5
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8*, i8** %4
%47 = load i32, i32* %8
%48 = call i32 @memcpy(i8* %45, i8* %46, i32 %47)
%49 = load i8*, i8** %5
%50 = load i32, i32* %7
%51 = load i32, i32* %8
%52 = add nsw i32 %50, %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %49, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %7
%56 = load i32, i32* %8
%57 = add nsw i32 %55, %56
store i32 %57, i32* %3
br label %58
58: ; preds = %23, %15
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [14 x i8] c"reversing %s\0A\00"
define i32 @reverse_ip(i8* %0, i8* %1) {
%3 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 46)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i32 @memcpy(i8* %1, i8* %0, i32 %7)
br label %9
9: ; preds = %6, %11
%10 = phi i32 [ %27, %11 ], [ %7, %6 ]
ret i32 %10
11: ; preds = %2
%12 = ptrtoint i8* %4 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = shl i64 %14, 32
%17 = ashr exact i64 %16, 32
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = tail call i32 @reverse_ip(i8* %19, i8* %1)
%21 = add nsw i32 %20, 1
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 46, i8* %23
%24 = sext i32 %21 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
%26 = tail call i32 @memcpy(i8* %25, i8* %0, i32 %15)
%27 = add nsw i32 %21, %15
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %1, i64 %28
store i8 0, i8* %29
br label %9
}