#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_port;
  int /*<<< orphan*/ sin_addr;
};
struct gsn_t {
  int dummy;
};

/* Variables and functions */
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int, unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int strlen(char *);
char *snprint_packet(struct gsn_t *gsn, struct sockaddr_in *peer, void *pack,
                     unsigned len, char *buf, int size) {
  unsigned int n;
  int pos;
  snprintf(buf, size,
           "Packet from %s:%u, length: %d, content:", inet_ntoa(peer->sin_addr),
           ntohs(peer->sin_port), len);
  buf[size - 1] = 0;
  pos = strlen(buf);
  for (n = 0; n < len; n++) {
    if ((pos + 4) < size) {
      sprintf((buf + pos), " %02hhx", ((unsigned char *)pack)[n]);
      pos += 3;
    }
  }
  buf[pos] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<snprint_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
mov    -0x1c(%rbp),%r9d
lea    0xe4f(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1233 <snprint_packet+0xe3>
mov    -0x34(%rbp),%eax
add    $0x4,%eax
cmp    -0x2c(%rbp),%eax
jge    1220 <snprint_packet+0xd0>
mov    -0x28(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%ecx
lea    0xe1a(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1070 <sprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x34(%rbp)
jmp    1225 <snprint_packet+0xd5>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11da <snprint_packet+0x8a>
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<snprint_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r13d
mov    %r8,%r15
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x4(%rsi),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbp
mov    (%rbx),%edi
call   1060 <ntohs@plt>
lea    0xe7d(%rip),%rdx
mov    %r15,%rdi
mov    %r13d,%esi
mov    %rbp,%rcx
mov    %eax,%r8d
mov    %r12d,%r9d
call   1050 <snprintf@plt>
movslq %r13d,%rax
movb   $0x0,-0x1(%rax,%r15,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
test   %r12d,%r12d
je     11ed <snprint_packet+0x9d>
mov    %r12d,%ebp
xor    %r12d,%r12d
jmp    11c9 <snprint_packet+0x79>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r12
cmp    %r12,%rbp
je     11ed <snprint_packet+0x9d>
lea    0x4(%rbx),%eax
cmp    %r13d,%eax
jge    11c0 <snprint_packet+0x70>
movslq %ebx,%rdi
add    %r15,%rdi
movzbl (%r14,%r12,1),%edx
lea    0xe45(%rip),%rsi
call   1070 <sprintf@plt>
add    $0x3,%ebx
jmp    11c0 <snprint_packet+0x70>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.gsn_t = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
@.str = [40 x i8] c"Packet from %s:%u, length: %d, content:\00"
@.str.1 = [8 x i8] c" %02hhx\00"
define i8* @snprint_packet(%struct.gsn_t* %0, %struct.sockaddr_in* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca %struct.gsn_t*
%8 = alloca %struct.sockaddr_in*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store %struct.gsn_t* %0, %struct.gsn_t** %7
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i32 %5, i32* %12
%15 = load i8*, i8** %11
%16 = load i32, i32* %12
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i8* @inet_ntoa(i32 %19)
%21 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
%25 = load i32, i32* %10
%26 = call i32 @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %24, i32 %25)
%27 = load i8*, i8** %11
%28 = load i32, i32* %12
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %11
%33 = call i32 @strlen(i8* %32)
store i32 %33, i32* %14
store i32 0, i32* %13
br label %34
34: ; preds = %57, %6
%35 = load i32, i32* %13
%36 = load i32, i32* %10
%37 = icmp ult i32 %35, %36
br i1 %37, label %38, label %60
38: ; preds = %34
%39 = load i32, i32* %14
%40 = add nsw i32 %39, 4
%41 = load i32, i32* %12
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %56
43: ; preds = %38
%44 = load i8*, i8** %11
%45 = load i32, i32* %14
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8*, i8** %9
%49 = load i32, i32* %13
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = call i32 @sprintf(i8* %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %52)
%54 = load i32, i32* %14
%55 = add nsw i32 %54, 3
store i32 %55, i32* %14
br label %56
56: ; preds = %43, %38
br label %57
57: ; preds = %56
%58 = load i32, i32* %13
%59 = add i32 %58, 1
store i32 %59, i32* %13
br label %34
60: ; preds = %34
%61 = load i8*, i8** %11
%62 = load i32, i32* %14
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8 0, i8* %64
%65 = load i8*, i8** %11
ret i8* %65
}

LLVM IR (-O2):
%struct.gsn_t = type { i32 }
%struct.sockaddr_in = type { i32, i32 }
@.str = [40 x i8] c"Packet from %s:%u, length: %d, content:\00"
@.str.1 = [8 x i8] c" %02hhx\00"
define i8* @snprint_packet(%struct.gsn_t* nocapture readnone %0, %struct.sockaddr_in* nocapture %1, i8* nocapture %2, i32 %3, i8* returned %4, i32 %5) {
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i8* @inet_ntoa(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @ntohs(i32 %11)
%13 = tail call i32 @snprintf(i8* %4, i32 %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %12, i32 %3)
%14 = add nsw i32 %5, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %4, i64 %15
store i8 0, i8* %16
%17 = tail call i32 @strlen(i8* %4)
%18 = icmp eq i32 %3, 0
br i1 %18, label %37, label %19
19: ; preds = %6
%20 = zext i32 %3 to i64
br label %21
21: ; preds = %19, %33
%22 = phi i64 [ 0, %19 ], [ %35, %33 ]
%23 = phi i32 [ %17, %19 ], [ %34, %33 ]
%24 = add nsw i32 %23, 4
%25 = icmp slt i32 %24, %5
br i1 %25, label %26, label %33
26: ; preds = %21
%27 = sext i32 %23 to i64
%28 = getelementptr inbounds i8, i8* %4, i64 %27
%29 = getelementptr inbounds i8, i8* %2, i64 %22
%30 = load i8, i8* %29
%31 = tail call i32 @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %30)
%32 = add nsw i32 %23, 3
br label %33
33: ; preds = %21, %26
%34 = phi i32 [ %32, %26 ], [ %23, %21 ]
%35 = add nuw nsw i64 %22, 1
%36 = icmp eq i64 %35, %20
br i1 %36, label %37, label %21
37: ; preds = %33, %6
%38 = phi i32 [ %17, %6 ], [ %34, %33 ]
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %4, i64 %39
store i8 0, i8* %40
ret i8* %4
}