#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 /*<<< orphan*/ sun_family;
  int /*<<< orphan*/ sun_path;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_unix_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ send_fd_using_sockfd(int, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
void send_fd(int fd) {
  int sockfd, len;
  struct sockaddr_un server_unix_addr;
  bzero((char *)&server_unix_addr, sizeof(server_unix_addr));
  strcpy(server_unix_addr.sun_path, "/tmp/fd_trans_socket");
  server_unix_addr.sun_family = AF_LOCAL;
  len = strlen("/tmp/fd_trans_socket") + 1;
  len += sizeof(server_unix_addr.sun_family);
  if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return;
  }
  int i, ret = -1;
  for (i = 1; i < 3 && ret < 0; i++) {
    ret = connect(sockfd, (struct sockaddr *)&server_unix_addr, len);
    if (ret < 0) {
      perror("sender: connect");
      sleep(1);
    }
  }
  if (ret >= 0) {
    if ((send_fd_using_sockfd(fd, sockfd) < 0))
      fprintf(stderr, "sender: failed to send file descriptor (fd = %d)\n", fd);
  } else {
    perror("sender: connect");
  }
  sleep(1);
  close(sockfd);
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    0xe3d(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe25(%rip),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0xc(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1227 <send_fd+0x87>
lea    0xdf1(%rip),%rdi
call   1070 <perror@plt>
jmp    12fd <send_fd+0x15d>
movl   $0xffffffff,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x3,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
jge    124e <send_fd+0xae>
cmpl   $0x0,-0x20(%rbp)
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    125e <send_fd+0xbe>
jmp    12a3 <send_fd+0x103>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10a0 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1290 <send_fd+0xf0>
lea    0xd94(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1295 <send_fd+0xf5>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1235 <send_fd+0x95>
cmpl   $0x0,-0x20(%rbp)
jl     12df <send_fd+0x13f>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1090 <send_fd_using_sockfd@plt>
cmp    $0x0,%rax
jge    12da <send_fd+0x13a>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xd50(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12eb <send_fd+0x14b>
lea    0xd2f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r12d
mov    %rsp,%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
mov    0x4(%rsp),%edi
lea    0xe3a(%rip),%rbx
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e13(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1298 <send_fd+0xf8>
mov    %eax,%r13d
add    $0x5,%ebx
mov    $0x1,%bpl
mov    %rsp,%r15
mov    %r13d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   10a0 <connect@plt>
test   %eax,%eax
jns    124f <send_fd+0xaf>
sets   %r14b
lea    0xded(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
test   %r14b,%bpl
mov    $0x0,%ebp
jne    120c <send_fd+0x6c>
lea    0xdcd(%rip),%rdi
call   1070 <perror@plt>
jmp    1277 <send_fd+0xd7>
mov    %r12d,%edi
mov    %r13d,%esi
call   1090 <send_fd_using_sockfd@plt>
test   %rax,%rax
jns    1277 <send_fd+0xd7>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
mov    %r12d,%edx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %r13d,%edi
call   1050 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6f(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <perror@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"/tmp/fd_trans_socket\00"
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [16 x i8] c"sender: connect\00"
@stderr = global i32 0
@.str.3 = [50 x i8] c"sender: failed to send file descriptor (fd = %d)\0A\00"
define void @send_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = bitcast %struct.sockaddr_un* %5 to i8*
%9 = call i32 @bzero(i8* %8, i32 8)
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
%11 = load i32, i32* %10
%12 = call i32 @strcpy(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @AF_LOCAL
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
store i32 %13, i32* %14
%15 = call i32 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%16 = add nsw i32 %15, 1
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = add i64 %18, 4
%20 = trunc i64 %19 to i32
store i32 %20, i32* %4
%21 = load i32, i32* @AF_LOCAL
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %1
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %69
27: ; preds = %1
store i32 -1, i32* %7
store i32 1, i32* %6
br label %28
28: ; preds = %47, %27
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 3
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br label %34
34: ; preds = %31, %28
%35 = phi i1 [ false, %28 ], [ %33, %31 ]
br i1 %35, label %36, label %50
36: ; preds = %34
%37 = load i32, i32* %3
%38 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%39 = load i32, i32* %4
%40 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @sleep(i32 1)
br label %46
46: ; preds = %43, %36
br label %47
47: ; preds = %46
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
br label %28
50: ; preds = %34
%51 = load i32, i32* %7
%52 = icmp sge i32 %51, 0
br i1 %52, label %53, label %63
53: ; preds = %50
%54 = load i32, i32* %2
%55 = load i32, i32* %3
%56 = call i64 @send_fd_using_sockfd(i32 %54, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %53
%59 = load i32, i32* @stderr
%60 = load i32, i32* %2
%61 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %58, %53
br label %65
63: ; preds = %50
%64 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %65
65: ; preds = %63, %62
%66 = call i32 @sleep(i32 1)
%67 = load i32, i32* %3
%68 = call i32 @close(i32 %67)
br label %69
69: ; preds = %65, %25
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"/tmp/fd_trans_socket\00"
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [16 x i8] c"sender: connect\00"
@stderr = global i32 0
@.str.3 = [50 x i8] c"sender: failed to send file descriptor (fd = %d)\0A\00"
define void @send_fd(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = call i32 @bzero(i8* %3, i32 8)
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
%6 = load i32, i32* %5
%7 = tail call i32 @strcpy(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @AF_LOCAL
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
store i32 %8, i32* %9
%10 = tail call i32 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%11 = add i32 %10, 5
%12 = load i32, i32* @AF_LOCAL
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %1
%17 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
br label %20
18: ; preds = %1
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %39
20: ; preds = %16, %24
%21 = phi i1 [ true, %16 ], [ false, %24 ]
%22 = call i32 @connect(i32 %14, %struct.sockaddr* %17, i32 %11)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @sleep(i32 1)
%27 = select i1 %21, i1 %23, i1 false
br i1 %27, label %20, label %34
28: ; preds = %20
%29 = call i64 @send_fd_using_sockfd(i32 %0, i32 %14)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %36
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %28, %31, %34
%37 = call i32 @sleep(i32 1)
%38 = call i32 @close(i32 %14)
br label %39
39: ; preds = %36, %18
ret void
}