#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 /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ child_process(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ parent_process(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  int sock[2];
  if (socketpair(PF_UNIX, SOCK_STREAM, 0, sock)) {
    perror("socketpair");
    exit(1);
  } else {
    printf("Established socket pair: (%d, %d)\n", sock[0], sock[1]);
  }
  switch (fork()) {
  case 0:
    close(sock[0]);
    child_process(sock[1]);
    break;
  case -1:
    perror("fork");
    exit(1);
  default:
    close(sock[1]);
    parent_process(sock[0]);
    wait(NULL);
    break;
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0x0,%rax
je     11dc <main+0x4c>
lea    0xe33(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     122d <main+0x9d>
jmp    1208 <main+0x78>
mov    -0x10(%rbp),%eax
test   %eax,%eax
jne    1243 <main+0xb3>
jmp    1218 <main+0x88>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <child_process@plt>
jmp    125c <main+0xcc>
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1030 <parent_process@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
test   %rax,%rax
jne    1204 <main+0x74>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11ef <main+0x5f>
cmp    $0xffffffff,%eax
je     120d <main+0x7d>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
call   1030 <parent_process@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    1200 <main+0x70>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <child_process@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xdf5(%rip),%rdi
jmp    1214 <main+0x84>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@.str.1 = [35 x i8] c"Established socket pair: (%d, %d)\0A\00"
@.str.2 = [5 x i8] c"fork\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
store i32 0, i32* %1
%3 = load i32, i32* @PF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i64 @socketpair(i32 %3, i32 %4, i32 0, i32* %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %15)
br label %17
17: ; preds = %11
%18 = call i32 (...) @fork()
switch i32 %18, label %29 [
i32 0, label %19
i32 -1, label %26
]
19: ; preds = %17
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @child_process(i32 %24)
br label %37
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @parent_process(i32 %34)
%36 = call i32 @wait(i32* null)
br label %37
37: ; preds = %29, %19
ret i32 0
}

LLVM IR (-O2):
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@.str.1 = [35 x i8] c"Established socket pair: (%d, %d)\0A\00"
@.str.2 = [5 x i8] c"fork\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = load i32, i32* @PF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @socketpair(i32 %3, i32 %4, i32 0, i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %5
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %14)
%16 = call i32 (...) @fork()
switch i32 %16, label %25 [
i32 0, label %17
i32 -1, label %22
]
17: ; preds = %11
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %13
%21 = call i32 @child_process(i32 %20)
br label %31
22: ; preds = %11
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %11
%26 = load i32, i32* %13
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %5
%29 = call i32 @parent_process(i32 %28)
%30 = call i32 @wait(i32* null)
br label %31
31: ; preds = %25, %17
ret i32 0
}