#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 session_handler {
  int dummy;
};
struct session {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(struct session_handler *);
int /*<<< orphan*/ free_session(struct session *);
int /*<<< orphan*/ handler_get_session_at_index(struct session_handler *,
                                                struct session **,
                                                int /*<<< orphan*/);
scalar_t__ handler_num_sessions(struct session_handler *);
void free_session_handler(struct session_handler *handler) {
  struct session *session;
  while (handler_num_sessions(handler)) {
    handler_get_session_at_index(handler, &session, 0);
    free_session(session);
  }
  free(handler);
}


x86 assembly (clang, -O0):
<free_session_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <handler_num_sessions@plt>
cmp    $0x0,%rax
je     117c <free_session_handler+0x3c>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1050 <handler_get_session_at_index@plt>
mov    -0x10(%rbp),%rdi
call   1060 <free_session@plt>
jmp    114c <free_session_handler+0xc>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<free_session_handler>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <handler_num_sessions@plt>
test   %rax,%rax
je     1183 <free_session_handler+0x43>
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <handler_get_session_at_index@plt>
mov    (%rsp),%rdi
call   1060 <free_session@plt>
mov    %rbx,%rdi
call   1040 <handler_num_sessions@plt>
test   %rax,%rax
jne    1160 <free_session_handler+0x20>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.session_handler = type { i32 }
%struct.session = type { i32 }
define void @free_session_handler(%struct.session_handler* %0) {
%2 = alloca %struct.session_handler*
%3 = alloca %struct.session*
store %struct.session_handler* %0, %struct.session_handler** %2
br label %4
4: ; preds = %8, %1
%5 = load %struct.session_handler*, %struct.session_handler** %2
%6 = call i64 @handler_num_sessions(%struct.session_handler* %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = load %struct.session_handler*, %struct.session_handler** %2
%10 = call i32 @handler_get_session_at_index(%struct.session_handler* %9, %struct.session** %3, i32 0)
%11 = load %struct.session*, %struct.session** %3
%12 = call i32 @free_session(%struct.session* %11)
br label %4
13: ; preds = %4
%14 = load %struct.session_handler*, %struct.session_handler** %2
%15 = call i32 @free(%struct.session_handler* %14)
ret void
}

LLVM IR (-O2):
%struct.session_handler = type { i32 }
%struct.session = type { i32 }
define void @free_session_handler(%struct.session_handler* %0) {
%2 = alloca %struct.session*
%3 = bitcast %struct.session** %2 to i8*
%4 = call i64 @handler_num_sessions(%struct.session_handler* %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %1, %6
%7 = call i32 @handler_get_session_at_index(%struct.session_handler* %0, %struct.session** %2, i32 0)
%8 = load %struct.session*, %struct.session** %2
%9 = call i32 @free_session(%struct.session* %8)
%10 = call i64 @handler_num_sessions(%struct.session_handler* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %6
12: ; preds = %6, %1
%13 = call i32 @free(%struct.session_handler* %0)
ret void
}