#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;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert_fd(char *, int /*<<< orphan*/);
int clientsocket();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int serversocket();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
void test_socket(void) {
  int sock;
  pid_t pid;
  int sockfds[2];
  assert_fd("socketpair", socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds));
  close(sockfds[0]);
  close(sockfds[1]);
  pid = fork();
  if (pid < 0) {
    perror("fork");
    return;
  }
  if (!pid) {
    sleep(1);
    sock = clientsocket();
    if (sock < 0) {
      fprintf(stderr, "ERROR: client failed to get connection!\n");
    } else {
      printf("SUCCESS: client got connection: %d!\n", sock);
      close(sock);
    }
    exit(0);
  }
  sock = serversocket();
  if (sock < 0) {
    fprintf(stderr, "ERROR: server failed to get connection!\n");
  } else {
    printf("SUCCESS: server got connection: %d!\n", sock);
    close(sock);
  }
}


x86 assembly (clang, -O0):
<test_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
mov    %eax,%esi
lea    0xe22(%rip),%rdi
call   1030 <assert_fd@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    121a <test_socket+0x6a>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
jmp    12c0 <test_socket+0x110>
cmpq   $0x0,-0x10(%rbp)
jne    127b <test_socket+0xcb>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    $0x0,%al
call   10a0 <clientsocket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    125d <test_socket+0xad>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1274 <test_socket+0xc4>
mov    -0x4(%rbp),%esi
lea    0xdd2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1050 <serversocket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    12a9 <test_socket+0xf9>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12c0 <test_socket+0x110>
mov    -0x4(%rbp),%esi
lea    0xdd4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_socket>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
lea    0xe26(%rip),%rdi
mov    %eax,%esi
call   1030 <assert_fd@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1240 <test_socket+0x90>
je     124e <test_socket+0x9e>
xor    %eax,%eax
call   1050 <serversocket@plt>
test   %eax,%eax
js     1225 <test_socket+0x75>
mov    %eax,%ebx
lea    0xe72(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    123a <test_socket+0x8a>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1070 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdc4(%rip),%rdi
call   1090 <perror@plt>
jmp    123a <test_socket+0x8a>
mov    $0x1,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   10a0 <clientsocket@plt>
test   %eax,%eax
jns    127f <test_socket+0xcf>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    %eax,%ebx
lea    0xdb1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"socketpair\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@stderr = global i32 0
@.str.2 = [41 x i8] c"ERROR: client failed to get connection!\0A\00"
@.str.3 = [37 x i8] c"SUCCESS: client got connection: %d!\0A\00"
@.str.4 = [41 x i8] c"ERROR: server failed to get connection!\0A\00"
@.str.5 = [37 x i8] c"SUCCESS: server got connection: %d!\0A\00"
define void @test_socket() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i32]
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @socketpair(i32 %4, i32 %5, i32 0, i32* %6)
%8 = call i32 @assert_fd(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = load i32, i32* %9
%11 = call i32 @close(i32 %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = call i64 (...) @fork()
store i64 %15, i64* %2
%16 = load i64, i64* %2
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %50
20: ; preds = %0
%21 = load i64, i64* %2
%22 = icmp ne i64 %21, 0
br i1 %22, label %38, label %23
23: ; preds = %20
%24 = call i32 @sleep(i32 1)
%25 = call i32 (...) @clientsocket()
store i32 %25, i32* %1
%26 = load i32, i32* %1
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %36
31: ; preds = %23
%32 = load i32, i32* %1
%33 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %1
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %31, %28
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %20
%39 = call i32 (...) @serversocket()
store i32 %39, i32* %1
%40 = load i32, i32* %1
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %50
45: ; preds = %38
%46 = load i32, i32* %1
%47 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %1
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %18, %45, %42
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"socketpair\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@stderr = global i32 0
@.str.2 = [41 x i8] c"ERROR: client failed to get connection!\0A\00"
@.str.3 = [37 x i8] c"SUCCESS: client got connection: %d!\0A\00"
@.str.4 = [41 x i8] c"ERROR: server failed to get connection!\0A\00"
@.str.5 = [37 x i8] c"SUCCESS: server got connection: %d!\0A\00"
define void @test_socket() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @socketpair(i32 %3, i32 %4, i32 0, i32* %5)
%7 = call i32 @assert_fd(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* %5
%9 = call i32 @close(i32 %8)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%11 = load i32, i32* %10
%12 = call i32 @close(i32 %11)
%13 = call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %40
17: ; preds = %0
%18 = icmp eq i64 %13, 0
br i1 %18, label %19, label %31
19: ; preds = %17
%20 = call i32 @sleep(i32 1)
%21 = call i32 (...) @clientsocket()
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %29
26: ; preds = %19
%27 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%28 = call i32 @close(i32 %21)
br label %29
29: ; preds = %26, %23
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %17
%32 = call i32 (...) @serversocket()
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %40
37: ; preds = %31
%38 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%39 = call i32 @close(i32 %32)
br label %40
40: ; preds = %34, %37, %15
ret void
}