#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 */

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ FIONREAD;
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ print_err(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int get_respose(int *sock, char **buffer) {
  int len = 0;
  int countdown = 10;
  while (!len && countdown > 0 && ioctl(*sock, FIONREAD, &len) >= 0) {
    countdown--;
    sleep(1);
  }
  if (len <= 0) {
    print_err("No reply recieved");
    return EXIT_FAILURE;
  }
  if ((*buffer = (char *)malloc(sizeof(char) * len)) == NULL)
    return EXIT_FAILURE;
  if (read(*sock, *buffer, len) < 0) {
    print_err("Failed to read respond");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<get_respose>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0xa,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
jne    11ae <get_respose+0x5e>
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jle    11ae <get_respose+0x5e>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
setge  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11be <get_respose+0x6e>
jmp    11d6 <get_respose+0x86>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    116e <get_respose+0x1e>
cmpl   $0x0,-0x1c(%rbp)
jg     11fd <get_respose+0xad>
lea    0xe19(%rip),%rdi
call   1030 <print_err@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <get_respose+0x126>
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    122e <get_respose+0xde>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <get_respose+0x126>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    126a <get_respose+0x11a>
lea    0xdbe(%rip),%rdi
call   1030 <print_err@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <get_respose+0x126>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_respose>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
movl   $0x0,0xc(%rsp)
mov    $0xa,%ebx
mov    0x2e71(%rip),%r12
lea    0xc(%rsp),%r15
nopl   0x0(%rax)
mov    0x0(%r13),%edi
mov    (%r12),%esi
mov    %r15,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     11e0 <get_respose+0x90>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
jne    11b1 <get_respose+0x61>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     1180 <get_respose+0x30>
test   %edi,%edi
jle    11e8 <get_respose+0x98>
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11fd <get_respose+0xad>
mov    0x0(%r13),%edi
mov    0xc(%rsp),%edx
mov    %rax,%rsi
call   1050 <read@plt>
test   %rax,%rax
js     11f1 <get_respose+0xa1>
mov    0x2df2(%rip),%rax
jmp    1204 <get_respose+0xb4>
mov    0xc(%rsp),%edi
test   %edi,%edi
jg     11b5 <get_respose+0x65>
lea    0xe11(%rip),%rdi
jmp    11f8 <get_respose+0xa8>
lea    0xe1a(%rip),%rdi
call   1030 <print_err@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@FIONREAD = global i32 0
@.str = [18 x i8] c"No reply recieved\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [23 x i8] c"Failed to read respond\00"
@EXIT_SUCCESS = global i32 0
define i32 @get_respose(i32* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 10, i32* %7
br label %8
8: ; preds = %22, %2
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %8
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = load i32*, i32** %4
%16 = load i32, i32* %15
%17 = load i32, i32* @FIONREAD
%18 = call i64 @ioctl(i32 %16, i32 %17, i32* %6)
%19 = icmp sge i64 %18, 0
br label %20
20: ; preds = %14, %11, %8
%21 = phi i1 [ false, %11 ], [ false, %8 ], [ %19, %14 ]
br i1 %21, label %22, label %26
22: ; preds = %20
%23 = load i32, i32* %7
%24 = add nsw i32 %23, -1
store i32 %24, i32* %7
%25 = call i32 @sleep(i32 1)
br label %8
26: ; preds = %20
%27 = load i32, i32* %6
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 @print_err(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
store i32 %31, i32* %3
br label %56
32: ; preds = %26
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = mul i64 1, %34
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i8*
%39 = load i8**, i8*** %5
store i8* %38, i8** %39
%40 = icmp eq i8* %38, null
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = load i32, i32* @EXIT_FAILURE
store i32 %42, i32* %3
br label %56
43: ; preds = %32
%44 = load i32*, i32** %4
%45 = load i32, i32* %44
%46 = load i8**, i8*** %5
%47 = load i8*, i8** %46
%48 = load i32, i32* %6
%49 = call i64 @read(i32 %45, i8* %47, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %43
%52 = call i32 @print_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
store i32 %53, i32* %3
br label %56
54: ; preds = %43
%55 = load i32, i32* @EXIT_SUCCESS
store i32 %55, i32* %3
br label %56
56: ; preds = %54, %51, %41, %29
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@.str = [18 x i8] c"No reply recieved\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [23 x i8] c"Failed to read respond\00"
@EXIT_SUCCESS = global i32 0
define i32 @get_respose(i32* nocapture %0, i8** nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
br label %5
5: ; preds = %2, %11
%6 = phi i32 [ %12, %11 ], [ 10, %2 ]
%7 = load i32, i32* %0
%8 = load i32, i32* @FIONREAD
%9 = call i64 @ioctl(i32 %7, i32 %8, i32* %3)
%10 = icmp sgt i64 %9, -1
br i1 %10, label %11, label %18
11: ; preds = %5
%12 = add nsw i32 %6, -1
%13 = call i32 @sleep(i32 1)
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
%16 = icmp ugt i32 %6, 1
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %5, label %20
18: ; preds = %5
%19 = load i32, i32* %3
br label %20
20: ; preds = %11, %18
%21 = phi i32 [ %19, %18 ], [ %14, %11 ]
%22 = icmp slt i32 %21, 1
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = call i64 @malloc(i32 %21)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %1
%26 = icmp eq i64 %24, 0
br i1 %26, label %35, label %27
27: ; preds = %23
%28 = load i32, i32* %0
%29 = load i32, i32* %3
%30 = call i64 @read(i32 %28, i8* %25, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27, %20
%33 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %20 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %27 ]
%34 = call i32 @print_err(i8* %33)
br label %35
35: ; preds = %32, %27, %23
%36 = phi i32* [ @EXIT_FAILURE, %23 ], [ @EXIT_SUCCESS, %27 ], [ @EXIT_FAILURE, %32 ]
%37 = load i32, i32* %36
ret i32 %37
}