#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 addrinfo {
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_protocol;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOPNOTSUPP;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ xgetaddrinfo(char const *const, char const *const,
                                struct addrinfo *, struct addrinfo **);
bool sock_test_tcpulp(const char *const remoteaddr, const char *const port) {
  struct addrinfo hints = {
      .ai_protocol = IPPROTO_TCP,
      .ai_socktype = SOCK_STREAM,
  };
  struct addrinfo *a, *addr;
  int sock = -1, ret = 0;
  bool test_pass = false;
  hints.ai_family = AF_INET;
  xgetaddrinfo(remoteaddr, port, &hints, &addr);
  for (a = addr; a; a = a->ai_next) {
    sock = socket(a->ai_family, a->ai_socktype, IPPROTO_TCP);
    if (sock < 0) {
      perror("socket");
      continue;
    }
    ret = setsockopt(sock, IPPROTO_TCP, 31, "mptcp", sizeof("mptcp"));
    if (ret == -1 && errno == EOPNOTSUPP)
      test_pass = true;
    close(sock);
    if (test_pass)
      break;
    if (!ret)
      fprintf(stderr, "setsockopt(TCP_ULP) returned 0\n");
    else
      perror("setsockopt(TCP_ULP)");
  }
  return test_pass;
}


x86 assembly (clang, -O0):
<sock_test_tcpulp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <xgetaddrinfo@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12c7 <sock_test_tcpulp+0x167>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1219 <sock_test_tcpulp+0xb9>
lea    0xdf1(%rip),%rdi
call   1060 <perror@plt>
jmp    12b6 <sock_test_tcpulp+0x156>
mov    -0x3c(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    $0x1f,%edx
lea    0xdd6(%rip),%rcx
mov    $0x6,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    126a <sock_test_tcpulp+0x10a>
mov    0x2d68(%rip),%rax
mov    (%rax),%rax
mov    0x2d6e(%rip),%rcx
cmp    (%rcx),%rax
jne    126a <sock_test_tcpulp+0x10a>
movl   $0x1,-0x44(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x44(%rbp)
je     1281 <sock_test_tcpulp+0x121>
jmp    12c7 <sock_test_tcpulp+0x167>
cmpl   $0x0,-0x40(%rbp)
jne    12a5 <sock_test_tcpulp+0x145>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd72(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12b1 <sock_test_tcpulp+0x151>
lea    0xd81(%rip),%rdi
call   1060 <perror@plt>
jmp    12b6 <sock_test_tcpulp+0x156>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11d5 <sock_test_tcpulp+0x75>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_test_tcpulp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e55(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1070 <xgetaddrinfo@plt>
mov    0x8(%rsp),%r12
xor    %eax,%eax
test   %r12,%r12
je     1275 <sock_test_tcpulp+0x115>
lea    0xe42(%rip),%r13
jmp    11e3 <sock_test_tcpulp+0x83>
lea    0xe32(%rip),%rdi
xchg   %ax,%ax
call   1060 <perror@plt>
mov    0x8(%r12),%r12
test   %r12,%r12
je     1273 <sock_test_tcpulp+0x113>
mov    (%r12),%esi
mov    0x4(%r12),%edi
mov    (%r15),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11c7 <sock_test_tcpulp+0x67>
mov    %eax,%ebp
mov    (%r15),%esi
mov    %eax,%edi
mov    $0x1f,%edx
mov    %r13,%rcx
mov    $0x6,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1240 <sock_test_tcpulp+0xe0>
mov    %eax,%r14d
mov    %ebp,%edi
call   1040 <close@plt>
test   %r14d,%r14d
jne    1260 <sock_test_tcpulp+0x100>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11d5 <sock_test_tcpulp+0x75>
nopl   (%rax)
mov    0x2d71(%rip),%rax
mov    (%rax),%r14
mov    0x2d77(%rip),%rax
mov    (%rax),%rbx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    %rbx,%r14
je     126c <sock_test_tcpulp+0x10c>
lea    0xdc6(%rip),%rdi
jmp    11d0 <sock_test_tcpulp+0x70>
mov    $0x1,%eax
jmp    1275 <sock_test_tcpulp+0x115>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [6 x i8] c"mptcp\00"
@errno = global i64 0
@EOPNOTSUPP = global i64 0
@stderr = global i32 0
@.str.2 = [32 x i8] c"setsockopt(TCP_ULP) returned 0\0A\00"
@.str.3 = [20 x i8] c"setsockopt(TCP_ULP)\00"
define i32 @sock_test_tcpulp(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 0
%12 = load i32, i32* @SOCK_STREAM
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 1
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store %struct.addrinfo* null, %struct.addrinfo** %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 3
%16 = load i32, i32* @IPPROTO_TCP
store i32 %16, i32* %15
store i32 -1, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i8*, i8** %3
%20 = load i8*, i8** %4
%21 = call i32 @xgetaddrinfo(i8* %19, i8* %20, %struct.addrinfo* %5, %struct.addrinfo** %7)
%22 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %22, %struct.addrinfo** %6
br label %23
23: ; preds = %65, %2
%24 = load %struct.addrinfo*, %struct.addrinfo** %6
%25 = icmp ne %struct.addrinfo* %24, null
br i1 %25, label %26, label %69
26: ; preds = %23
%27 = load %struct.addrinfo*, %struct.addrinfo** %6
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = load %struct.addrinfo*, %struct.addrinfo** %6
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @IPPROTO_TCP
%34 = call i32 @socket(i32 %29, i32 %32, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %65
39: ; preds = %26
%40 = load i32, i32* %8
%41 = load i32, i32* @IPPROTO_TCP
%42 = call i32 @setsockopt(i32 %40, i32 %41, i32 31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 6)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i64, i64* @errno
%47 = load i64, i64* @EOPNOTSUPP
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 1, i32* %10
br label %50
50: ; preds = %49, %45, %39
%51 = load i32, i32* %8
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %10
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
br label %69
56: ; preds = %50
%57 = load i32, i32* %9
%58 = icmp ne i32 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %64
62: ; preds = %56
%63 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %64
64: ; preds = %62, %59
br label %65
65: ; preds = %64, %37
%66 = load %struct.addrinfo*, %struct.addrinfo** %6
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 2
%68 = load %struct.addrinfo*, %struct.addrinfo** %67
store %struct.addrinfo* %68, %struct.addrinfo** %6
br label %23
69: ; preds = %55, %23
%70 = load i32, i32* %10
ret i32 %70
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [6 x i8] c"mptcp\00"
@errno = global i64 0
@EOPNOTSUPP = global i64 0
@stderr = global i32 0
@.str.2 = [32 x i8] c"setsockopt(TCP_ULP) returned 0\0A\00"
@.str.3 = [20 x i8] c"setsockopt(TCP_ULP)\00"
define i32 @sock_test_tcpulp(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
%7 = load i32, i32* @SOCK_STREAM
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store %struct.addrinfo* null, %struct.addrinfo** %9
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
%11 = load i32, i32* @IPPROTO_TCP
store i32 %11, i32* %10
%12 = bitcast %struct.addrinfo** %4 to i8*
%13 = load i32, i32* @AF_INET
store i32 %13, i32* %8
%14 = call i32 @xgetaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %49, label %17
17: ; preds = %2, %45
%18 = phi %struct.addrinfo* [ %47, %45 ], [ %15, %2 ]
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @IPPROTO_TCP
%24 = call i32 @socket(i32 %20, i32 %22, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %45
28: ; preds = %17
%29 = load i32, i32* @IPPROTO_TCP
%30 = call i32 @setsockopt(i32 %24, i32 %29, i32 31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 6)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @EOPNOTSUPP
%35 = icmp eq i64 %33, %34
%36 = call i32 @close(i32 %24)
br i1 %35, label %49, label %43
37: ; preds = %28
%38 = call i32 @close(i32 %24)
%39 = icmp eq i32 %30, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %45
43: ; preds = %32, %37
%44 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %40, %43, %26
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%47 = load %struct.addrinfo*, %struct.addrinfo** %46
%48 = icmp eq %struct.addrinfo* %47, null
br i1 %48, label %49, label %17
49: ; preds = %45, %32, %2
%50 = phi i32 [ 0, %2 ], [ 1, %32 ], [ 0, %45 ]
ret i32 %50
}