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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ _cmd(char *);
int /*<<< orphan*/ _interpret_status(int, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ diod_log_init(char *);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ err(char *, int /*<<< orphan*/);
int /*<<< orphan*/ err_exit(char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int system(char *);
int /*<<< orphan*/ usage();
scalar_t__ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                   int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int cs = -1, s[2];
  char *cmd1, *cmd2;
  pid_t pid;
  if (argc != 3)
    usage();
  cmd1 = argv[1];
  cmd2 = argv[2];
  diod_log_init(argv[0]);
  if (socketpair(AF_LOCAL, SOCK_STREAM, 0, s) < 0)
    err_exit("socketpair");
  switch ((pid = fork())) {
  case -1:
    err_exit("fork");
  case 0:
    close(s[0]);
    if (dup2(s[1], 0) < 0)
      err_exit("dup2 for %s leg", _cmd(cmd2));
    close(s[1]);
    if ((cs = system(cmd2)) == -1)
      err_exit("fork for %s leg", _cmd(cmd2));
    _interpret_status(cs, _cmd(cmd2));
    exit(0);
  default:
    close(s[1]);
    if (dup2(s[0], 0) < 0) {
      err("dup2 for %s leg", _cmd(cmd1));
      break;
    }
    close(s[0]);
    if ((cs = system(cmd1)) == -1)
      err("fork for %s leg", _cmd(cmd1));
    close(0);
    break;
  }
  close(0);
  if (waitpid(pid, NULL, 0) < 0)
    err_exit("waitpid");
  if (cs != -1)
    _interpret_status(cs, _cmd(cmd1));
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11fe <main+0x2e>
mov    $0x0,%al
call   10a0 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <diod_log_init@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rcx
xor    %edx,%edx
call   1060 <socketpair@plt>
cmp    $0x0,%rax
jge    1257 <main+0x87>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1080 <err_exit@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,-0x34(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     1284 <main+0xb4>
jmp    1274 <main+0xa4>
mov    -0x38(%rbp),%eax
test   %eax,%eax
je     1292 <main+0xc2>
jmp    131c <main+0x14c>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1080 <err_exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0x0,%eax
jge    12c6 <main+0xf6>
mov    -0x30(%rbp),%rdi
call   1090 <_cmd@plt>
mov    %eax,%esi
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1080 <err_exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12fc <main+0x12c>
mov    -0x30(%rbp),%rdi
call   1090 <_cmd@plt>
mov    %eax,%esi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1080 <err_exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <_cmd@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%esi
call   10e0 <_interpret_status@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0x0,%eax
jge    1353 <main+0x183>
mov    -0x28(%rbp),%rdi
call   1090 <_cmd@plt>
mov    %eax,%esi
lea    0xcc7(%rip),%rdi
call   1070 <err@plt>
jmp    138e <main+0x1be>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1387 <main+0x1b7>
mov    -0x28(%rbp),%rdi
call   1090 <_cmd@plt>
mov    %eax,%esi
lea    0xc9e(%rip),%rdi
call   1070 <err@plt>
xor    %edi,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x34(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    $0x0,%rax
jge    13bb <main+0x1eb>
lea    0xc7c(%rip),%rdi
mov    $0x0,%al
call   1080 <err_exit@plt>
cmpl   $0xffffffff,-0x14(%rbp)
je     13de <main+0x20e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <_cmd@plt>
mov    -0x40(%rbp),%edi
mov    %eax,%esi
call   10e0 <_interpret_status@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
je     11e7 <main+0x17>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    (%rbx),%rdi
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%rbx
call   10d0 <diod_log_init@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1060 <socketpair@plt>
test   %rax,%rax
jns    1228 <main+0x58>
lea    0xddf(%rip),%rdi
xor    %eax,%eax
call   1080 <err_exit@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1248 <main+0x78>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12b7 <main+0xe7>
lea    0xdca(%rip),%rdi
xor    %eax,%eax
call   1080 <err_exit@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
test   %eax,%eax
jns    1278 <main+0xa8>
mov    %rbx,%rdi
call   1090 <_cmd@plt>
lea    0xda1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <err_exit@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1030 <system@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12a8 <main+0xd8>
mov    %rbx,%rdi
call   1090 <_cmd@plt>
lea    0xd81(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <err_exit@plt>
mov    %rbx,%rdi
call   1090 <_cmd@plt>
mov    %ebp,%edi
jmp    1355 <main+0x185>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
test   %eax,%eax
jns    12ec <main+0x11c>
mov    %r14,%rdi
call   1090 <_cmd@plt>
lea    0xd32(%rip),%rdi
mov    %eax,%esi
call   1070 <err@plt>
mov    $0xffffffff,%ebx
jmp    1321 <main+0x151>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    %r14,%rdi
call   1030 <system@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    131a <main+0x14a>
mov    %r14,%rdi
call   1090 <_cmd@plt>
lea    0xd0d(%rip),%rdi
mov    %eax,%esi
call   1070 <err@plt>
xor    %edi,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <waitpid@plt>
test   %rax,%rax
jns    1363 <main+0x193>
lea    0xcf1(%rip),%rdi
xor    %eax,%eax
call   1080 <err_exit@plt>
cmp    $0xffffffff,%ebx
je     1368 <main+0x198>
mov    %r14,%rdi
call   1090 <_cmd@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   10e0 <_interpret_status@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
cmp    $0xffffffff,%ebx
jne    134b <main+0x17b>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [16 x i8] c"dup2 for %s leg\00"
@.str.3 = [16 x i8] c"fork for %s leg\00"
@.str.4 = [8 x i8] c"waitpid\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (...) @usage()
br label %15
15: ; preds = %13, %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %8
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
store i8* %21, i8** %9
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @diod_log_init(i8* %24)
%26 = load i32, i32* @AF_LOCAL
%27 = load i32, i32* @SOCK_STREAM
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%29 = call i64 @socketpair(i32 %26, i32 %27, i32 0, i32* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %15
%32 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %15
%34 = call i32 (...) @fork()
store i32 %34, i32* %10
switch i32 %34, label %66 [
i32 -1, label %35
i32 0, label %37
]
35: ; preds = %33
%36 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %33, %35
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @dup2(i32 %42, i32 0)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %37
%46 = load i8*, i8** %9
%47 = call i32 @_cmd(i8* %46)
%48 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %45, %37
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = load i8*, i8** %9
%54 = call i32 @system(i8* %53)
store i32 %54, i32* %6
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %49
%57 = load i8*, i8** %9
%58 = call i32 @_cmd(i8* %57)
%59 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %56, %49
%61 = load i32, i32* %6
%62 = load i8*, i8** %9
%63 = call i32 @_cmd(i8* %62)
%64 = call i32 @_interpret_status(i32 %61, i32 %63)
%65 = call i32 @exit(i32 0)
unreachable
66: ; preds = %33
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%71 = load i32, i32* %70
%72 = call i32 @dup2(i32 %71, i32 0)
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %66
%75 = load i8*, i8** %8
%76 = call i32 @_cmd(i8* %75)
%77 = call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %76)
br label %91
78: ; preds = %66
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%80 = load i32, i32* %79
%81 = call i32 @close(i32 %80)
%82 = load i8*, i8** %8
%83 = call i32 @system(i8* %82)
store i32 %83, i32* %6
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %89
85: ; preds = %78
%86 = load i8*, i8** %8
%87 = call i32 @_cmd(i8* %86)
%88 = call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %87)
br label %89
89: ; preds = %85, %78
%90 = call i32 @close(i32 0)
br label %91
91: ; preds = %89, %74
%92 = call i32 @close(i32 0)
%93 = load i32, i32* %10
%94 = call i64 @waitpid(i32 %93, i32* null, i32 0)
%95 = icmp slt i64 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %91
%97 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %98
98: ; preds = %96, %91
%99 = load i32, i32* %6
%100 = icmp ne i32 %99, -1
br i1 %100, label %101, label %106
101: ; preds = %98
%102 = load i32, i32* %6
%103 = load i8*, i8** %8
%104 = call i32 @_cmd(i8* %103)
%105 = call i32 @_interpret_status(i32 %102, i32 %104)
br label %106
106: ; preds = %101, %98
%107 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [16 x i8] c"dup2 for %s leg\00"
@.str.3 = [16 x i8] c"fork for %s leg\00"
@.str.4 = [8 x i8] c"waitpid\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (...) @usage()
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = load i8*, i8** %1
%14 = tail call i32 @diod_log_init(i8* %13)
%15 = load i32, i32* @AF_LOCAL
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%18 = call i64 @socketpair(i32 %15, i32 %16, i32 0, i32* %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %8
%21 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %8
%23 = call i32 (...) @fork()
switch i32 %23, label %48 [
i32 -1, label %24
i32 0, label %26
]
24: ; preds = %22
%25 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %22, %24
%27 = load i32, i32* %17
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @dup2(i32 %30, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 @_cmd(i8* %12)
%35 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %33, %26
%37 = load i32, i32* %29
%38 = call i32 @close(i32 %37)
%39 = call i32 @system(i8* %12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = call i32 @_cmd(i8* %12)
%43 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41, %36
%45 = call i32 @_cmd(i8* %12)
%46 = call i32 @_interpret_status(i32 %39, i32 %45)
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %22
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %17
%53 = call i32 @dup2(i32 %52, i32 0)
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = call i32 @_cmd(i8* %10)
%57 = call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %56)
br label %68
58: ; preds = %48
%59 = load i32, i32* %17
%60 = call i32 @close(i32 %59)
%61 = call i32 @system(i8* %10)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = call i32 @_cmd(i8* %10)
%65 = call i32 @err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %63, %58
%67 = call i32 @close(i32 0)
br label %68
68: ; preds = %66, %55
%69 = phi i32 [ -1, %55 ], [ %61, %66 ]
%70 = call i32 @close(i32 0)
%71 = call i64 @waitpid(i32 %23, i32* null, i32 0)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %68
%74 = call i32 (i8*, ...) @err_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %75
75: ; preds = %73, %68
%76 = icmp eq i32 %69, -1
br i1 %76, label %80, label %77
77: ; preds = %75
%78 = call i32 @_cmd(i8* %10)
%79 = call i32 @_interpret_status(i32 %69, i32 %78)
br label %80
80: ; preds = %77, %75
%81 = call i32 @exit(i32 0)
unreachable
}