#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 msghdr {
  int dummy;
};
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINPROGRESS;
scalar_t__ ETIMEDOUT;
int /*<<< orphan*/ MSG_FASTOPEN;
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ POLLOUT;
scalar_t__ errno;
int poll_one(int, int /*<<< orphan*/, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
ssize_t send_process_tfo(int fd, struct msghdr *msg, int timeout_ms) {
  int ret = sendmsg(fd, msg, MSG_FASTOPEN);
  if (ret != 0 && errno == EINPROGRESS) {
    if (poll_one(fd, POLLOUT, timeout_ms) != 1) {
      errno = ETIMEDOUT;
      return -1;
    }
    ret = sendmsg(fd, msg, MSG_NOSIGNAL);
  }
  return ret;
}


x86 assembly (clang, -O0):
<send_process_tfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e98(%rip),%rax
mov    (%rax),%edx
call   1040 <sendmsg@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11c3 <send_process_tfo+0xa3>
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%rax
jne    11c3 <send_process_tfo+0xa3>
mov    -0x8(%rbp),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <poll_one@plt>
cmp    $0x1,%eax
je     11ab <send_process_tfo+0x8b>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rcx
mov    0x2e2c(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <send_process_tfo+0xa9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   1040 <sendmsg@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_process_tfo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ea2(%rip),%rax
mov    (%rax),%edx
call   1040 <sendmsg@plt>
test   %eax,%eax
je     118c <send_process_tfo+0x6c>
mov    0x2e80(%rip),%r15
mov    (%r15),%rcx
mov    0x2e8e(%rip),%rdx
cmp    (%rdx),%rcx
jne    11a2 <send_process_tfo+0x82>
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14d,%edx
call   1030 <poll_one@plt>
cmp    $0x1,%eax
jne    1190 <send_process_tfo+0x70>
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <sendmsg@plt>
xor    %eax,%eax
jmp    11a2 <send_process_tfo+0x82>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32 }
@MSG_FASTOPEN = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@POLLOUT = global i32 0
@ETIMEDOUT = global i64 0
@MSG_NOSIGNAL = global i32 0
define i32 @send_process_tfo(i32 %0, %struct.msghdr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.msghdr*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store %struct.msghdr* %1, %struct.msghdr** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load %struct.msghdr*, %struct.msghdr** %6
%11 = load i32, i32* @MSG_FASTOPEN
%12 = call i32 @sendmsg(i32 %9, %struct.msghdr* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %3
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINPROGRESS
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %32
19: ; preds = %15
%20 = load i32, i32* %5
%21 = load i32, i32* @POLLOUT
%22 = load i32, i32* %7
%23 = call i32 @poll_one(i32 %20, i32 %21, i32 %22)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = load i64, i64* @ETIMEDOUT
store i64 %26, i64* @errno
store i32 -1, i32* %4
br label %34
27: ; preds = %19
%28 = load i32, i32* %5
%29 = load %struct.msghdr*, %struct.msghdr** %6
%30 = load i32, i32* @MSG_NOSIGNAL
%31 = call i32 @sendmsg(i32 %28, %struct.msghdr* %29, i32 %30)
store i32 %31, i32* %8
br label %32
32: ; preds = %27, %15, %3
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %25
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.msghdr = type { i32 }
@MSG_FASTOPEN = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@POLLOUT = global i32 0
@ETIMEDOUT = global i64 0
@MSG_NOSIGNAL = global i32 0
define i32 @send_process_tfo(i32 %0, %struct.msghdr* %1, i32 %2) {
%4 = load i32, i32* @MSG_FASTOPEN
%5 = tail call i32 @sendmsg(i32 %0, %struct.msghdr* %1, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %3
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINPROGRESS
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %20
11: ; preds = %7
%12 = load i32, i32* @POLLOUT
%13 = tail call i32 @poll_one(i32 %0, i32 %12, i32 %2)
%14 = icmp eq i32 %13, 1
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = load i64, i64* @ETIMEDOUT
store i64 %16, i64* @errno
br label %20
17: ; preds = %11
%18 = load i32, i32* @MSG_NOSIGNAL
%19 = tail call i32 @sendmsg(i32 %0, %struct.msghdr* %1, i32 %18)
br label %20
20: ; preds = %3, %7, %17, %15
%21 = phi i32 [ -1, %15 ], [ %19, %17 ], [ %5, %7 ], [ 0, %3 ]
ret i32 %21
}