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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ EINTR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int);
scalar_t__ errno;
scalar_t__ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ system_error(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
FILE *check_rw_socket(char *command, pid_t *pid) {
  int sockets[2], status;
  pid_t wres;
  FILE *res;
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0)
    system_error("Failed to create socket pair!");
  *pid = fork();
  if (*pid < 0)
    system_error("Failed to fork new process!");
  if (!*pid) {
    if (dup2(sockets[1], 0) < 0)
      system_error("Failed to reopen stdin!");
    if (dup2(sockets[1], 1) < 0)
      system_error("Failed to reopen stdout!");
    close(sockets[0]);
    if (execlp("sh", "sh", "-c", command, NULL) < 0)
      system_error("Failed to exec command!");
  }
  close(sockets[1]);
  res = fdopen(sockets[0], "r+");
  if (!res)
    system_error("Failed to convert socket to stream!");
  do {
    wres = waitpid(*pid, &status, WNOHANG);
  } while ((wres < 0) && (errno == EINTR));
  if (wres < 0) {
    fprintf(stderr, "[Error] Failed to start child process: %s\n", command);
    exit(1);
  }
  return res;
}


x86 assembly (clang, -O0):
<check_rw_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rcx
xor    %edx,%edx
call   1060 <socketpair@plt>
cmp    $0x0,%rax
jge    11e3 <check_rw_socket+0x43>
lea    0xe22(%rip),%rdi
call   1080 <system_error@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jge    120e <check_rw_socket+0x6e>
lea    0xe15(%rip),%rdi
call   1080 <system_error@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12a1 <check_rw_socket+0x101>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    123c <check_rw_socket+0x9c>
lea    0xe03(%rip),%rdi
call   1080 <system_error@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    125f <check_rw_socket+0xbf>
lea    0xdf8(%rip),%rdi
call   1080 <system_error@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rcx
lea    0xdf9(%rip),%rsi
lea    0xdf5(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10b0 <execlp@plt>
cmp    $0x0,%rax
jge    129c <check_rw_socket+0xfc>
lea    0xdda(%rip),%rdi
call   1080 <system_error@plt>
jmp    12a1 <check_rw_socket+0x101>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
lea    0xdd6(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12d3 <check_rw_socket+0x133>
lea    0xdbe(%rip),%rdi
call   1080 <system_error@plt>
jmp    12d8 <check_rw_socket+0x138>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2cfa(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   1090 <waitpid@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x31(%rbp)
jge    131f <check_rw_socket+0x17f>
mov    0x2cb4(%rip),%rax
mov    (%rax),%rax
mov    0x2ca2(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    12d8 <check_rw_socket+0x138>
cmpq   $0x0,-0x28(%rbp)
jge    1358 <check_rw_socket+0x1b8>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd67(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_rw_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
xor    %edx,%edx
call   1060 <socketpair@plt>
test   %rax,%rax
jns    11e1 <check_rw_socket+0x41>
lea    0xe24(%rip),%rdi
call   1080 <system_error@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,(%r12)
test   %rax,%rax
js     11fc <check_rw_socket+0x5c>
test   %rax,%rax
jne    1281 <check_rw_socket+0xe1>
jmp    1211 <check_rw_socket+0x71>
lea    0xe1b(%rip),%rdi
call   1080 <system_error@plt>
mov    (%r12),%rax
test   %rax,%rax
jne    1281 <check_rw_socket+0xe1>
mov    0x4(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
test   %rax,%rax
jns    122d <check_rw_socket+0x8d>
lea    0xe12(%rip),%rdi
call   1080 <system_error@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
test   %rax,%rax
jns    124c <check_rw_socket+0xac>
lea    0xe0b(%rip),%rdi
call   1080 <system_error@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
lea    0xe10(%rip),%rdi
lea    0xe0c(%rip),%rdx
mov    %rdi,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   10b0 <execlp@plt>
test   %rax,%rax
jns    1281 <check_rw_socket+0xe1>
lea    0xdf5(%rip),%rdi
call   1080 <system_error@plt>
mov    %rbx,0x8(%rsp)
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
lea    0xdf0(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    12b2 <check_rw_socket+0x112>
lea    0xddf(%rip),%rdi
call   1080 <system_error@plt>
mov    0x2d27(%rip),%r13
lea    0x14(%rsp),%rbx
mov    0x2cfb(%rip),%rbp
mov    0x2cec(%rip),%r14
nopl   0x0(%rax)
mov    (%r12),%rdi
mov    0x0(%r13),%edx
mov    %rbx,%rsi
call   1090 <waitpid@plt>
test   %rax,%rax
jns    12ee <check_rw_socket+0x14e>
mov    (%r14),%rcx
cmp    %rcx,0x0(%rbp)
je     12d0 <check_rw_socket+0x130>
test   %rax,%rax
js     1305 <check_rw_socket+0x165>
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
mov    0x8(%rsp),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [30 x i8] c"Failed to create socket pair!\00"
@.str.1 = [28 x i8] c"Failed to fork new process!\00"
@.str.2 = [24 x i8] c"Failed to reopen stdin!\00"
@.str.3 = [25 x i8] c"Failed to reopen stdout!\00"
@.str.4 = [3 x i8] c"sh\00"
@.str.5 = [3 x i8] c"-c\00"
@.str.6 = [24 x i8] c"Failed to exec command!\00"
@.str.7 = [3 x i8] c"r+\00"
@.str.8 = [36 x i8] c"Failed to convert socket to stream!\00"
@WNOHANG = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str.9 = [43 x i8] c"[Error] Failed to start child process: %s\0A\00"
define i32* @check_rw_socket(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
store i8* %0, i8** %3
store i64* %1, i64** %4
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%12 = call i64 @socketpair(i32 %9, i32 %10, i32 0, i32* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @system_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = call i64 (...) @fork()
%18 = load i64*, i64** %4
store i64 %17, i64* %18
%19 = load i64*, i64** %4
%20 = load i64, i64* %19
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @system_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = load i64*, i64** %4
%26 = load i64, i64* %25
%27 = icmp ne i64 %26, 0
br i1 %27, label %52, label %28
28: ; preds = %24
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i64 @dup2(i32 %30, i32 0)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @system_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i64 @dup2(i32 %37, i32 1)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @system_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %3
%47 = call i64 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %46, i32* null)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = call i32 @system_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %49, %42
br label %52
52: ; preds = %51, %24
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%57 = load i32, i32* %56
%58 = call i32* @fdopen(i32 %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %58, i32** %8
%59 = load i32*, i32** %8
%60 = icmp ne i32* %59, null
br i1 %60, label %63, label %61
61: ; preds = %52
%62 = call i32 @system_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0))
br label %63
63: ; preds = %61, %52
br label %64
64: ; preds = %76, %63
%65 = load i64*, i64** %4
%66 = load i64, i64* %65
%67 = load i32, i32* @WNOHANG
%68 = call i64 @waitpid(i64 %66, i32* %6, i32 %67)
store i64 %68, i64* %7
br label %69
69: ; preds = %64
%70 = load i64, i64* %7
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %69
%73 = load i64, i64* @errno
%74 = load i64, i64* @EINTR
%75 = icmp eq i64 %73, %74
br label %76
76: ; preds = %72, %69
%77 = phi i1 [ false, %69 ], [ %75, %72 ]
br i1 %77, label %64, label %78
78: ; preds = %76
%79 = load i64, i64* %7
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %86
81: ; preds = %78
%82 = load i32, i32* @stderr
%83 = load i8*, i8** %3
%84 = call i32 @fprintf(i32 %82, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0), i8* %83)
%85 = call i32 @exit(i32 1)
unreachable
86: ; preds = %78
%87 = load i32*, i32** %8
ret i32* %87
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [30 x i8] c"Failed to create socket pair!\00"
@.str.1 = [28 x i8] c"Failed to fork new process!\00"
@.str.2 = [24 x i8] c"Failed to reopen stdin!\00"
@.str.3 = [25 x i8] c"Failed to reopen stdout!\00"
@.str.4 = [3 x i8] c"sh\00"
@.str.5 = [3 x i8] c"-c\00"
@.str.6 = [24 x i8] c"Failed to exec command!\00"
@.str.7 = [3 x i8] c"r+\00"
@.str.8 = [36 x i8] c"Failed to convert socket to stream!\00"
@WNOHANG = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str.9 = [43 x i8] c"[Error] Failed to start child process: %s\0A\00"
define noalias i32* @check_rw_socket(i8* %0, i64* nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i64 @socketpair(i32 %7, i32 %8, i32 0, i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @system_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i64 (...) @fork()
store i64 %15, i64* %1
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @system_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%19 = load i64, i64* %1
br label %20
20: ; preds = %17, %14
%21 = phi i64 [ %19, %17 ], [ %15, %14 ]
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %20
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i64 @dup2(i32 %25, i32 0)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @system_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = load i32, i32* %24
%32 = call i64 @dup2(i32 %31, i32 1)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @system_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = load i32, i32* %9
%38 = call i32 @close(i32 %37)
%39 = call i64 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32* null)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @system_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %36, %41, %20
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %9
%48 = call i32* @fdopen(i32 %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %52
50: ; preds = %43
%51 = call i32 @system_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0))
br label %52
52: ; preds = %50, %43
br label %53
53: ; preds = %52, %53
%54 = load i64, i64* %1
%55 = load i32, i32* @WNOHANG
%56 = call i64 @waitpid(i64 %54, i32* %4, i32 %55)
%57 = icmp slt i64 %56, 0
%58 = load i64, i64* @errno
%59 = load i64, i64* @EINTR
%60 = icmp eq i64 %58, %59
%61 = select i1 %57, i1 %60, i1 false
br i1 %61, label %53, label %62
62: ; preds = %53
br i1 %57, label %63, label %67
63: ; preds = %62
%64 = load i32, i32* @stderr
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0), i8* %0)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %62
ret i32* %48
}