#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 *msg_dequeue(struct msg_queue *);
int /*<<< orphan*/ msg_enqueue(struct msg_queue *, struct msg *);
size_t msgnmove(struct msg_queue *dst, struct msg_queue *src, size_t n) {
  size_t i;
  struct msg *mptr;
  if (n == 0)
    return 0;
  for (i = 0; i <= n - 1; i++) {
    mptr = msg_dequeue(src);
    if (mptr == NULL)
      break;
    msg_enqueue(dst, mptr);
  }
  return i;
}


x86 assembly (clang, -O0):
<msgnmove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    114c <msgnmove+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11ac <msgnmove+0x8c>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
ja     11a4 <msgnmove+0x84>
mov    -0x18(%rbp),%rdi
call   1030 <msg_dequeue@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1186 <msgnmove+0x66>
jmp    11a4 <msgnmove+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <msg_enqueue@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1154 <msgnmove+0x34>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msgnmove>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1163 <msgnmove+0x43>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
add    $0xffffffffffffffff,%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <msg_dequeue@plt>
test   %rax,%rax
je     1165 <msgnmove+0x45>
mov    %r12,%rdi
mov    %rax,%rsi
call   1040 <msg_enqueue@plt>
add    $0x1,%rbx
cmp    %r14,%rbx
jbe    1140 <msgnmove+0x20>
jmp    1165 <msgnmove+0x45>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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