#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;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int);
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;
  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!");
  if (waitpid(*pid, &status, WNOHANG)) {
    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    $0x30,%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,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d3 <check_rw_socket+0x133>
lea    0xdbe(%rip),%rdi
call   1080 <system_error@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2cff(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   1090 <waitpid@plt>
cmp    $0x0,%rax
je     1319 <check_rw_socket+0x179>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xda6(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_rw_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rcx
xor    %edx,%edx
call   1060 <socketpair@plt>
test   %rax,%rax
jns    11de <check_rw_socket+0x3e>
lea    0xe27(%rip),%rdi
call   1080 <system_error@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,(%r15)
test   %rax,%rax
js     11f8 <check_rw_socket+0x58>
test   %rax,%rax
jne    127d <check_rw_socket+0xdd>
jmp    120c <check_rw_socket+0x6c>
lea    0xe1f(%rip),%rdi
call   1080 <system_error@plt>
mov    (%r15),%rax
test   %rax,%rax
jne    127d <check_rw_socket+0xdd>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
test   %rax,%rax
jns    1228 <check_rw_socket+0x88>
lea    0xe17(%rip),%rdi
call   1080 <system_error@plt>
mov    0x8(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
test   %rax,%rax
jns    1247 <check_rw_socket+0xa7>
lea    0xe10(%rip),%rdi
call   1080 <system_error@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
lea    0xe14(%rip),%rdi
lea    0xe10(%rip),%rdx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   10b0 <execlp@plt>
test   %rax,%rax
jns    127d <check_rw_socket+0xdd>
lea    0xdf9(%rip),%rdi
call   1080 <system_error@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
lea    0xdf8(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12aa <check_rw_socket+0x10a>
lea    0xde7(%rip),%rdi
call   1080 <system_error@plt>
mov    (%r15),%rdi
mov    0x2d2c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
call   1090 <waitpid@plt>
test   %rax,%rax
jne    12d2 <check_rw_socket+0x132>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
mov    %r14,%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
@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 i32*
store i8* %0, i8** %3
store i64* %1, i64** %4
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%11 = call i64 @socketpair(i32 %8, i32 %9, i32 0, i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @system_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i64 (...) @fork()
%17 = load i64*, i64** %4
store i64 %16, i64* %17
%18 = load i64*, i64** %4
%19 = load i64, i64* %18
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @system_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = load i64*, i64** %4
%25 = load i64, i64* %24
%26 = icmp ne i64 %25, 0
br i1 %26, label %51, label %27
27: ; preds = %23
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i64 @dup2(i32 %29, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @system_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i64 @dup2(i32 %36, i32 1)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @system_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = load i8*, i8** %3
%46 = 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* %45, i32* null)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @system_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %50
50: ; preds = %48, %41
br label %51
51: ; preds = %50, %23
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32* @fdopen(i32 %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %57, i32** %7
%58 = load i32*, i32** %7
%59 = icmp ne i32* %58, null
br i1 %59, label %62, label %60
60: ; preds = %51
%61 = call i32 @system_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0))
br label %62
62: ; preds = %60, %51
%63 = load i64*, i64** %4
%64 = load i64, i64* %63
%65 = load i32, i32* @WNOHANG
%66 = call i64 @waitpid(i64 %64, i32* %6, i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %62
%69 = load i32, i32* @stderr
%70 = load i8*, i8** %3
%71 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0), i8* %70)
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %62
%74 = load i32*, i32** %7
ret i32* %74
}

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
@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
%53 = load i64, i64* %1
%54 = load i32, i32* @WNOHANG
%55 = call i64 @waitpid(i64 %53, i32* %4, i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %52
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0), i8* %0)
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %52
ret i32* %48
}