#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 sockaddr_un {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cl_un;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int receive_fd_using_sockfd(int *, int);
int setup_server(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int receive_fd(void) {
  int fd = -1, sockfd;
  if ((sockfd = setup_server("/tmp/fd_trans_socket")) < 0) {
    fprintf(stderr, "receiver: failed to create socket.\n");
    return fd;
  }
  listen(sockfd, 0);
  int i;
  for (i = 1; i < 3 && fd < 0; i++) {
    struct sockaddr_un cl_un;
    socklen_t cl_un_len = sizeof(cl_un);
    int csockfd = accept(sockfd, (struct sockaddr *)&cl_un, &cl_un_len);
    if (csockfd < 0) {
      perror("accept");
      sleep(1);
    } else {
      int ret = receive_fd_using_sockfd(&fd, csockfd);
      if ((ret < 0) || (fd < 0))
        fprintf(stderr, "receiver: failed to receive file descriptor\n");
    }
    close(csockfd);
    sleep(1);
  }
  close(sockfd);
  return fd;
}


x86 assembly (clang, -O0):
<receive_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xffffffff,-0x8(%rbp)
lea    0xe6a(%rip),%rdi
call   1030 <setup_server@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c7 <receive_fd+0x47>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ab <receive_fd+0x12b>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1070 <listen@plt>
movl   $0x1,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x3,-0x10(%rbp)
mov    %al,-0x25(%rbp)
jge    11f1 <receive_fd+0x71>
cmpl   $0x0,-0x8(%rbp)
setl   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1201 <receive_fd+0x81>
jmp    129d <receive_fd+0x11d>
movl   $0x4,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1240 <receive_fd+0xc0>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    127d <receive_fd+0xfd>
mov    -0x20(%rbp),%esi
lea    -0x8(%rbp),%rdi
call   1060 <receive_fd_using_sockfd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1263 <receive_fd+0xe3>
cmpl   $0x0,-0x8(%rbp)
jge    1278 <receive_fd+0xf8>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
call   1050 <fprintf@plt>
jmp    127d <receive_fd+0xfd>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11d8 <receive_fd+0x58>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0xffffffff,0x8(%rsp)
ff
lea    0xe63(%rip),%rdi
call   1030 <setup_server@plt>
test   %eax,%eax
js     125f <receive_fd+0xdf>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1070 <listen@plt>
mov    $0x1,%ebp
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x8(%rsp),%r13
nopl   0x0(%rax)
movl   $0x4,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <accept@plt>
mov    %eax,%r12d
test   %eax,%eax
js     123a <receive_fd+0xba>
mov    %r13,%rdi
mov    %r12d,%esi
call   1060 <receive_fd_using_sockfd@plt>
test   %eax,%eax
js     1202 <receive_fd+0x82>
cmpl   $0x0,0x8(%rsp)
jns    1217 <receive_fd+0x97>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
cmp    $0x1,%ebp
ja     1252 <receive_fd+0xd2>
add    $0x1,%ebp
cmpl   $0x0,0x8(%rsp)
js     11d0 <receive_fd+0x50>
jmp    1252 <receive_fd+0xd2>
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1217 <receive_fd+0x97>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%eax
jmp    1279 <receive_fd+0xf9>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xda6(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"/tmp/fd_trans_socket\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"receiver: failed to create socket.\0A\00"
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [45 x i8] c"receiver: failed to receive file descriptor\0A\00"
define i32 @receive_fd() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 -1, i32* %2
%9 = call i32 @setup_server(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 %9, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %2
store i32 %14, i32* %1
br label %58
15: ; preds = %0
%16 = load i32, i32* %3
%17 = call i32 @listen(i32 %16, i32 0)
store i32 1, i32* %4
br label %18
18: ; preds = %51, %15
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 0
br label %24
24: ; preds = %21, %18
%25 = phi i1 [ false, %18 ], [ %23, %21 ]
br i1 %25, label %26, label %54
26: ; preds = %24
store i32 4, i32* %6
%27 = load i32, i32* %3
%28 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%29 = call i32 @accept(i32 %27, %struct.sockaddr* %28, i32* %6)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @sleep(i32 1)
br label %47
35: ; preds = %26
%36 = load i32, i32* %7
%37 = call i32 @receive_fd_using_sockfd(i32* %2, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = load i32, i32* %2
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40, %35
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %43, %40
br label %47
47: ; preds = %46, %32
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
%50 = call i32 @sleep(i32 1)
br label %51
51: ; preds = %47
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
store i32 %53, i32* %4
br label %18
54: ; preds = %24
%55 = load i32, i32* %3
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %2
store i32 %57, i32* %1
br label %58
58: ; preds = %54, %11
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"/tmp/fd_trans_socket\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"receiver: failed to create socket.\0A\00"
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [45 x i8] c"receiver: failed to receive file descriptor\0A\00"
define i32 @receive_fd() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
store i32 -1, i32* %1
%5 = tail call i32 @setup_server(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %42
10: ; preds = %0
%11 = tail call i32 @listen(i32 %5, i32 0)
%12 = bitcast %struct.sockaddr_un* %2 to i8*
%13 = bitcast i32* %3 to i8*
%14 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
br label %15
15: ; preds = %10, %31
%16 = phi i32 [ 1, %10 ], [ %34, %31 ]
store i32 4, i32* %3
%17 = call i32 @accept(i32 %5, %struct.sockaddr* %14, i32* %3)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @sleep(i32 1)
br label %31
22: ; preds = %15
%23 = call i32 @receive_fd_using_sockfd(i32* %1, i32 %17)
%24 = icmp slt i32 %23, 0
%25 = load i32, i32* %1
%26 = icmp slt i32 %25, 0
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %28, %22, %19
%32 = call i32 @close(i32 %17)
%33 = call i32 @sleep(i32 1)
%34 = add nuw nsw i32 %16, 1
%35 = icmp ult i32 %16, 2
%36 = load i32, i32* %1
%37 = icmp slt i32 %36, 0
%38 = select i1 %35, i1 %37, i1 false
br i1 %38, label %15, label %39
39: ; preds = %31
%40 = call i32 @close(i32 %5)
%41 = load i32, i32* %1
br label %42
42: ; preds = %39, %7
%43 = phi i32 [ %41, %39 ], [ -1, %7 ]
ret i32 %43
}