#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_SCTP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void *create_associations(void *arg) {
  struct sockaddr_in remote_addr;
  int fd;
  remote_addr.sin_family = AF_INET;
  remote_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  remote_addr.sin_port = htons(5001);
  while (1) {
    if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) < 0) {
      perror("socket");
    }
    if (connect(fd, (struct sockaddr *)&remote_addr,
                sizeof(struct sockaddr_in)) < 0) {
      perror("connect");
    }
    if (close(fd) < 0) {
      perror("close");
    }
  }
}


x86 assembly (clang, -O0):
<create_associations>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xe81(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1389,%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11cc <create_associations+0x6c>
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    11f3 <create_associations+0x93>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1211 <create_associations+0xb1>
lea    0xe0d(%rip),%rdi
call   1060 <perror@plt>
jmp    1194 <create_associations+0x34>

x86 assembly (clang, -O2):
<create_associations>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e73(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
lea    0xe7c(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    $0x1389,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2e36(%rip),%r14
mov    0x2e27(%rip),%r15
lea    0x8(%rsp),%rbx
lea    0xe64(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%edi
mov    (%r14),%esi
mov    (%r15),%edx
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11e1 <create_associations+0x81>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
jns    1201 <create_associations+0xa1>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    11c0 <create_associations+0x60>
mov    %r12,%rdi
call   1060 <perror@plt>
jmp    11c0 <create_associations+0x60>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@IPPROTO_SCTP = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [6 x i8] c"close\00"
define i8* @create_associations(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %5, i32* %6
%7 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%9 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i32 0, i32 0
store i32 %7, i32* %9
%10 = call i32 @htons(i32 5001)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %10, i32* %11
br label %12
12: ; preds = %1, %33
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = load i32, i32* @IPPROTO_SCTP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = load i32, i32* %4
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i64 @connect(i32 %21, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i32, i32* %4
%29 = call i64 @close(i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
br label %12
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@IPPROTO_SCTP = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [6 x i8] c"close\00"
define noalias i8* @create_associations(i8* nocapture readnone %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %4, i32* %5
%6 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 5001)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %8, i32* %9
%10 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %11
11: ; preds = %29, %1
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_SCTP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = call i64 @connect(i32 %15, %struct.sockaddr* %10, i32 12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = call i64 @close(i32 %15)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
br label %11
}