#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 tcp_info {
  scalar_t__ tcpi_state;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ info;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
scalar_t__ TCP_ESTABLISHED;
int /*<<< orphan*/ TCP_INFO;
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct tcp_info *, int /*<<< orphan*/ *);
int SocketConnected(int sock) {
  if (sock <= 0)
    return 0;
  struct tcp_info info;
  int len = sizeof(info);
  getsockopt(sock, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *)&len);
  if ((info.tcpi_state == TCP_ESTABLISHED)) {
    return 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<SocketConnected>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     1131 <SocketConnected+0x21>
movl   $0x0,-0x4(%rbp)
jmp    1181 <SocketConnected+0x71>
movl   $0x8,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%r8
call   1030 <getsockopt@plt>
mov    -0x10(%rbp),%rax
mov    0x2e73(%rip),%rcx
cmp    (%rcx),%rax
jne    117a <SocketConnected+0x6a>
movl   $0x1,-0x4(%rbp)
jmp    1181 <SocketConnected+0x71>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SocketConnected>:
test   %edi,%edi
jle    115a <SocketConnected+0x4a>
sub    $0x18,%rsp
movl   $0x8,0xc(%rsp)
mov    0x2ec1(%rip),%rax
mov    (%rax),%esi
mov    0x2ea0(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
call   1030 <getsockopt@plt>
mov    0x10(%rsp),%rcx
mov    0x2e8b(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
sete   %al
add    $0x18,%rsp
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.tcp_info = type { i64 }
@IPPROTO_TCP = global i32 0
@TCP_INFO = global i32 0
@TCP_ESTABLISHED = global i64 0
define i32 @SocketConnected(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.tcp_info
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp sle i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %20
9: ; preds = %1
store i32 8, i32* %5
%10 = load i32, i32* %3
%11 = load i32, i32* @IPPROTO_TCP
%12 = load i32, i32* @TCP_INFO
%13 = call i32 @getsockopt(i32 %10, i32 %11, i32 %12, %struct.tcp_info* %4, i32* %5)
%14 = getelementptr inbounds %struct.tcp_info, %struct.tcp_info* %4, i32 0, i32 0
%15 = load i64, i64* %14
%16 = load i64, i64* @TCP_ESTABLISHED
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %9
store i32 1, i32* %2
br label %20
19: ; preds = %9
store i32 0, i32* %2
br label %20
20: ; preds = %19, %18, %8
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.tcp_info = type { i64 }
@IPPROTO_TCP = global i32 0
@TCP_INFO = global i32 0
@TCP_ESTABLISHED = global i64 0
define i32 @SocketConnected(i32 %0) {
%2 = alloca %struct.tcp_info
%3 = alloca i32
%4 = icmp slt i32 %0, 1
br i1 %4, label %16, label %5
5: ; preds = %1
%6 = bitcast %struct.tcp_info* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 8, i32* %3
%8 = load i32, i32* @IPPROTO_TCP
%9 = load i32, i32* @TCP_INFO
%10 = call i32 @getsockopt(i32 %0, i32 %8, i32 %9, %struct.tcp_info* %2, i32* %3)
%11 = getelementptr inbounds %struct.tcp_info, %struct.tcp_info* %2, i64 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @TCP_ESTABLISHED
%14 = icmp eq i64 %12, %13
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %1, %5
%17 = phi i32 [ %15, %5 ], [ 0, %1 ]
ret i32 %17
}