#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 */
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int parse_plugin_output(char *, double *, char *);
int /*<<< orphan*/ stderr;
int read_and_process_plugin_output(int read_fd, double *progress, char *msg) {
  int bytes_avail;
  char buf[1024];
  FILE *read_stream;
  ioctl(read_fd, FIONREAD, &bytes_avail);
  if (bytes_avail <= 0) {
    fprintf(stderr, "*** No report available from plugin. Ctrl-C to stop\n");
    return -1;
  }
  read_stream = fdopen(read_fd, "r");
  if (fgets(buf, sizeof(buf), read_stream) == NULL)
    return -1;
  fprintf(stderr, "*** Output read from plugin : %s", buf);
  return parse_plugin_output(buf, progress, msg);
}


x86 assembly (clang, -O0):
<read_and_process_plugin_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1030 <ioctl@plt>
cmpl   $0x0,-0x1c(%rbp)
jg     11a8 <read_and_process_plugin_output+0x58>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <read_and_process_plugin_output+0xd1>
mov    -0x8(%rbp),%edi
lea    0xe83(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11ec <read_and_process_plugin_output+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <read_and_process_plugin_output+0xd1>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <parse_plugin_output@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_and_process_plugin_output>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
call   1030 <ioctl@plt>
cmpl   $0x0,0xc(%rsp)
jle    11d1 <read_and_process_plugin_output+0x81>
lea    0xeb1(%rip),%rsi
mov    %ebp,%edi
call   1060 <fdopen@plt>
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11e8 <read_and_process_plugin_output+0x98>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <parse_plugin_output@plt>
jmp    11ed <read_and_process_plugin_output+0x9d>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@FIONREAD = global i32 0
@stderr = global i32 0
@.str = [53 x i8] c"*** No report available from plugin. Ctrl-C to stop\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"*** Output read from plugin : %s\00"
define i32 @read_and_process_plugin_output(i32 %0, double* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca double*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32*
store i32 %0, i32* %5
store double* %1, double** %6
store i8* %2, i8** %7
%11 = load i32, i32* %5
%12 = load i32, i32* @FIONREAD
%13 = call i32 @ioctl(i32 %11, i32 %12, i32* %8)
%14 = load i32, i32* %8
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %35
19: ; preds = %3
%20 = load i32, i32* %5
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %10
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%23 = load i32*, i32** %10
%24 = call i32* @fgets(i8* %22, i32 1024, i32* %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -1, i32* %4
br label %35
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%32 = load double*, double** %6
%33 = load i8*, i8** %7
%34 = call i32 @parse_plugin_output(i8* %31, double* %32, i8* %33)
store i32 %34, i32* %4
br label %35
35: ; preds = %27, %26, %16
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@stderr = global i32 0
@.str = [53 x i8] c"*** No report available from plugin. Ctrl-C to stop\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"*** Output read from plugin : %s\00"
define i32 @read_and_process_plugin_output(i32 %0, double* %1, i8* %2) {
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @FIONREAD
%9 = call i32 @ioctl(i32 %0, i32 %8, i32* %4)
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
br label %23
15: ; preds = %3
%16 = call i32* @fdopen(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32* @fgets(i8* %7, i32 1024, i32* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%22 = call i32 @parse_plugin_output(i8* %7, double* %1, i8* %2)
br label %23
23: ; preds = %15, %19, %12
%24 = phi i32 [ -1, %12 ], [ %22, %19 ], [ -1, %15 ]
ret i32 %24
}