#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 memSuite {
  int dummy;
};
struct memChunk {
  int dummy;
};

/* Variables and functions */
struct memChunk *GetFirstChunkFromSuite(struct memSuite *);
struct memChunk *GetNextMemoryChunk(struct memSuite *, struct memChunk *);
scalar_t__ GetSizeOfMemoryChunk(struct memChunk *);
int lv_rec_get_memsize(struct memSuite *suite) {
  int size = 0;
  struct memChunk *chunk = GetFirstChunkFromSuite(suite);
  while (chunk) {
    size += GetSizeOfMemoryChunk(chunk);
    chunk = GetNextMemoryChunk(suite, chunk);
  }
  return size;
}


x86 assembly (clang, -O0):
<lv_rec_get_memsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <GetFirstChunkFromSuite@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1187 <lv_rec_get_memsize+0x57>
mov    -0x18(%rbp),%rdi
call   1050 <GetSizeOfMemoryChunk@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <GetNextMemoryChunk@plt>
mov    %rax,-0x18(%rbp)
jmp    1150 <lv_rec_get_memsize+0x20>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lv_rec_get_memsize>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <GetFirstChunkFromSuite@plt>
test   %rax,%rax
je     116f <lv_rec_get_memsize+0x3f>
mov    %rax,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <GetSizeOfMemoryChunk@plt>
add    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <GetNextMemoryChunk@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1150 <lv_rec_get_memsize+0x20>
jmp    1171 <lv_rec_get_memsize+0x41>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.memSuite = type { i32 }
%struct.memChunk = type { i32 }
define i32 @lv_rec_get_memsize(%struct.memSuite* %0) {
%2 = alloca %struct.memSuite*
%3 = alloca i32
%4 = alloca %struct.memChunk*
store %struct.memSuite* %0, %struct.memSuite** %2
store i32 0, i32* %3
%5 = load %struct.memSuite*, %struct.memSuite** %2
%6 = call %struct.memChunk* @GetFirstChunkFromSuite(%struct.memSuite* %5)
store %struct.memChunk* %6, %struct.memChunk** %4
br label %7
7: ; preds = %10, %1
%8 = load %struct.memChunk*, %struct.memChunk** %4
%9 = icmp ne %struct.memChunk* %8, null
br i1 %9, label %10, label %20
10: ; preds = %7
%11 = load %struct.memChunk*, %struct.memChunk** %4
%12 = call i64 @GetSizeOfMemoryChunk(%struct.memChunk* %11)
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = add nsw i64 %14, %12
%16 = trunc i64 %15 to i32
store i32 %16, i32* %3
%17 = load %struct.memSuite*, %struct.memSuite** %2
%18 = load %struct.memChunk*, %struct.memChunk** %4
%19 = call %struct.memChunk* @GetNextMemoryChunk(%struct.memSuite* %17, %struct.memChunk* %18)
store %struct.memChunk* %19, %struct.memChunk** %4
br label %7
20: ; preds = %7
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
%struct.memSuite = type { i32 }
%struct.memChunk = type { i32 }
define i32 @lv_rec_get_memsize(%struct.memSuite* %0) {
%2 = tail call %struct.memChunk* @GetFirstChunkFromSuite(%struct.memSuite* %0)
%3 = icmp eq %struct.memChunk* %2, null
br i1 %3, label %12, label %4
4: ; preds = %1, %4
%5 = phi %struct.memChunk* [ %10, %4 ], [ %2, %1 ]
%6 = phi i32 [ %9, %4 ], [ 0, %1 ]
%7 = tail call i64 @GetSizeOfMemoryChunk(%struct.memChunk* %5)
%8 = trunc i64 %7 to i32
%9 = add i32 %6, %8
%10 = tail call %struct.memChunk* @GetNextMemoryChunk(%struct.memSuite* %0, %struct.memChunk* %5)
%11 = icmp eq %struct.memChunk* %10, null
br i1 %11, label %12, label %4
12: ; preds = %4, %1
%13 = phi i32 [ 0, %1 ], [ %9, %4 ]
ret i32 %13
}