#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_un {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ cl_un;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int accept(int, struct sockaddr *, int *);
int fd_receive_using_sockfd(int *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int fd_receive(int sockfd, pid_t from) {
  int fd = -1;
  int status;
  fd_set readfds;
  struct timeval timeout;
  listen(sockfd, 0);
  for (;;) {
    struct sockaddr_un cl_un;
    socklen_t cl_un_len = sizeof(cl_un);
    FD_ZERO(&readfds);
    FD_SET(sockfd, &readfds);
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    if (select(sockfd + 1, &readfds, NULL, NULL, &timeout) >= 0 &&
        FD_ISSET(sockfd, &readfds)) {
      int csockfd = accept(sockfd, (struct sockaddr *)&cl_un, &cl_un_len);
      if (csockfd < 0) {
        perror("accept");
      } else {
        int ret = fd_receive_using_sockfd(&fd, csockfd);
        if (ret < 0)
          fprintf(stderr, "receiver: failed to receive file descriptor\n");
        break;
      }
    }
    if (waitpid(from, &status, WNOHANG) > 0 && WIFEXITED(status)) {
      fprintf(stderr, "accept: child process died\n");
      break;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<fd_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1070 <listen@plt>
movl   $0x4,-0x34(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10d0 <FD_SET@plt>
movl   $0x1,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x0,%rax
jl     129a <fd_receive+0xea>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     129a <fd_receive+0xea>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1262 <fd_receive+0xb2>
lea    0xda8(%rip),%rdi
call   10a0 <perror@plt>
jmp    1295 <fd_receive+0xe5>
mov    -0x38(%rbp),%esi
lea    -0xc(%rbp),%rdi
call   1080 <fd_receive_using_sockfd@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1290 <fd_receive+0xe0>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
call   1040 <fprintf@plt>
jmp    12ea <fd_receive+0x13a>
jmp    129a <fd_receive+0xea>
mov    -0x8(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rsi
call   1090 <waitpid@plt>
cmp    $0x0,%rax
jle    12e5 <fd_receive+0x135>
mov    -0x10(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12e5 <fd_receive+0x135>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0xd59(%rip),%rsi
call   1040 <fprintf@plt>
jmp    12ea <fd_receive+0x13a>
jmp    11cf <fd_receive+0x1f>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_receive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r15d
mov    %edi,%ebx
movl   $0xffffffff,0x4(%rsp)
ff
xor    %esi,%esi
call   1070 <listen@plt>
lea    0x1(%rbx),%r12d
lea    0x14(%rsp),%rbp
lea    0x18(%rsp),%r13
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
movl   $0x4,0x8(%rsp)
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
movl   $0x1,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1060 <select@plt>
test   %rax,%rax
js     1262 <fd_receive+0xb2>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1262 <fd_receive+0xb2>
mov    %ebx,%edi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   10b0 <accept@plt>
test   %eax,%eax
jns    129a <fd_receive+0xea>
lea    0xda3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r14,%rsi
call   1090 <waitpid@plt>
test   %rax,%rax
jle    11f0 <fd_receive+0x40>
mov    0xc(%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
je     11f0 <fd_receive+0x40>
lea    0xd9c(%rip),%rsi
jmp    12b1 <fd_receive+0x101>
lea    0x4(%rsp),%rdi
mov    %eax,%esi
call   1080 <fd_receive_using_sockfd@plt>
test   %eax,%eax
jns    12bf <fd_receive+0x10f>
lea    0xd56(%rip),%rsi
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   1040 <fprintf@plt>
mov    0x4(%rsp),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"receiver: failed to receive file descriptor\0A\00"
@WNOHANG = global i32 0
@.str.2 = [28 x i8] c"accept: child process died\0A\00"
define i32 @fd_receive(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca %struct.sockaddr_un
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 -1, i32* %5
%13 = load i32, i32* %3
%14 = call i32 @listen(i32 %13, i32 0)
br label %15
15: ; preds = %59, %2
store i32 4, i32* %10
%16 = call i32 @FD_ZERO(i32* %7)
%17 = load i32, i32* %3
%18 = call i32 @FD_SET(i32 %17, i32* %7)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
%23 = call i64 @select(i32 %22, i32* %7, i32* null, i32* null, %struct.timeval* %8)
%24 = icmp sge i64 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %15
%26 = load i32, i32* %3
%27 = call i64 @FD_ISSET(i32 %26, i32* %7)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %47
29: ; preds = %25
%30 = load i32, i32* %3
%31 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%32 = call i32 @accept(i32 %30, %struct.sockaddr* %31, i32* %10)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %46
37: ; preds = %29
%38 = load i32, i32* %11
%39 = call i32 @fd_receive_using_sockfd(i32* %5, i32 %38)
store i32 %39, i32* %12
%40 = load i32, i32* %12
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %45
45: ; preds = %42, %37
br label %60
46: ; preds = %35
br label %47
47: ; preds = %46, %25, %15
%48 = load i32, i32* %4
%49 = load i32, i32* @WNOHANG
%50 = call i64 @waitpid(i32 %48, i32* %6, i32 %49)
%51 = icmp sgt i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %47
%53 = load i32, i32* %6
%54 = call i64 @WIFEXITED(i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %60
59: ; preds = %52, %47
br label %15
60: ; preds = %56, %45
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"receiver: failed to receive file descriptor\0A\00"
@WNOHANG = global i32 0
@.str.2 = [28 x i8] c"accept: child process died\0A\00"
define i32 @fd_receive(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca %struct.sockaddr_un
%8 = alloca i32
%9 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%10 = bitcast i32* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = bitcast %struct.timeval* %6 to i8*
%13 = tail call i32 @listen(i32 %0, i32 0)
%14 = bitcast %struct.sockaddr_un* %7 to i8*
%15 = bitcast i32* %8 to i8*
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%18 = add nsw i32 %0, 1
%19 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
br label %20
20: ; preds = %44, %2
store i32 4, i32* %8
%21 = call i32 @FD_ZERO(i32* %5)
%22 = call i32 @FD_SET(i32 %0, i32* %5)
store i32 1, i32* %16
store i64 0, i64* %17
%23 = call i64 @select(i32 %18, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%24 = icmp sgt i64 %23, -1
br i1 %24, label %25, label %36
25: ; preds = %20
%26 = call i64 @FD_ISSET(i32 %0, i32* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = call i32 @accept(i32 %0, %struct.sockaddr* %19, i32* %8)
%30 = icmp slt i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = call i32 @fd_receive_using_sockfd(i32* %3, i32 %29)
%33 = icmp slt i32 %32, 0
br i1 %33, label %45, label %49
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %25, %20
%37 = load i32, i32* @WNOHANG
%38 = call i64 @waitpid(i32 %1, i32* %4, i32 %37)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i32, i32* %4
%42 = call i64 @WIFEXITED(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %36, %40
br label %20
45: ; preds = %40, %31
%46 = phi i8* [ getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), %31 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), %40 ]
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* %46)
br label %49
49: ; preds = %45, %31
%50 = load i32, i32* %3
ret i32 %50
}