#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 msg_queue {
  int dummy;
};
struct msg {
  int dummy;
};

/* Variables and functions */
struct msg *alloc_msg();
int /*<<< orphan*/ init_msg_queue(struct msg_queue *);
int /*<<< orphan*/ msg_enqueue(struct msg_queue *, struct msg *);
size_t alloc_msg_queue(struct msg_queue *queue, size_t len) {
  struct msg *mp;
  size_t i;
  init_msg_queue(queue);
  if (!len)
    return 0;
  for (i = 0; i < len; i++) {
    mp = alloc_msg();
    if (mp)
      msg_enqueue(queue, mp);
    else
      break;
  }
  return i;
}


x86 assembly (clang, -O0):
<alloc_msg_queue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <init_msg_queue@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1161 <alloc_msg_queue+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11c2 <alloc_msg_queue+0x92>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11ba <alloc_msg_queue+0x8a>
mov    $0x0,%al
call   1030 <alloc_msg@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119f <alloc_msg_queue+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <msg_enqueue@plt>
jmp    11a4 <alloc_msg_queue+0x74>
jmp    11ba <alloc_msg_queue+0x8a>
jmp    11a9 <alloc_msg_queue+0x79>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1169 <alloc_msg_queue+0x39>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_msg_queue>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <init_msg_queue@plt>
xor    %ebx,%ebx
test   %r14,%r14
je     1173 <alloc_msg_queue+0x43>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <alloc_msg@plt>
test   %rax,%rax
je     1173 <alloc_msg_queue+0x43>
mov    %r15,%rdi
mov    %rax,%rsi
call   1050 <msg_enqueue@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1150 <alloc_msg_queue+0x20>
mov    %r14,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.msg_queue = type { i32 }
%struct.msg = type { i32 }
define i64 @alloc_msg_queue(%struct.msg_queue* %0, i64 %1) {
%3 = alloca i64
%4 = alloca %struct.msg_queue*
%5 = alloca i64
%6 = alloca %struct.msg*
%7 = alloca i64
store %struct.msg_queue* %0, %struct.msg_queue** %4
store i64 %1, i64* %5
%8 = load %struct.msg_queue*, %struct.msg_queue** %4
%9 = call i32 @init_msg_queue(%struct.msg_queue* %8)
%10 = load i64, i64* %5
%11 = icmp ne i64 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %2
store i64 0, i64* %3
br label %33
13: ; preds = %2
store i64 0, i64* %7
br label %14
14: ; preds = %28, %13
%15 = load i64, i64* %7
%16 = load i64, i64* %5
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = call %struct.msg* (...) @alloc_msg()
store %struct.msg* %19, %struct.msg** %6
%20 = load %struct.msg*, %struct.msg** %6
%21 = icmp ne %struct.msg* %20, null
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load %struct.msg_queue*, %struct.msg_queue** %4
%24 = load %struct.msg*, %struct.msg** %6
%25 = call i32 @msg_enqueue(%struct.msg_queue* %23, %struct.msg* %24)
br label %27
26: ; preds = %18
br label %31
27: ; preds = %22
br label %28
28: ; preds = %27
%29 = load i64, i64* %7
%30 = add i64 %29, 1
store i64 %30, i64* %7
br label %14
31: ; preds = %26, %14
%32 = load i64, i64* %7
store i64 %32, i64* %3
br label %33
33: ; preds = %31, %12
%34 = load i64, i64* %3
ret i64 %34
}

LLVM IR (-O2):
%struct.msg_queue = type { i32 }
%struct.msg = type { i32 }
define i64 @alloc_msg_queue(%struct.msg_queue* %0, i64 %1) {
%3 = tail call i32 @init_msg_queue(%struct.msg_queue* %0)
%4 = icmp eq i64 %1, 0
br i1 %4, label %13, label %5
5: ; preds = %2, %9
%6 = phi i64 [ %11, %9 ], [ 0, %2 ]
%7 = tail call %struct.msg* (...) @alloc_msg()
%8 = icmp eq %struct.msg* %7, null
br i1 %8, label %13, label %9
9: ; preds = %5
%10 = tail call i32 @msg_enqueue(%struct.msg_queue* %0, %struct.msg* %7)
%11 = add nuw i64 %6, 1
%12 = icmp eq i64 %11, %1
br i1 %12, label %13, label %5
13: ; preds = %5, %9, %2
%14 = phi i64 [ 0, %2 ], [ %6, %5 ], [ %1, %9 ]
ret i64 %14
}