#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_un {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  char *h_name;
  scalar_t__ h_addrtype;
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
scalar_t__ AF_INET;
struct hostent *gethostbyaddr(char *, int, scalar_t__);
struct hostent *gethostbyname(char *);
scalar_t__ getpeername(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char,
                           unsigned char, unsigned char);
int /*<<< orphan*/ strncpy(char *, char *, int);
void libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
                                      char *rip, int rip_len) {
  unsigned int len;
  struct sockaddr_in sin;
  struct hostent *host;
  struct hostent *host1;
  char ip[128];
  unsigned char *p;
  int n;
  struct sockaddr_un *un;
  rip[0] = '\0';
  name[0] = '\0';
  len = sizeof sin;
  if (getpeername(fd, (struct sockaddr *)&sin, &len) < 0) {
    perror("getpeername");
    return;
  }
  host = gethostbyaddr((char *)&sin.sin_addr, sizeof sin.sin_addr, AF_INET);
  if (host == NULL) {
    perror("gethostbyaddr");
    return;
  }
  strncpy(name, host->h_name, name_len);
  name[name_len - 1] = '\0';
  host1 = gethostbyname(host->h_name);
  if (host1 == NULL)
    return;
  p = (unsigned char *)host1;
  n = 0;
  while (p != NULL) {
    p = (unsigned char *)host1->h_addr_list[n++];
    if (p == NULL)
      continue;
    if ((host1->h_addrtype != AF_INET))
      continue;
    if (host1->h_addrtype == AF_INET)
      sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
    p = NULL;
    strncpy(rip, ip, rip_len);
    rip[rip_len - 1] = '\0';
  }
}


x86 assembly (clang, -O0):
<libwebsockets_get_peer_addresses>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x4,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <getpeername@plt>
cmp    $0x0,%rax
jge    11bd <libwebsockets_get_peer_addresses+0x5d>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
jmp    1346 <libwebsockets_get_peer_addresses+0x1e6>
lea    -0x30(%rbp),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%rdx
mov    $0x4,%esi
call   1070 <gethostbyaddr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f5 <libwebsockets_get_peer_addresses+0x95>
lea    0xe21(%rip),%rdi
call   1060 <perror@plt>
jmp    1346 <libwebsockets_get_peer_addresses+0x1e6>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1239 <libwebsockets_get_peer_addresses+0xd9>
jmp    1346 <libwebsockets_get_peer_addresses+0x1e6>
mov    -0x40(%rbp),%rax
mov    %rax,-0xc8(%rbp)
movl   $0x0,-0xcc(%rbp)
cmpq   $0x0,-0xc8(%rbp)
je     1346 <libwebsockets_get_peer_addresses+0x1e6>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rax
mov    -0xcc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0xcc(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0xc8(%rbp)
cmpq   $0x0,-0xc8(%rbp)
jne    1296 <libwebsockets_get_peer_addresses+0x136>
jmp    124e <libwebsockets_get_peer_addresses+0xee>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d43(%rip),%rcx
cmp    (%rcx),%rax
je     12b3 <libwebsockets_get_peer_addresses+0x153>
jmp    124e <libwebsockets_get_peer_addresses+0xee>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d26(%rip),%rcx
cmp    (%rcx),%rax
jne    1312 <libwebsockets_get_peer_addresses+0x1b2>
lea    -0xc0(%rbp),%rdi
mov    -0xc8(%rbp),%rax
mov    (%rax),%dl
mov    -0xc8(%rbp),%rax
mov    0x1(%rax),%cl
mov    -0xc8(%rbp),%rax
mov    0x2(%rax),%r8b
mov    -0xc8(%rbp),%rax
lea    0xd1c(%rip),%rsi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl 0x3(%rax),%r9d
call   1080 <sprintf@plt>
movq   $0x0,-0xc8(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0xc0(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    124e <libwebsockets_get_peer_addresses+0xee>
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libwebsockets_get_peer_addresses>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %edx,%r12d
mov    %rsi,%rbp
movb   $0x0,(%rcx)
movb   $0x0,(%rsi)
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1040 <getpeername@plt>
test   %rax,%rax
js     125d <libwebsockets_get_peer_addresses+0xfd>
mov    0x2e3e(%rip),%r13
mov    0x0(%r13),%rdx
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
call   1070 <gethostbyaddr@plt>
test   %rax,%rax
je     1266 <libwebsockets_get_peer_addresses+0x106>
mov    %rax,%rbx
mov    (%rax),%rsi
mov    %rbp,%rdi
mov    %r12d,%edx
call   1030 <strncpy@plt>
movslq %r12d,%rax
movb   $0x0,-0x1(%rax,%rbp,1)
mov    (%rbx),%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1272 <libwebsockets_get_peer_addresses+0x112>
mov    0x10(%rax),%rcx
mov    (%rcx),%rsi
test   %rsi,%rsi
je     1272 <libwebsockets_get_peer_addresses+0x112>
movslq %r14d,%rbp
mov    0x0(%r13),%rdx
mov    0x8(%rax),%rax
add    $0x8,%rcx
nopl   0x0(%rax,%rax,1)
cmp    %rdx,%rax
je     1223 <libwebsockets_get_peer_addresses+0xc3>
mov    (%rcx),%rsi
add    $0x8,%rcx
test   %rsi,%rsi
jne    1210 <libwebsockets_get_peer_addresses+0xb0>
jmp    1272 <libwebsockets_get_peer_addresses+0x112>
movzbl (%rsi),%edx
movzbl 0x1(%rsi),%ecx
movzbl 0x2(%rsi),%r8d
movzbl 0x3(%rsi),%r9d
lea    0xddf(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r15,%rbp,1)
jmp    1272 <libwebsockets_get_peer_addresses+0x112>
lea    0xd9c(%rip),%rdi
jmp    126d <libwebsockets_get_peer_addresses+0x10d>
lea    0xd9f(%rip),%rdi
call   1060 <perror@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.hostent = type { i8*, i64, i64* }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"getpeername\00"
@AF_INET = global i64 0
@.str.1 = [14 x i8] c"gethostbyaddr\00"
@.str.2 = [12 x i8] c"%u.%u.%u.%u\00"
define void @libwebsockets_get_peer_addresses(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.hostent*
%14 = alloca %struct.hostent*
%15 = alloca [128 x i8]
%16 = alloca i8*
%17 = alloca i32
%18 = alloca %struct.sockaddr_un*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%19 = load i8*, i8** %9
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 0, i8* %20
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 0, i8* %22
store i32 4, i32* %11
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%25 = call i64 @getpeername(i32 %23, %struct.sockaddr* %24, i32* %11)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %5
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %114
29: ; preds = %5
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%31 = bitcast i32* %30 to i8*
%32 = load i64, i64* @AF_INET
%33 = call %struct.hostent* @gethostbyaddr(i8* %31, i32 4, i64 %32)
store %struct.hostent* %33, %struct.hostent** %13
%34 = load %struct.hostent*, %struct.hostent** %13
%35 = icmp eq %struct.hostent* %34, null
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %114
38: ; preds = %29
%39 = load i8*, i8** %7
%40 = load %struct.hostent*, %struct.hostent** %13
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = load i32, i32* %8
%44 = call i32 @strncpy(i8* %39, i8* %42, i32 %43)
%45 = load i8*, i8** %7
%46 = load i32, i32* %8
%47 = sub nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 0, i8* %49
%50 = load %struct.hostent*, %struct.hostent** %13
%51 = getelementptr inbounds %struct.hostent, %struct.hostent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = call %struct.hostent* @gethostbyname(i8* %52)
store %struct.hostent* %53, %struct.hostent** %14
%54 = load %struct.hostent*, %struct.hostent** %14
%55 = icmp eq %struct.hostent* %54, null
br i1 %55, label %56, label %57
56: ; preds = %38
br label %114
57: ; preds = %38
%58 = load %struct.hostent*, %struct.hostent** %14
%59 = bitcast %struct.hostent* %58 to i8*
store i8* %59, i8** %16
store i32 0, i32* %17
br label %60
60: ; preds = %104, %82, %75, %57
%61 = load i8*, i8** %16
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %114
63: ; preds = %60
%64 = load %struct.hostent*, %struct.hostent** %14
%65 = getelementptr inbounds %struct.hostent, %struct.hostent* %64, i32 0, i32 2
%66 = load i64*, i64** %65
%67 = load i32, i32* %17
%68 = add nsw i32 %67, 1
store i32 %68, i32* %17
%69 = sext i32 %67 to i64
%70 = getelementptr inbounds i64, i64* %66, i64 %69
%71 = load i64, i64* %70
%72 = inttoptr i64 %71 to i8*
store i8* %72, i8** %16
%73 = load i8*, i8** %16
%74 = icmp eq i8* %73, null
br i1 %74, label %75, label %76
75: ; preds = %63
br label %60
76: ; preds = %63
%77 = load %struct.hostent*, %struct.hostent** %14
%78 = getelementptr inbounds %struct.hostent, %struct.hostent* %77, i32 0, i32 1
%79 = load i64, i64* %78
%80 = load i64, i64* @AF_INET
%81 = icmp ne i64 %79, %80
br i1 %81, label %82, label %83
82: ; preds = %76
br label %60
83: ; preds = %76
%84 = load %struct.hostent*, %struct.hostent** %14
%85 = getelementptr inbounds %struct.hostent, %struct.hostent* %84, i32 0, i32 1
%86 = load i64, i64* %85
%87 = load i64, i64* @AF_INET
%88 = icmp eq i64 %86, %87
br i1 %88, label %89, label %104
89: ; preds = %83
%90 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%91 = load i8*, i8** %16
%92 = getelementptr inbounds i8, i8* %91, i64 0
%93 = load i8, i8* %92
%94 = load i8*, i8** %16
%95 = getelementptr inbounds i8, i8* %94, i64 1
%96 = load i8, i8* %95
%97 = load i8*, i8** %16
%98 = getelementptr inbounds i8, i8* %97, i64 2
%99 = load i8, i8* %98
%100 = load i8*, i8** %16
%101 = getelementptr inbounds i8, i8* %100, i64 3
%102 = load i8, i8* %101
%103 = call i32 @sprintf(i8* %90, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %93, i8 zeroext %96, i8 zeroext %99, i8 zeroext %102)
br label %104
104: ; preds = %89, %83
store i8* null, i8** %16
%105 = load i8*, i8** %9
%106 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%107 = load i32, i32* %10
%108 = call i32 @strncpy(i8* %105, i8* %106, i32 %107)
%109 = load i8*, i8** %9
%110 = load i32, i32* %10
%111 = sub nsw i32 %110, 1
%112 = sext i32 %111 to i64
%113 = getelementptr inbounds i8, i8* %109, i64 %112
store i8 0, i8* %113
br label %60
114: ; preds = %27, %36, %56, %60
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
%struct.hostent = type { i8*, i64, i64* }
@.str = [12 x i8] c"getpeername\00"
@AF_INET = global i64 0
@.str.1 = [14 x i8] c"gethostbyaddr\00"
@.str.2 = [12 x i8] c"%u.%u.%u.%u\00"
define void @libwebsockets_get_peer_addresses(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca [128 x i8]
%9 = bitcast i32* %6 to i8*
%10 = bitcast %struct.sockaddr_in* %7 to i8*
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
store i8 0, i8* %3
store i8 0, i8* %1
store i32 4, i32* %6
%12 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%13 = call i64 @getpeername(i32 %0, %struct.sockaddr* %12, i32* %6)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %5
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %65
17: ; preds = %5
%18 = load i64, i64* @AF_INET
%19 = call %struct.hostent* @gethostbyaddr(i8* %10, i32 4, i64 %18)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %65
23: ; preds = %17
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 @strncpy(i8* %1, i8* %25, i32 %2)
%27 = add nsw i32 %2, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %1, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %24
%31 = call %struct.hostent* @gethostbyname(i8* %30)
%32 = icmp eq %struct.hostent* %31, null
br i1 %32, label %65, label %33
33: ; preds = %23
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i64 0, i32 2
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i64 0, i32 1
%36 = add nsw i32 %4, -1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %3, i64 %37
%39 = load i64*, i64** %34
%40 = load i64, i64* @AF_INET
%41 = load i64, i64* %39
%42 = icmp eq i64 %41, 0
br i1 %42, label %65, label %43
43: ; preds = %33
%44 = load i64, i64* %35
%45 = icmp eq i64 %44, %40
br label %51
46: ; preds = %51
%47 = add nuw i64 %53, 1
%48 = getelementptr inbounds i64, i64* %39, i64 %53
%49 = load i64, i64* %48
%50 = icmp eq i64 %49, 0
br i1 %50, label %65, label %51
51: ; preds = %43, %46
%52 = phi i64 [ %49, %46 ], [ %41, %43 ]
%53 = phi i64 [ %47, %46 ], [ 1, %43 ]
br i1 %45, label %54, label %46
54: ; preds = %51
%55 = inttoptr i64 %52 to i8*
%56 = load i8, i8* %55
%57 = getelementptr inbounds i8, i8* %55, i64 1
%58 = load i8, i8* %57
%59 = getelementptr inbounds i8, i8* %55, i64 2
%60 = load i8, i8* %59
%61 = getelementptr inbounds i8, i8* %55, i64 3
%62 = load i8, i8* %61
%63 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %56, i8 zeroext %58, i8 zeroext %60, i8 zeroext %62)
%64 = call i32 @strncpy(i8* %3, i8* %11, i32 %4)
store i8 0, i8* %38
br label %65
65: ; preds = %46, %33, %54, %23, %21, %15
ret void
}