#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*/ response;
typedef int /*<<< orphan*/ converted;

/* Variables and functions */
int /*<<< orphan*/ PRINT_ERR(char *);
int htonl(int);
int ntohl(int);
scalar_t__ read(int, int *, int);
scalar_t__ write(int, int *, int);
int query_receiver_busy_state(int sockfd) {
  int request = (int)'X';
  int converted = htonl(request);
  int response = 0;
  if (write(sockfd, &converted, sizeof(converted)) < 0) {
    PRINT_ERR("cannot write data to the socket for sender/receiver sync");
    return -1;
  }
  if (read(sockfd, &response, sizeof(response)) <= 0) {
    PRINT_ERR("cannot read data from the socket for sender/receiver sync");
    return -1;
  }
  if (ntohl(response) == 12) {
    PRINT_ERR("receiver is busy with an existing test running");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<query_receiver_busy_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x58,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11a7 <query_receiver_busy_state+0x57>
lea    0xe6a(%rip),%rdi
call   1070 <PRINT_ERR@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <query_receiver_busy_state+0xba>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jg     11da <query_receiver_busy_state+0x8a>
lea    0xe70(%rip),%rdi
call   1070 <PRINT_ERR@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <query_receiver_busy_state+0xba>
mov    -0x14(%rbp),%edi
call   1060 <ntohl@plt>
cmp    $0xc,%eax
jne    1203 <query_receiver_busy_state+0xb3>
lea    0xe81(%rip),%rdi
call   1070 <PRINT_ERR@plt>
movl   $0x1,-0x4(%rbp)
jmp    120a <query_receiver_busy_state+0xba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<query_receiver_busy_state>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x58,%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
movl   $0x0,(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     11b6 <query_receiver_busy_state+0x66>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11bf <query_receiver_busy_state+0x6f>
mov    (%rsp),%edi
call   1060 <ntohl@plt>
xor    %ebx,%ebx
cmp    $0xc,%eax
jne    11cb <query_receiver_busy_state+0x7b>
mov    $0x1,%ebx
lea    0xebf(%rip),%rdi
jmp    11c6 <query_receiver_busy_state+0x76>
lea    0xe43(%rip),%rdi
jmp    11c6 <query_receiver_busy_state+0x76>
lea    0xe73(%rip),%rdi
call   1070 <PRINT_ERR@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [57 x i8] c"cannot write data to the socket for sender/receiver sync\00"
@.str.1 = [58 x i8] c"cannot read data from the socket for sender/receiver sync\00"
@.str.2 = [47 x i8] c"receiver is busy with an existing test running\00"
define i32 @query_receiver_busy_state(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 88, i32* %4
%7 = load i32, i32* %4
%8 = call i32 @htonl(i32 %7)
store i32 %8, i32* %5
store i32 0, i32* %6
%9 = load i32, i32* %3
%10 = call i64 @write(i32 %9, i32* %5, i32 4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @PRINT_ERR(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
14: ; preds = %1
%15 = load i32, i32* %3
%16 = call i64 @read(i32 %15, i32* %6, i32 4)
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @PRINT_ERR(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
20: ; preds = %14
%21 = load i32, i32* %6
%22 = call i32 @ntohl(i32 %21)
%23 = icmp eq i32 %22, 12
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @PRINT_ERR(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %27
26: ; preds = %20
store i32 0, i32* %2
br label %27
27: ; preds = %26, %24, %18, %12
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [57 x i8] c"cannot write data to the socket for sender/receiver sync\00"
@.str.1 = [58 x i8] c"cannot read data from the socket for sender/receiver sync\00"
@.str.2 = [47 x i8] c"receiver is busy with an existing test running\00"
define i32 @query_receiver_busy_state(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @htonl(i32 88)
store i32 %5, i32* %2
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = call i64 @write(i32 %0, i32* %2, i32 4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %1
%10 = call i64 @read(i32 %0, i32* %3, i32 4)
%11 = icmp slt i64 %10, 1
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i32, i32* %3
%14 = tail call i32 @ntohl(i32 %13)
%15 = icmp eq i32 %14, 12
br i1 %15, label %16, label %20
16: ; preds = %12, %9, %1
%17 = phi i8* [ getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), %12 ]
%18 = phi i32 [ -1, %1 ], [ -1, %9 ], [ 1, %12 ]
%19 = tail call i32 @PRINT_ERR(i8* %17)
br label %20
20: ; preds = %16, %12
%21 = phi i32 [ 0, %12 ], [ %18, %16 ]
ret i32 %21
}