#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 in_addr {
  int dummy;
};
struct hostent {
  struct in_addr **h_addr_list;
};
struct apattern {
  int dummy;
};

/* Variables and functions */
int get_address_index(struct in_addr *, struct apattern *, int);
int /*<<< orphan*/ memcpy(struct in_addr *, struct in_addr *, int);
void sort_addresses(struct hostent *host, struct apattern *sortlist,
                    int nsort) {
  struct in_addr a1, a2;
  int i1, i2, ind1, ind2;
  for (i1 = 0; host->h_addr_list[i1]; i1++) {
    memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
    ind1 = get_address_index(&a1, sortlist, nsort);
    for (i2 = i1 - 1; i2 >= 0; i2--) {
      memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
      ind2 = get_address_index(&a2, sortlist, nsort);
      if (ind2 <= ind1)
        break;
      memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
    }
    memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
  }
}


x86 assembly (clang, -O0):
<sort_addresses>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1234 <sort_addresses+0x114>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    -0x18(%rbp),%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1030 <get_address_index@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     1204 <sort_addresses+0xe4>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
call   1030 <get_address_index@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jg     11d4 <sort_addresses+0xb4>
jmp    1204 <sort_addresses+0xe4>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    1189 <sort_addresses+0x69>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113a <sort_addresses+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_addresses>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,0x4(%rsp)
mov    %rsi,%r12
mov    (%rdi),%rax
mov    (%rax),%rsi
test   %rsi,%rsi
je     121e <sort_addresses+0xfe>
mov    %rdi,%rbp
xor    %r15d,%r15d
mov    %rsp,%rbx
lea    0x10(%rsp),%r13
jmp    1194 <sort_addresses+0x74>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %r14d,%r14d
mov    %rsp,%rbx
mov    0x0(%rbp),%rax
movslq %r14d,%rcx
mov    (%rax,%rcx,8),%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x0(%rbp),%rax
mov    0x8(%rax,%r15,8),%rsi
add    $0x1,%r15
test   %rsi,%rsi
je     121e <sort_addresses+0xfe>
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    0x4(%rsp),%edx
call   1030 <get_address_index@plt>
test   %r15,%r15
je     1160 <sort_addresses+0x40>
mov    %eax,%ebx
mov    %r15,0x8(%rsp)
mov    %r15,%r14
nop
lea    -0x1(%r14),%r15
mov    0x0(%rbp),%rax
mov    %r15d,%ecx
mov    (%rax,%rcx,8),%rsi
mov    %r13,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    0x4(%rsp),%edx
call   1030 <get_address_index@plt>
cmp    %ebx,%eax
jle    1214 <sort_addresses+0xf4>
mov    0x0(%rbp),%rax
mov    (%rax,%r14,8),%rdi
mov    %r13,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
lea    0x1(%r15),%rax
mov    %r15,%r14
cmp    $0x1,%rax
jg     11c0 <sort_addresses+0xa0>
xor    %r14d,%r14d
mov    0x8(%rsp),%r15
jmp    1163 <sort_addresses+0x43>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { %struct.in_addr** }
%struct.in_addr = type { i32 }
%struct.apattern = type { i32 }
define void @sort_addresses(%struct.hostent* %0, %struct.apattern* %1, i32 %2) {
%4 = alloca %struct.hostent*
%5 = alloca %struct.apattern*
%6 = alloca i32
%7 = alloca %struct.in_addr
%8 = alloca %struct.in_addr
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.hostent* %0, %struct.hostent** %4
store %struct.apattern* %1, %struct.apattern** %5
store i32 %2, i32* %6
store i32 0, i32* %9
br label %13
13: ; preds = %78, %3
%14 = load %struct.hostent*, %struct.hostent** %4
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 0
%16 = load %struct.in_addr**, %struct.in_addr*** %15
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %16, i64 %18
%20 = load %struct.in_addr*, %struct.in_addr** %19
%21 = icmp ne %struct.in_addr* %20, null
br i1 %21, label %22, label %81
22: ; preds = %13
%23 = load %struct.hostent*, %struct.hostent** %4
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load %struct.in_addr**, %struct.in_addr*** %24
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %25, i64 %27
%29 = load %struct.in_addr*, %struct.in_addr** %28
%30 = call i32 @memcpy(%struct.in_addr* %7, %struct.in_addr* %29, i32 4)
%31 = load %struct.apattern*, %struct.apattern** %5
%32 = load i32, i32* %6
%33 = call i32 @get_address_index(%struct.in_addr* %7, %struct.apattern* %31, i32 %32)
store i32 %33, i32* %11
%34 = load i32, i32* %9
%35 = sub nsw i32 %34, 1
store i32 %35, i32* %10
br label %36
36: ; preds = %65, %22
%37 = load i32, i32* %10
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %68
39: ; preds = %36
%40 = load %struct.hostent*, %struct.hostent** %4
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load %struct.in_addr**, %struct.in_addr*** %41
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %42, i64 %44
%46 = load %struct.in_addr*, %struct.in_addr** %45
%47 = call i32 @memcpy(%struct.in_addr* %8, %struct.in_addr* %46, i32 4)
%48 = load %struct.apattern*, %struct.apattern** %5
%49 = load i32, i32* %6
%50 = call i32 @get_address_index(%struct.in_addr* %8, %struct.apattern* %48, i32 %49)
store i32 %50, i32* %12
%51 = load i32, i32* %12
%52 = load i32, i32* %11
%53 = icmp sle i32 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %39
br label %68
55: ; preds = %39
%56 = load %struct.hostent*, %struct.hostent** %4
%57 = getelementptr inbounds %struct.hostent, %struct.hostent* %56, i32 0, i32 0
%58 = load %struct.in_addr**, %struct.in_addr*** %57
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %58, i64 %61
%63 = load %struct.in_addr*, %struct.in_addr** %62
%64 = call i32 @memcpy(%struct.in_addr* %63, %struct.in_addr* %8, i32 4)
br label %65
65: ; preds = %55
%66 = load i32, i32* %10
%67 = add nsw i32 %66, -1
store i32 %67, i32* %10
br label %36
68: ; preds = %54, %36
%69 = load %struct.hostent*, %struct.hostent** %4
%70 = getelementptr inbounds %struct.hostent, %struct.hostent* %69, i32 0, i32 0
%71 = load %struct.in_addr**, %struct.in_addr*** %70
%72 = load i32, i32* %10
%73 = add nsw i32 %72, 1
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %71, i64 %74
%76 = load %struct.in_addr*, %struct.in_addr** %75
%77 = call i32 @memcpy(%struct.in_addr* %76, %struct.in_addr* %7, i32 4)
br label %78
78: ; preds = %68
%79 = load i32, i32* %9
%80 = add nsw i32 %79, 1
store i32 %80, i32* %9
br label %13
81: ; preds = %13
ret void
}

LLVM IR (-O2):
%struct.hostent = type { %struct.in_addr** }
%struct.in_addr = type { i32 }
%struct.apattern = type { i32 }
define void @sort_addresses(%struct.hostent* nocapture %0, %struct.apattern* %1, i32 %2) {
%4 = alloca %struct.in_addr
%5 = alloca %struct.in_addr
%6 = bitcast %struct.in_addr* %4 to i8*
%7 = bitcast %struct.in_addr* %5 to i8*
%8 = getelementptr inbounds %struct.hostent, %struct.hostent* %0, i64 0, i32 0
%9 = load %struct.in_addr**, %struct.in_addr*** %8
%10 = load %struct.in_addr*, %struct.in_addr** %9
%11 = icmp eq %struct.in_addr* %10, null
br i1 %11, label %47, label %12
12: ; preds = %3, %34
%13 = phi i64 [ %42, %34 ], [ 0, %3 ]
%14 = phi %struct.in_addr* [ %45, %34 ], [ %10, %3 ]
%15 = call i32 @memcpy(%struct.in_addr* %4, %struct.in_addr* %14, i32 4)
%16 = call i32 @get_address_index(%struct.in_addr* %4, %struct.apattern* %1, i32 %2)
%17 = icmp eq i64 %13, 0
br i1 %17, label %34, label %18
18: ; preds = %12, %28
%19 = phi i64 [ %20, %28 ], [ %13, %12 ]
%20 = add nsw i64 %19, -1
%21 = load %struct.in_addr**, %struct.in_addr*** %8
%22 = and i64 %20, 4294967295
%23 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %21, i64 %22
%24 = load %struct.in_addr*, %struct.in_addr** %23
%25 = call i32 @memcpy(%struct.in_addr* %5, %struct.in_addr* %24, i32 4)
%26 = call i32 @get_address_index(%struct.in_addr* %5, %struct.apattern* %1, i32 %2)
%27 = icmp sgt i32 %26, %16
br i1 %27, label %28, label %34
28: ; preds = %18
%29 = load %struct.in_addr**, %struct.in_addr*** %8
%30 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %29, i64 %19
%31 = load %struct.in_addr*, %struct.in_addr** %30
%32 = call i32 @memcpy(%struct.in_addr* %31, %struct.in_addr* %5, i32 4)
%33 = icmp sgt i64 %19, 1
br i1 %33, label %18, label %34
34: ; preds = %28, %18, %12
%35 = phi i64 [ 0, %12 ], [ %19, %18 ], [ 0, %28 ]
%36 = load %struct.in_addr**, %struct.in_addr*** %8
%37 = shl i64 %35, 32
%38 = ashr exact i64 %37, 32
%39 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %36, i64 %38
%40 = load %struct.in_addr*, %struct.in_addr** %39
%41 = call i32 @memcpy(%struct.in_addr* %40, %struct.in_addr* %4, i32 4)
%42 = add nuw i64 %13, 1
%43 = load %struct.in_addr**, %struct.in_addr*** %8
%44 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %43, i64 %42
%45 = load %struct.in_addr*, %struct.in_addr** %44
%46 = icmp eq %struct.in_addr* %45, null
br i1 %46, label %47, label %12
47: ; preds = %34, %3
ret void
}