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

/* Variables and functions */
scalar_t__ mread_closed(struct mread_pool *);
int mread_poll(struct mread_pool *, int /*<<< orphan*/);
char *mread_pull(struct mread_pool *, int);
int /*<<< orphan*/ mread_yield(struct mread_pool *);
int /*<<< orphan*/ printf(char *, int, ...);
void test(struct mread_pool *m) {
  int id = mread_poll(m, 0);
  if (id >= 0) {
    for (;;) {
      char *buffer = mread_pull(m, 4);
      if (buffer == NULL) {
        if (mread_closed(m)) {
          printf("%d: CLOSED\n", id);
        }
        break;
      } else {
        printf("%d : %d %d %d %d\n", id, buffer[0], buffer[1], buffer[2],
               buffer[3]);
        mread_yield(m);
      }
    }
  }
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1050 <mread_poll@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1204 <test+0xb4>
jmp    1179 <test+0x29>
mov    -0x8(%rbp),%rdi
mov    $0x4,%esi
call   1030 <mread_pull@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bf <test+0x6f>
mov    -0x8(%rbp),%rdi
call   1070 <mread_closed@plt>
cmp    $0x0,%rax
je     11ba <test+0x6a>
mov    -0xc(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ff <test+0xaf>
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rax
movsbl (%rax),%edx
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%ecx
mov    -0x18(%rbp),%rax
movsbl 0x2(%rax),%r8d
mov    -0x18(%rbp),%rax
movsbl 0x3(%rax),%r9d
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <mread_yield@plt>
jmp    1179 <test+0x29>
jmp    1204 <test+0xb4>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %esi,%esi
call   1050 <mread_poll@plt>
test   %eax,%eax
js     11d8 <test+0x88>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x4,%esi
call   1030 <mread_pull@plt>
test   %rax,%rax
je     11b7 <test+0x67>
lea    0xe8f(%rip),%r14
nopl   (%rax)
movsbl (%rax),%edx
movsbl 0x1(%rax),%ecx
movsbl 0x2(%rax),%r8d
movsbl 0x3(%rax),%r9d
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <mread_yield@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1030 <mread_pull@plt>
test   %rax,%rax
jne    1180 <test+0x30>
mov    %rbx,%rdi
call   1070 <mread_closed@plt>
test   %rax,%rax
je     11d8 <test+0x88>
lea    0xe35(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <printf@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.mread_pool = type { i32 }
@.str = [12 x i8] c"%d: CLOSED\0A\00"
@.str.1 = [18 x i8] c"%d : %d %d %d %d\0A\00"
define void @test(%struct.mread_pool* %0) {
%2 = alloca %struct.mread_pool*
%3 = alloca i32
%4 = alloca i8*
store %struct.mread_pool* %0, %struct.mread_pool** %2
%5 = load %struct.mread_pool*, %struct.mread_pool** %2
%6 = call i32 @mread_poll(%struct.mread_pool* %5, i32 0)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %46
9: ; preds = %1
br label %10
10: ; preds = %44, %9
%11 = load %struct.mread_pool*, %struct.mread_pool** %2
%12 = call i8* @mread_pull(%struct.mread_pool* %11, i32 4)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %23
15: ; preds = %10
%16 = load %struct.mread_pool*, %struct.mread_pool** %2
%17 = call i64 @mread_closed(%struct.mread_pool* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %3
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %19, %15
br label %45
23: ; preds = %10
%24 = load i32, i32* %3
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 2
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 3
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %28, i32 %32, i32 %36, i32 %40)
%42 = load %struct.mread_pool*, %struct.mread_pool** %2
%43 = call i32 @mread_yield(%struct.mread_pool* %42)
br label %44
44: ; preds = %23
br label %10
45: ; preds = %22
br label %46
46: ; preds = %45, %1
ret void
}

LLVM IR (-O2):
%struct.mread_pool = type { i32 }
@.str = [12 x i8] c"%d: CLOSED\0A\00"
@.str.1 = [18 x i8] c"%d : %d %d %d %d\0A\00"
define void @test(%struct.mread_pool* %0) {
%2 = tail call i32 @mread_poll(%struct.mread_pool* %0, i32 0)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %29
4: ; preds = %1
%5 = tail call i8* @mread_pull(%struct.mread_pool* %0, i32 4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %12, %4
%8 = tail call i64 @mread_closed(%struct.mread_pool* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %29, label %10
10: ; preds = %7
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %29
12: ; preds = %4, %12
%13 = phi i8* [ %27, %12 ], [ %5, %4 ]
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = getelementptr inbounds i8, i8* %13, i64 1
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = getelementptr inbounds i8, i8* %13, i64 2
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = getelementptr inbounds i8, i8* %13, i64 3
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %15, i32 %18, i32 %21, i32 %24)
%26 = tail call i32 @mread_yield(%struct.mread_pool* %0)
%27 = tail call i8* @mread_pull(%struct.mread_pool* %0, i32 4)
%28 = icmp eq i8* %27, null
br i1 %28, label %7, label %12
29: ; preds = %7, %10, %1
ret void
}