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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
char char_queue_dequeue(struct char_queue *);
int /*<<< orphan*/ char_queue_is_empty(struct char_queue *);
size_t char_queue_size(struct char_queue *);
size_t char_queue_dequeue_line(struct char_queue *queue, char *str,
                               const size_t n) {
  assert(!char_queue_is_empty(queue));
  const size_t iqs = char_queue_size(queue);
  size_t i;
  for (i = 0; i < iqs && i < (n - 1); ++i) {
    const char c = char_queue_dequeue(queue);
    if (c == '\n') {
      break;
    } else {
      str[i] = c;
    }
  }
  str[i] = 0;
  return i;
}


x86 assembly (clang, -O0):
<char_queue_dequeue_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <char_queue_is_empty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <char_queue_size@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x20(%rbp),%rcx
mov    %al,-0x2a(%rbp)
jae    11ac <char_queue_dequeue_line+0x6c>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    11bc <char_queue_dequeue_line+0x7c>
jmp    11fe <char_queue_dequeue_line+0xbe>
mov    -0x8(%rbp),%rdi
call   1050 <char_queue_dequeue@plt>
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    11da <char_queue_dequeue_line+0x9a>
jmp    11fe <char_queue_dequeue_line+0xbe>
mov    -0x29(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11ed <char_queue_dequeue_line+0xad>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1184 <char_queue_dequeue_line+0x44>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<char_queue_dequeue_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
call   1060 <char_queue_is_empty@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    %r15,%rdi
call   1030 <char_queue_size@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11ac <char_queue_dequeue_line+0x6c>
cmp    $0x1,%r12
je     11ac <char_queue_dequeue_line+0x6c>
add    $0xffffffffffffffff,%rax
add    $0xfffffffffffffffe,%r12
cmp    %r12,%rax
cmovb  %rax,%r12
add    $0x1,%r12
xor    %ebx,%ebx
nopl   (%rax)
mov    %r15,%rdi
call   1050 <char_queue_dequeue@plt>
cmp    $0xa,%al
je     11ac <char_queue_dequeue_line+0x6c>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1190 <char_queue_dequeue_line+0x50>
mov    %r12,%rbx
movb   $0x0,(%r14,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.char_queue = type { i32 }
define i64 @char_queue_dequeue_line(%struct.char_queue* %0, i8* %1, i64 %2) {
%4 = alloca %struct.char_queue*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8
store %struct.char_queue* %0, %struct.char_queue** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load %struct.char_queue*, %struct.char_queue** %4
%11 = call i32 @char_queue_is_empty(%struct.char_queue* %10)
%12 = icmp ne i32 %11, 0
%13 = xor i1 %12, true
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load %struct.char_queue*, %struct.char_queue** %4
%17 = call i64 @char_queue_size(%struct.char_queue* %16)
store i64 %17, i64* %7
store i64 0, i64* %8
br label %18
18: ; preds = %42, %3
%19 = load i64, i64* %8
%20 = load i64, i64* %7
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i64, i64* %8
%24 = load i64, i64* %6
%25 = sub i64 %24, 1
%26 = icmp ult i64 %23, %25
br label %27
27: ; preds = %22, %18
%28 = phi i1 [ false, %18 ], [ %26, %22 ]
br i1 %28, label %29, label %45
29: ; preds = %27
%30 = load %struct.char_queue*, %struct.char_queue** %4
%31 = call signext i8 @char_queue_dequeue(%struct.char_queue* %30)
store i8 %31, i8* %9
%32 = load i8, i8* %9
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 10
br i1 %34, label %35, label %36
35: ; preds = %29
br label %45
36: ; preds = %29
%37 = load i8, i8* %9
%38 = load i8*, i8** %5
%39 = load i64, i64* %8
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 %37, i8* %40
br label %41
41: ; preds = %36
br label %42
42: ; preds = %41
%43 = load i64, i64* %8
%44 = add i64 %43, 1
store i64 %44, i64* %8
br label %18
45: ; preds = %35, %27
%46 = load i8*, i8** %5
%47 = load i64, i64* %8
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
%49 = load i64, i64* %8
ret i64 %49
}

LLVM IR (-O2):
%struct.char_queue = type { i32 }
define i64 @char_queue_dequeue_line(%struct.char_queue* %0, i8* nocapture writeonly %1, i64 %2) {
%4 = tail call i32 @char_queue_is_empty(%struct.char_queue* %0)
%5 = icmp eq i32 %4, 0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = tail call i64 @char_queue_size(%struct.char_queue* %0)
%9 = icmp ne i64 %8, 0
%10 = icmp ne i64 %2, 1
%11 = and i1 %9, %10
br i1 %11, label %12, label %25
12: ; preds = %3
%13 = add i64 %8, -1
%14 = add i64 %2, -2
%15 = call i64 @llvm.umin.i64(i64 %13, i64 %14)
%16 = add nuw i64 %15, 1
br label %17
17: ; preds = %12, %21
%18 = phi i64 [ %23, %21 ], [ 0, %12 ]
%19 = tail call signext i8 @char_queue_dequeue(%struct.char_queue* %0)
%20 = icmp eq i8 %19, 10
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = getelementptr inbounds i8, i8* %1, i64 %18
store i8 %19, i8* %22
%23 = add nuw i64 %18, 1
%24 = icmp eq i64 %18, %15
br i1 %24, label %25, label %17
25: ; preds = %21, %17, %3
%26 = phi i64 [ 0, %3 ], [ %18, %17 ], [ %16, %21 ]
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 0, i8* %27
ret i64 %26
}