#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 mem_chunk {
  int dummy;
};

/* Variables and functions */
struct mem_chunk *alloc_chunk(struct mem_chunk *);
int /*<<< orphan*/ free_chunks(struct mem_chunk *);
struct mem_chunk *_alloc_num_chunks(size_t chunks) {
  struct mem_chunk *head = NULL;
  struct mem_chunk *thiz = NULL;
  while (chunks--) {
    if ((thiz = alloc_chunk(thiz)) == NULL)
      goto cleanup;
    if (!head)
      head = thiz;
  }
  return head;
cleanup:
  free_chunks(head);
  return NULL;
}


x86 assembly (clang, -O0):
<_alloc_num_chunks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
je     1189 <_alloc_num_chunks+0x69>
mov    -0x20(%rbp),%rdi
call   1030 <alloc_chunk@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1171 <_alloc_num_chunks+0x51>
jmp    1196 <_alloc_num_chunks+0x76>
cmpq   $0x0,-0x18(%rbp)
jne    1184 <_alloc_num_chunks+0x64>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    113c <_alloc_num_chunks+0x1c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a7 <_alloc_num_chunks+0x87>
mov    -0x18(%rbp),%rdi
call   1040 <free_chunks@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_alloc_num_chunks>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1167 <_alloc_num_chunks+0x47>
mov    %rdi,%rbx
xor    %eax,%eax
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
call   1030 <alloc_chunk@plt>
test   %rax,%rax
je     115f <_alloc_num_chunks+0x3f>
add    $0xffffffffffffffff,%rbx
test   %r14,%r14
cmove  %rax,%r14
test   %rbx,%rbx
jne    1140 <_alloc_num_chunks+0x20>
jmp    116a <_alloc_num_chunks+0x4a>
mov    %r14,%rdi
call   1040 <free_chunks@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.mem_chunk = type { i32 }
define %struct.mem_chunk* @_alloc_num_chunks(i64 %0) {
%2 = alloca %struct.mem_chunk*
%3 = alloca i64
%4 = alloca %struct.mem_chunk*
%5 = alloca %struct.mem_chunk*
store i64 %0, i64* %3
store %struct.mem_chunk* null, %struct.mem_chunk** %4
store %struct.mem_chunk* null, %struct.mem_chunk** %5
br label %6
6: ; preds = %20, %1
%7 = load i64, i64* %3
%8 = add i64 %7, -1
store i64 %8, i64* %3
%9 = icmp ne i64 %7, 0
br i1 %9, label %10, label %21
10: ; preds = %6
%11 = load %struct.mem_chunk*, %struct.mem_chunk** %5
%12 = call %struct.mem_chunk* @alloc_chunk(%struct.mem_chunk* %11)
store %struct.mem_chunk* %12, %struct.mem_chunk** %5
%13 = icmp eq %struct.mem_chunk* %12, null
br i1 %13, label %14, label %15
14: ; preds = %10
br label %23
15: ; preds = %10
%16 = load %struct.mem_chunk*, %struct.mem_chunk** %4
%17 = icmp ne %struct.mem_chunk* %16, null
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = load %struct.mem_chunk*, %struct.mem_chunk** %5
store %struct.mem_chunk* %19, %struct.mem_chunk** %4
br label %20
20: ; preds = %18, %15
br label %6
21: ; preds = %6
%22 = load %struct.mem_chunk*, %struct.mem_chunk** %4
store %struct.mem_chunk* %22, %struct.mem_chunk** %2
br label %26
23: ; preds = %14
%24 = load %struct.mem_chunk*, %struct.mem_chunk** %4
%25 = call i32 @free_chunks(%struct.mem_chunk* %24)
store %struct.mem_chunk* null, %struct.mem_chunk** %2
br label %26
26: ; preds = %23, %21
%27 = load %struct.mem_chunk*, %struct.mem_chunk** %2
ret %struct.mem_chunk* %27
}

LLVM IR (-O2):
%struct.mem_chunk = type { i32 }
define %struct.mem_chunk* @_alloc_num_chunks(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %16, label %3
3: ; preds = %1, %10
%4 = phi i64 [ %7, %10 ], [ %0, %1 ]
%5 = phi %struct.mem_chunk* [ %8, %10 ], [ null, %1 ]
%6 = phi %struct.mem_chunk* [ %12, %10 ], [ null, %1 ]
%7 = add i64 %4, -1
%8 = tail call %struct.mem_chunk* @alloc_chunk(%struct.mem_chunk* %5)
%9 = icmp eq %struct.mem_chunk* %8, null
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = icmp eq %struct.mem_chunk* %6, null
%12 = select i1 %11, %struct.mem_chunk* %8, %struct.mem_chunk* %6
%13 = icmp eq i64 %7, 0
br i1 %13, label %16, label %3
14: ; preds = %3
%15 = tail call i32 @free_chunks(%struct.mem_chunk* %6)
br label %16
16: ; preds = %10, %1, %14
%17 = phi %struct.mem_chunk* [ null, %14 ], [ null, %1 ], [ %12, %10 ]
ret %struct.mem_chunk* %17
}