#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 {
  scalar_t__ sa_family;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int nodelay_connect(int (*real_connect)(int, const struct sockaddr *,
                                        socklen_t),
                    int sock, const struct sockaddr *addr, socklen_t addrlen) {
  int res = real_connect(sock, addr, addrlen);
  if (!res && addr->sa_family == AF_INET) {
    int opt = 1;
    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
  }
  return res;
}


x86 assembly (clang, -O0):
<nodelay_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   *%rax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1185 <nodelay_connect+0x75>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e8f(%rip),%rcx
cmp    (%rcx),%rax
jne    1185 <nodelay_connect+0x75>
movl   $0x1,-0x24(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nodelay_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rax
mov    %esi,%edi
mov    %rdx,%rsi
mov    %ecx,%edx
call   *%rax
mov    %eax,%ebx
test   %eax,%eax
jne    116d <nodelay_connect+0x5d>
mov    0x0(%rbp),%rax
mov    0x2ea5(%rip),%rcx
cmp    (%rcx),%rax
jne    116d <nodelay_connect+0x5d>
movl   $0x1,0xc(%rsp)
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @nodelay_connect(i32 (i32, %struct.sockaddr*, i32)* %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca i32 (i32, %struct.sockaddr*, i32)*
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 (i32, %struct.sockaddr*, i32)* %0, i32 (i32, %struct.sockaddr*, i32)** %5
store i32 %1, i32* %6
store %struct.sockaddr* %2, %struct.sockaddr** %7
store i32 %3, i32* %8
%11 = load i32 (i32, %struct.sockaddr*, i32)*, i32 (i32, %struct.sockaddr*, i32)** %5
%12 = load i32, i32* %6
%13 = load %struct.sockaddr*, %struct.sockaddr** %7
%14 = load i32, i32* %8
%15 = call i32 %11(i32 %12, %struct.sockaddr* %13, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %4
%19 = load %struct.sockaddr*, %struct.sockaddr** %7
%20 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @AF_INET
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %18
store i32 1, i32* %10
%25 = load i32, i32* %6
%26 = load i32, i32* @IPPROTO_TCP
%27 = load i32, i32* @TCP_NODELAY
%28 = call i32 @setsockopt(i32 %25, i32 %26, i32 %27, i32* %10, i32 4)
br label %29
29: ; preds = %24, %18, %4
%30 = load i32, i32* %9
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @nodelay_connect(i32 (i32, %struct.sockaddr*, i32)* nocapture %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca i32
%6 = tail call i32 %0(i32 %1, %struct.sockaddr* %2, i32 %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %4
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @AF_INET
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%15 = load i32, i32* @IPPROTO_TCP
%16 = load i32, i32* @TCP_NODELAY
%17 = call i32 @setsockopt(i32 %1, i32 %15, i32 %16, i32* %5, i32 4)
br label %18
18: ; preds = %13, %8, %4
ret i32 %6
}