#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 */

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ ECONNREFUSED;
int /*<<< orphan*/ assert(int);
int close(int);
scalar_t__ errno;
int /*<<< orphan*/ *getenv(char *);
scalar_t__ simple_socket_close(int);
int simple_socket_connect(char const *, int);
int main() {
  int sock;
  const char *ipv4_addr = "127.0.0.1";
  const char *ipv6_addr = "::1";
  int good_port = 22;
  int bad_port = 12345;
  sock = simple_socket_connect(ipv4_addr, good_port);
  assert(sock >= 0);
  assert(simple_socket_close(sock) == 0);
  sock = simple_socket_connect(ipv4_addr, bad_port);
  assert(sock == -3);
  assert(errno == ECONNREFUSED);
  assert(close(sock) == -1);
  assert(errno == EBADF);
  if (getenv("SKIP_IPV6_TESTS") == NULL) {
    sock = simple_socket_connect(ipv6_addr, good_port);
    assert(sock >= 0);
    assert(simple_socket_close(sock) == 0);
    sock = simple_socket_connect(ipv6_addr, bad_port);
    assert(sock == -3);
    assert(errno == ECONNREFUSED);
    assert(close(sock) == -1);
    assert(errno == EBADF);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe99(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x16,-0x1c(%rbp)
movl   $0x3039,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <simple_socket_connect@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%edi
call   1060 <simple_socket_close@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <simple_socket_connect@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xfffffffd,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%rax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xdd1(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
jne    12ff <main+0x1af>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <simple_socket_connect@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%edi
call   1060 <simple_socket_close@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <simple_socket_connect@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xfffffffd,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%rax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%rax
mov    0x2ce1(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xea1(%rip),%rbx
mov    %rbx,%rdi
mov    $0x16,%esi
call   1040 <simple_socket_connect@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1070 <assert@plt>
mov    %ebp,%edi
call   1060 <simple_socket_close@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
mov    $0x3039,%esi
call   1040 <simple_socket_connect@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xfffffffd,%eax
sete   %dil
call   1070 <assert@plt>
mov    0x2e25(%rip),%r14
mov    (%r14),%rax
mov    0x2e2b(%rip),%r15
xor    %edi,%edi
cmp    (%r15),%rax
sete   %dil
call   1070 <assert@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   1070 <assert@plt>
mov    (%r14),%rax
mov    0x2de6(%rip),%r12
xor    %edi,%edi
cmp    (%r12),%rax
sete   %dil
call   1070 <assert@plt>
lea    0xe0e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    129a <main+0x14a>
lea    0xdf5(%rip),%rbx
mov    %rbx,%rdi
mov    $0x16,%esi
call   1040 <simple_socket_connect@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1070 <assert@plt>
mov    %ebp,%edi
call   1060 <simple_socket_close@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
mov    $0x3039,%esi
call   1040 <simple_socket_connect@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xfffffffd,%eax
sete   %dil
call   1070 <assert@plt>
mov    (%r14),%rax
xor    %edi,%edi
cmp    (%r15),%rax
sete   %dil
call   1070 <assert@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   1070 <assert@plt>
mov    (%r14),%rax
xor    %edi,%edi
cmp    (%r12),%rax
sete   %dil
call   1070 <assert@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [4 x i8] c"::1\00"
@errno = global i64 0
@ECONNREFUSED = global i64 0
@EBADF = global i64 0
@.str.2 = [16 x i8] c"SKIP_IPV6_TESTS\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %3
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %4
store i32 22, i32* %5
store i32 12345, i32* %6
%7 = load i8*, i8** %3
%8 = load i32, i32* %5
%9 = call i32 @simple_socket_connect(i8* %7, i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp sge i32 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %2
%15 = call i64 @simple_socket_close(i32 %14)
%16 = icmp eq i64 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %3
%20 = load i32, i32* %6
%21 = call i32 @simple_socket_connect(i8* %19, i32 %20)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, -3
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i64, i64* @errno
%27 = load i64, i64* @ECONNREFUSED
%28 = icmp eq i64 %26, %27
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
%33 = icmp eq i32 %32, -1
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i64, i64* @errno
%37 = load i64, i64* @EBADF
%38 = icmp eq i64 %36, %37
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = call i32* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %78
43: ; preds = %0
%44 = load i8*, i8** %4
%45 = load i32, i32* %5
%46 = call i32 @simple_socket_connect(i8* %44, i32 %45)
store i32 %46, i32* %2
%47 = load i32, i32* %2
%48 = icmp sge i32 %47, 0
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = load i32, i32* %2
%52 = call i64 @simple_socket_close(i32 %51)
%53 = icmp eq i64 %52, 0
%54 = zext i1 %53 to i32
%55 = call i32 @assert(i32 %54)
%56 = load i8*, i8** %4
%57 = load i32, i32* %6
%58 = call i32 @simple_socket_connect(i8* %56, i32 %57)
store i32 %58, i32* %2
%59 = load i32, i32* %2
%60 = icmp eq i32 %59, -3
%61 = zext i1 %60 to i32
%62 = call i32 @assert(i32 %61)
%63 = load i64, i64* @errno
%64 = load i64, i64* @ECONNREFUSED
%65 = icmp eq i64 %63, %64
%66 = zext i1 %65 to i32
%67 = call i32 @assert(i32 %66)
%68 = load i32, i32* %2
%69 = call i32 @close(i32 %68)
%70 = icmp eq i32 %69, -1
%71 = zext i1 %70 to i32
%72 = call i32 @assert(i32 %71)
%73 = load i64, i64* @errno
%74 = load i64, i64* @EBADF
%75 = icmp eq i64 %73, %74
%76 = zext i1 %75 to i32
%77 = call i32 @assert(i32 %76)
br label %78
78: ; preds = %43, %0
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [4 x i8] c"::1\00"
@errno = global i64 0
@ECONNREFUSED = global i64 0
@EBADF = global i64 0
@.str.2 = [16 x i8] c"SKIP_IPV6_TESTS\00"
define i32 @main() {
%1 = tail call i32 @simple_socket_connect(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 22)
%2 = xor i32 %1, -1
%3 = lshr i32 %2, 31
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i64 @simple_socket_close(i32 %1)
%6 = icmp eq i64 %5, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @simple_socket_connect(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 12345)
%10 = icmp eq i32 %9, -3
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = load i64, i64* @errno
%14 = load i64, i64* @ECONNREFUSED
%15 = icmp eq i64 %13, %14
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = tail call i32 @close(i32 %9)
%19 = icmp eq i32 %18, -1
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
%22 = load i64, i64* @errno
%23 = load i64, i64* @EBADF
%24 = icmp eq i64 %22, %23
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
%27 = tail call i32* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %56
29: ; preds = %0
%30 = tail call i32 @simple_socket_connect(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 22)
%31 = xor i32 %30, -1
%32 = lshr i32 %31, 31
%33 = tail call i32 @assert(i32 %32)
%34 = tail call i64 @simple_socket_close(i32 %30)
%35 = icmp eq i64 %34, 0
%36 = zext i1 %35 to i32
%37 = tail call i32 @assert(i32 %36)
%38 = tail call i32 @simple_socket_connect(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 12345)
%39 = icmp eq i32 %38, -3
%40 = zext i1 %39 to i32
%41 = tail call i32 @assert(i32 %40)
%42 = load i64, i64* @errno
%43 = load i64, i64* @ECONNREFUSED
%44 = icmp eq i64 %42, %43
%45 = zext i1 %44 to i32
%46 = tail call i32 @assert(i32 %45)
%47 = tail call i32 @close(i32 %38)
%48 = icmp eq i32 %47, -1
%49 = zext i1 %48 to i32
%50 = tail call i32 @assert(i32 %49)
%51 = load i64, i64* @errno
%52 = load i64, i64* @EBADF
%53 = icmp eq i64 %51, %52
%54 = zext i1 %53 to i32
%55 = tail call i32 @assert(i32 %54)
br label %56
56: ; preds = %29, %0
ret i32 0
}