#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;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_SCTP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char const *, int /*<<< orphan*/,
                        int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int main() {
  int sd = socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP);
  if (sd < 0)
    perror("sctp socket error");
  struct sockaddr_in sa;
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = htonl(INADDR_ANY);
  sa.sin_port = htons(12345);
  int ret = bind(sd, (struct sockaddr *)&sa, sizeof(sa));
  if (ret < 0)
    perror("sctp bind error");
  listen(sd, 3);
  int client_sd = accept(sd, NULL, NULL);
  if (client_sd < 0)
    perror("sctp accept error");
  const char *response = "hi there";
  char buffer[80];
  int got = (int)recv(client_sd, buffer, sizeof(buffer), 0);
  if (got > 0) {
    buffer[got] = 0;
    printf("client said: %s\n", buffer);
  }
  send(client_sd, response, strlen(response), 0);
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    121b <main+0x4b>
lea    0xdea(%rip),%rdi
call   10d0 <perror@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    $0x3039,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    127f <main+0xaf>
lea    0xd98(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x8(%rbp),%edi
mov    $0x3,%esi
call   10b0 <listen@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10e0 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12b4 <main+0xe4>
lea    0xd73(%rip),%rdi
call   10d0 <perror@plt>
lea    0xd79(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x50,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jle    1301 <main+0x131>
movslq -0x84(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xd41(%rip),%rdi
call   1070 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x94(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x94(%rbp),%edi
mov    -0x90(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x8(%rbp),%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
js     12db <main+0x10b>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x3039,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %eax,%eax
js     12ec <main+0x11c>
mov    %r14d,%edi
mov    $0x3,%esi
call   10b0 <listen@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12fd <main+0x12d>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x50,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12ab <main+0xdb>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd9c(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <printf@plt>
lea    0xd82(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd1e(%rip),%rdi
call   10d0 <perror@plt>
jmp    1203 <main+0x33>
lea    0xd1f(%rip),%rdi
call   10d0 <perror@plt>
jmp    1259 <main+0x89>
lea    0xd1e(%rip),%rdi
call   10d0 <perror@plt>
jmp    127c <main+0xac>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_SCTP = global i32 0
@.str = [18 x i8] c"sctp socket error\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [16 x i8] c"sctp bind error\00"
@.str.2 = [18 x i8] c"sctp accept error\00"
@.str.3 = [9 x i8] c"hi there\00"
@.str.4 = [17 x i8] c"client said: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [80 x i8]
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = load i32, i32* @IPPROTO_SCTP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = call i32 @htons(i32 12345)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %17
%35 = load i32, i32* %2
%36 = call i32 @listen(i32 %35, i32 3)
%37 = load i32, i32* %2
%38 = call i32 @accept(i32 %37, i32* null, i32* null)
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %34
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8** %6
%44 = load i32, i32* %5
%45 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%46 = call i64 @recv(i32 %44, i8* %45, i32 80, i32 0)
%47 = trunc i64 %46 to i32
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %43
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 %52
store i8 0, i8* %53
%54 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%55 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %50, %43
%57 = load i32, i32* %5
%58 = load i8*, i8** %6
%59 = load i8*, i8** %6
%60 = call i32 @strlen(i8* %59)
%61 = call i32 @send(i32 %57, i8* %58, i32 %60, i32 0)
%62 = load i32, i32* %2
%63 = call i32 @close(i32 %62)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_SCTP = global i32 0
@.str = [18 x i8] c"sctp socket error\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [16 x i8] c"sctp bind error\00"
@.str.2 = [18 x i8] c"sctp accept error\00"
@.str.3 = [9 x i8] c"hi there\00"
@.str.4 = [17 x i8] c"client said: %s\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [80 x i8]
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_SCTP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = bitcast %struct.sockaddr_in* %1 to i8*
%12 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = call i32 @htons(i32 12345)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i32 @bind(i32 %6, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %10
%24 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %10
%26 = call i32 @listen(i32 %6, i32 3)
%27 = call i32 @accept(i32 %6, i32* null, i32* null)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%33 = call i64 @recv(i32 %27, i8* %32, i32 80, i32 0)
%34 = trunc i64 %33 to i32
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = shl i64 %33, 32
%38 = ashr exact i64 %37, 32
%39 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %38
store i8 0, i8* %39
%40 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %41
41: ; preds = %36, %31
%42 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @send(i32 %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %42, i32 0)
%44 = call i32 @close(i32 %6)
ret i32 0
}