#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 ip;
typedef int in_addr_t;

/* Variables and functions */
int ntohl(int);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int, int);
char *ip2str(in_addr_t ip) {
  static char buf[2][sizeof("aaa.bbb.ccc.ddd")];
  static short int index = 0;
  ip = ntohl(ip);
  sprintf(buf[index], "%d.%d.%d.%d", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
          (ip >> 8) & 0xff, (ip)&0xff);
  if (index) {
    index = 0;
    return buf[1];
  } else
    return buf[index++];
}


x86 assembly (clang, -O0):
<ip2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <ntohl@plt>
mov    %eax,-0xc(%rbp)
movswq 0x2f22(%rip),%rax
lea    0x2efb(%rip),%rdi
shl    $0x4,%rax
add    %rax,%rdi
mov    -0xc(%rbp),%edx
sar    $0x18,%edx
and    $0xff,%edx
mov    -0xc(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0xc(%rbp),%r8d
sar    $0x8,%r8d
and    $0xff,%r8d
mov    -0xc(%rbp),%r9d
and    $0xff,%r9d
lea    0xe7b(%rip),%rsi
call   1030 <sprintf@plt>
cmpw   $0x0,0x2ece(%rip)
je     11b5 <ip2str+0x95>
movw   $0x0,0x2ebf(%rip)
lea    0x2e98(%rip),%rax
add    $0x10,%rax
mov    %rax,-0x8(%rbp)
jmp    11e0 <ip2str+0xc0>
mov    0x2ea4(%rip),%ax
mov    %ax,%cx
add    $0x1,%cx
mov    %cx,0x2e96(%rip)
movswq %ax,%rcx
lea    0x2e6b(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip2str>:
push   %rbp
push   %rbx
push   %rax
call   1040 <ntohl@plt>
movzbl 0x2f31(%rip),%edi
shl    $0x4,%rdi
lea    0x2f06(%rip),%rbx
add    %rbx,%rdi
mov    %eax,%ecx
movzbl %ah,%ebp
movzbl %al,%r9d
mov    %eax,%edx
shr    $0x18,%edx
shr    $0x10,%ecx
movzbl %cl,%ecx
lea    0xea8(%rip),%rsi
mov    %ebp,%r8d
call   1030 <sprintf@plt>
mov    0x2efa(%rip),%cl
lea    0x2ee3(%rip),%rax
test   %cl,%cl
cmove  %rbx,%rax
xor    $0x1,%cl
mov    %cl,0x2ee4(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@ip2str.buf = internal global [2 x [16 x i8]] zeroinitializer
@ip2str.index = internal global i16 0
@.str = [12 x i8] c"%d.%d.%d.%d\00"
define i8* @ip2str(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = call i32 @ntohl(i32 %4)
store i32 %5, i32* %3
%6 = load i16, i16* @ip2str.index
%7 = sext i16 %6 to i64
%8 = getelementptr inbounds [2 x [16 x i8]], [2 x [16 x i8]]* @ip2str.buf, i64 0, i64 %7
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %3
%11 = ashr i32 %10, 24
%12 = and i32 %11, 255
%13 = load i32, i32* %3
%14 = ashr i32 %13, 16
%15 = and i32 %14, 255
%16 = load i32, i32* %3
%17 = ashr i32 %16, 8
%18 = and i32 %17, 255
%19 = load i32, i32* %3
%20 = and i32 %19, 255
%21 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %15, i32 %18, i32 %20)
%22 = load i16, i16* @ip2str.index
%23 = icmp ne i16 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %1
store i16 0, i16* @ip2str.index
store i8* getelementptr inbounds ([2 x [16 x i8]], [2 x [16 x i8]]* @ip2str.buf, i64 0, i64 1, i64 0), i8** %2
br label %31
25: ; preds = %1
%26 = load i16, i16* @ip2str.index
%27 = add i16 %26, 1
store i16 %27, i16* @ip2str.index
%28 = sext i16 %26 to i64
%29 = getelementptr inbounds [2 x [16 x i8]], [2 x [16 x i8]]* @ip2str.buf, i64 0, i64 %28
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %29, i64 0, i64 0
store i8* %30, i8** %2
br label %31
31: ; preds = %25, %24
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@ip2str.buf = internal global [2 x [16 x i8]] zeroinitializer
@ip2str.index = internal unnamed_addr global i1 false
@.str = [12 x i8] c"%d.%d.%d.%d\00"
define i8* @ip2str(i32 %0) {
%2 = tail call i32 @ntohl(i32 %0)
%3 = load i1, i1* @ip2str.index
%4 = zext i1 %3 to i64
%5 = getelementptr inbounds [2 x [16 x i8]], [2 x [16 x i8]]* @ip2str.buf, i64 0, i64 %4, i64 0
%6 = lshr i32 %2, 24
%7 = lshr i32 %2, 16
%8 = and i32 %7, 255
%9 = lshr i32 %2, 8
%10 = and i32 %9, 255
%11 = and i32 %2, 255
%12 = tail call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %8, i32 %10, i32 %11)
%13 = load i1, i1* @ip2str.index
%14 = select i1 %13, i8* getelementptr inbounds ([2 x [16 x i8]], [2 x [16 x i8]]* @ip2str.buf, i64 0, i64 1, i64 0), i8* getelementptr inbounds ([2 x [16 x i8]], [2 x [16 x i8]]* @ip2str.buf, i64 0, i64 0, i64 0)
%15 = xor i1 %13, true
store i1 %15, i1* @ip2str.index
ret i8* %14
}