#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 */
typedef int socklen_t;
typedef int /*<<< orphan*/ err;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ assert(int);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int /*<<< orphan*/ perror(char *);
int socket_connected(int s) {
  assert(s != -1);
  int err;
  socklen_t errlen = sizeof(err);
  if (getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &errlen)) {
    perror("getsockopt");
    return 0;
  }
  return err == 0;
}


x86 assembly (clang, -O0):
<socket_connected>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1030 <getsockopt@plt>
cmp    $0x0,%rax
je     1197 <socket_connected+0x67>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <socket_connected+0x76>
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connected>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
xor    %edi,%edi
cmp    $0xffffffff,%ebx
setne  %dil
call   1040 <assert@plt>
movl   $0x4,0x8(%rsp)
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %ebx,%edi
call   1030 <getsockopt@plt>
test   %rax,%rax
jne    1185 <socket_connected+0x55>
xor    %eax,%eax
cmpl   $0x0,0xc(%rsp)
sete   %al
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    117f <socket_connected+0x4f>

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@.str = [11 x i8] c"getsockopt\00"
define i32 @socket_connected(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, -1
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
store i32 4, i32* %5
%10 = load i32, i32* %3
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_ERROR
%13 = call i64 @getsockopt(i32 %10, i32 %11, i32 %12, i32* %4, i32* %5)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %21
17: ; preds = %1
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
store i32 %20, i32* %2
br label %21
21: ; preds = %17, %15
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@.str = [11 x i8] c"getsockopt\00"
define i32 @socket_connected(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = icmp ne i32 %0, -1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_ERROR
%11 = call i64 @getsockopt(i32 %0, i32 %9, i32 %10, i32* %2, i32* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %19
15: ; preds = %1
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %15, %13
%20 = phi i32 [ 0, %13 ], [ %18, %15 ]
ret i32 %20
}