#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ free(void *);
void *realloc(void *, size_t);
size_t sysconf(int /*<<< orphan*/);
void *list_compact_real(void *const _list, size_t objectsize) {
  struct {
    void *list;
    ssize_t size;
    size_t maxlength;
    int shmfd;
    void *oldmem;
  } *list = _list;
  if (list->maxlength * objectsize >= sysconf(_SC_PAGESIZE))
    return list->list;
  if (list->size) {
    void *tmp = realloc(list->list, list->size * objectsize);
    if (tmp) {
      list->list = tmp;
      list->maxlength = list->size;
    }
  } else if (list->list) {
    free(list->list);
    list->list = NULL;
    list->maxlength = 0;
  }
  return list->list;
}


x86 assembly (clang, -O0):
<list_compact_real>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jb     1187 <list_compact_real+0x57>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    121f <list_compact_real+0xef>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11de <list_compact_real+0xae>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
imul   -0x18(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d9 <list_compact_real+0xa9>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
jmp    1214 <list_compact_real+0xe4>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     120f <list_compact_real+0xdf>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x10(%rax)
jmp    1214 <list_compact_real+0xe4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_compact_real>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x10(%rdi),%rbx
imul   %rsi,%rbx
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
cmp    %rax,%rbx
jae    1190 <list_compact_real+0x60>
mov    (%r15),%rdi
mov    0x8(%r15),%rsi
test   %rsi,%rsi
je     1179 <list_compact_real+0x49>
imul   %r14,%rsi
call   1040 <realloc@plt>
test   %rax,%rax
je     1190 <list_compact_real+0x60>
mov    %rax,(%r15)
mov    0x8(%r15),%rax
jmp    118c <list_compact_real+0x5c>
test   %rdi,%rdi
je     1190 <list_compact_real+0x60>
call   1030 <free@plt>
movq   $0x0,(%r15)
xor    %eax,%eax
mov    %rax,0x10(%r15)
mov    (%r15),%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.anon = type { i8*, i64, i64, i32, i8* }
@_SC_PAGESIZE = global i32 0
define i8* @list_compact_real(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.anon*
%7 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = bitcast i8* %8 to %struct.anon*
store %struct.anon* %9, %struct.anon** %6
%10 = load %struct.anon*, %struct.anon** %6
%11 = getelementptr inbounds %struct.anon, %struct.anon* %10, i32 0, i32 2
%12 = load i64, i64* %11
%13 = load i64, i64* %5
%14 = mul i64 %12, %13
%15 = load i32, i32* @_SC_PAGESIZE
%16 = call i64 @sysconf(i32 %15)
%17 = icmp uge i64 %14, %16
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load %struct.anon*, %struct.anon** %6
%20 = getelementptr inbounds %struct.anon, %struct.anon* %19, i32 0, i32 0
%21 = load i8*, i8** %20
store i8* %21, i8** %3
br label %68
22: ; preds = %2
%23 = load %struct.anon*, %struct.anon** %6
%24 = getelementptr inbounds %struct.anon, %struct.anon* %23, i32 0, i32 1
%25 = load i64, i64* %24
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %49
27: ; preds = %22
%28 = load %struct.anon*, %struct.anon** %6
%29 = getelementptr inbounds %struct.anon, %struct.anon* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = load %struct.anon*, %struct.anon** %6
%32 = getelementptr inbounds %struct.anon, %struct.anon* %31, i32 0, i32 1
%33 = load i64, i64* %32
%34 = load i64, i64* %5
%35 = mul i64 %33, %34
%36 = call i8* @realloc(i8* %30, i64 %35)
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %48
39: ; preds = %27
%40 = load i8*, i8** %7
%41 = load %struct.anon*, %struct.anon** %6
%42 = getelementptr inbounds %struct.anon, %struct.anon* %41, i32 0, i32 0
store i8* %40, i8** %42
%43 = load %struct.anon*, %struct.anon** %6
%44 = getelementptr inbounds %struct.anon, %struct.anon* %43, i32 0, i32 1
%45 = load i64, i64* %44
%46 = load %struct.anon*, %struct.anon** %6
%47 = getelementptr inbounds %struct.anon, %struct.anon* %46, i32 0, i32 2
store i64 %45, i64* %47
br label %48
48: ; preds = %39, %27
br label %64
49: ; preds = %22
%50 = load %struct.anon*, %struct.anon** %6
%51 = getelementptr inbounds %struct.anon, %struct.anon* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %63
54: ; preds = %49
%55 = load %struct.anon*, %struct.anon** %6
%56 = getelementptr inbounds %struct.anon, %struct.anon* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 @free(i8* %57)
%59 = load %struct.anon*, %struct.anon** %6
%60 = getelementptr inbounds %struct.anon, %struct.anon* %59, i32 0, i32 0
store i8* null, i8** %60
%61 = load %struct.anon*, %struct.anon** %6
%62 = getelementptr inbounds %struct.anon, %struct.anon* %61, i32 0, i32 2
store i64 0, i64* %62
br label %63
63: ; preds = %54, %49
br label %64
64: ; preds = %63, %48
%65 = load %struct.anon*, %struct.anon** %6
%66 = getelementptr inbounds %struct.anon, %struct.anon* %65, i32 0, i32 0
%67 = load i8*, i8** %66
store i8* %67, i8** %3
br label %68
68: ; preds = %64, %18
%69 = load i8*, i8** %3
ret i8* %69
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
define i8* @list_compact_real(i8* nocapture %0, i64 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 16
%4 = bitcast i8* %3 to i64*
%5 = load i64, i64* %4
%6 = mul i64 %5, %1
%7 = load i32, i32* @_SC_PAGESIZE
%8 = tail call i64 @sysconf(i32 %7)
%9 = icmp ult i64 %6, %8
br i1 %9, label %10, label %29
10: ; preds = %2
%11 = getelementptr inbounds i8, i8* %0, i64 8
%12 = bitcast i8* %11 to i64*
%13 = load i64, i64* %12
%14 = icmp eq i64 %13, 0
%15 = bitcast i8* %0 to i8**
%16 = load i8*, i8** %15
br i1 %14, label %23, label %17
17: ; preds = %10
%18 = mul i64 %13, %1
%19 = tail call i8* @realloc(i8* %16, i64 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %29, label %21
21: ; preds = %17
store i8* %19, i8** %15
%22 = load i64, i64* %12
br label %27
23: ; preds = %10
%24 = icmp eq i8* %16, null
br i1 %24, label %29, label %25
25: ; preds = %23
%26 = tail call i32 @free(i8* %16)
store i8* null, i8** %15
br label %27
27: ; preds = %21, %25
%28 = phi i64 [ 0, %25 ], [ %22, %21 ]
store i64 %28, i64* %4
br label %29
29: ; preds = %27, %2, %23, %17
%30 = bitcast i8* %0 to i8**
%31 = load i8*, i8** %30
ret i8* %31
}