#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
};

/* Variables and functions */
unsigned short ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char,
                           unsigned char, unsigned char, unsigned short);
char *str_addr(void *sap) {
  struct sockaddr_in *sa = sap;
  static char ring_buffer[8][256];
  static int ringpos = 0;
  unsigned char *addptr;
  unsigned short portno = ntohs(sa->sin_port);
  char *buf;
  buf = ring_buffer[ringpos];
  addptr = (unsigned char *)&(sa->sin_addr);
  sprintf(buf, "%d.%d.%d.%d:%d", addptr[0], addptr[1], addptr[2], addptr[3],
          portno);
  ringpos = (ringpos + 1) % 8;
  return buf;
}


x86 assembly (clang, -O0):
<str_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <ntohs@plt>
mov    %ax,-0x1a(%rbp)
movslq 0x36f5(%rip),%rcx
lea    0x2eee(%rip),%rax
shl    $0x8,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%dl
mov    -0x18(%rbp),%rax
mov    0x1(%rax),%cl
mov    -0x18(%rbp),%rax
mov    0x2(%rax),%r8b
mov    -0x18(%rbp),%rax
mov    0x3(%rax),%al
lea    0xe74(%rip),%rsi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %al,%r9d
movzwl -0x1a(%rbp),%eax
mov    %eax,(%rsp)
call   1040 <sprintf@plt>
mov    0x3694(%rip),%eax
add    $0x1,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,0x3683(%rip)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_addr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x4(%rdi),%edi
call   1030 <ntohs@plt>
mov    0x370b(%rip),%ecx
shl    $0x8,%rcx
lea    0x2f00(%rip),%r14
add    %rcx,%r14
movzbl (%rbx),%edx
movzbl 0x1(%rbx),%ecx
movzbl 0x2(%rbx),%r8d
movzbl 0x3(%rbx),%r9d
movzwl %ax,%eax
mov    %eax,(%rsp)
lea    0xe9f(%rip),%rsi
mov    %r14,%rdi
call   1040 <sprintf@plt>
mov    0x36d1(%rip),%eax
lea    0x1(%rax),%ecx
lea    0x8(%rax),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
and    $0xfffffff8,%edx
neg    %edx
add    %edx,%eax
add    $0x1,%eax
mov    %eax,0x36b6(%rip)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@str_addr.ring_buffer = internal global [8 x [256 x i8]] zeroinitializer
@str_addr.ringpos = internal global i32 0
@.str = [15 x i8] c"%d.%d.%d.%d:%d\00"
define i8* @str_addr(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in*
%4 = alloca i8*
%5 = alloca i16
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to %struct.sockaddr_in*
store %struct.sockaddr_in* %8, %struct.sockaddr_in** %3
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %3
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%11 = load i32, i32* %10
%12 = call zeroext i16 @ntohs(i32 %11)
store i16 %12, i16* %5
%13 = load i32, i32* @str_addr.ringpos
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [8 x [256 x i8]], [8 x [256 x i8]]* @str_addr.ring_buffer, i64 0, i64 %14
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
store i8* %16, i8** %6
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %3
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
%19 = bitcast i32* %18 to i8*
store i8* %19, i8** %4
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = load i8, i8* %25
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 2
%29 = load i8, i8* %28
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i64 3
%32 = load i8, i8* %31
%33 = load i16, i16* %5
%34 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8 zeroext %23, i8 zeroext %26, i8 zeroext %29, i8 zeroext %32, i16 zeroext %33)
%35 = load i32, i32* @str_addr.ringpos
%36 = add nsw i32 %35, 1
%37 = srem i32 %36, 8
store i32 %37, i32* @str_addr.ringpos
%38 = load i8*, i8** %6
ret i8* %38
}

LLVM IR (-O2):
@str_addr.ring_buffer = internal global [8 x [256 x i8]] zeroinitializer
@str_addr.ringpos = internal unnamed_addr global i32 0
@.str = [15 x i8] c"%d.%d.%d.%d:%d\00"
define i8* @str_addr(i8* nocapture %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 4
%3 = bitcast i8* %2 to i32*
%4 = load i32, i32* %3
%5 = tail call zeroext i16 @ntohs(i32 %4)
%6 = load i32, i32* @str_addr.ringpos
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds [8 x [256 x i8]], [8 x [256 x i8]]* @str_addr.ring_buffer, i64 0, i64 %7, i64 0
%9 = load i8, i8* %0
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = load i8, i8* %10
%12 = getelementptr inbounds i8, i8* %0, i64 2
%13 = load i8, i8* %12
%14 = getelementptr inbounds i8, i8* %0, i64 3
%15 = load i8, i8* %14
%16 = tail call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8 zeroext %9, i8 zeroext %11, i8 zeroext %13, i8 zeroext %15, i16 zeroext %5)
%17 = load i32, i32* @str_addr.ringpos
%18 = add nsw i32 %17, 1
%19 = srem i32 %18, 8
store i32 %19, i32* @str_addr.ringpos
ret i8* %8
}