#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 /*<<< orphan*/ uint32_t;
struct sctp_assoc_ids {
  int *gaids_assoc_id;
};
typedef int /*<<< orphan*/ sctp_assoc_t;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_SCTP;
int /*<<< orphan*/ SCTP_GET_ASSOC_ID_LIST;
int /*<<< orphan*/ exit(int);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *,
                      int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int sn_get_asoc_id(int fd) {
  int len = (1 * sizeof(sctp_assoc_t)) + sizeof(uint32_t);
  char *buf = (char *)malloc(len);
  if (getsockopt(fd, IPPROTO_SCTP, SCTP_GET_ASSOC_ID_LIST, (void *)buf, &len)) {
    perror("getsockopt");
    exit(-1);
  }
  return ((struct sctp_assoc_ids *)buf)->gaids_assoc_id[0];
}


x86 assembly (clang, -O0):
<sn_get_asoc_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x8,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rcx
lea    -0x8(%rbp),%r8
call   1030 <getsockopt@plt>
cmp    $0x0,%rax
je     11a0 <sn_get_asoc_id+0x60>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sn_get_asoc_id>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
movl   $0x8,0x4(%rsp)
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rcx
call   1030 <getsockopt@plt>
test   %rax,%rax
jne    118c <sn_get_asoc_id+0x4c>
mov    (%rbx),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sctp_assoc_ids = type { i32* }
@IPPROTO_SCTP = global i32 0
@SCTP_GET_ASSOC_ID_LIST = global i32 0
@.str = [11 x i8] c"getsockopt\00"
define i32 @sn_get_asoc_id(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %2
store i32 8, i32* %3
%5 = load i32, i32* %3
%6 = call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %4
%8 = load i32, i32* %2
%9 = load i32, i32* @IPPROTO_SCTP
%10 = load i32, i32* @SCTP_GET_ASSOC_ID_LIST
%11 = load i8*, i8** %4
%12 = call i64 @getsockopt(i32 %8, i32 %9, i32 %10, i8* %11, i32* %3)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %1
%18 = load i8*, i8** %4
%19 = bitcast i8* %18 to %struct.sctp_assoc_ids*
%20 = getelementptr inbounds %struct.sctp_assoc_ids, %struct.sctp_assoc_ids* %19, i32 0, i32 0
%21 = load i32*, i32** %20
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32, i32* %22
ret i32 %23
}

LLVM IR (-O2):
%struct.sctp_assoc_ids = type { i32* }
@IPPROTO_SCTP = global i32 0
@SCTP_GET_ASSOC_ID_LIST = global i32 0
@.str = [11 x i8] c"getsockopt\00"
define i32 @sn_get_asoc_id(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 8, i32* %2
%4 = tail call i64 @malloc(i32 8)
%5 = inttoptr i64 %4 to i8*
%6 = load i32, i32* @IPPROTO_SCTP
%7 = load i32, i32* @SCTP_GET_ASSOC_ID_LIST
%8 = call i64 @getsockopt(i32 %0, i32 %6, i32 %7, i8* %5, i32* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %1
%14 = inttoptr i64 %4 to %struct.sctp_assoc_ids*
%15 = getelementptr inbounds %struct.sctp_assoc_ids, %struct.sctp_assoc_ids* %14, i64 0, i32 0
%16 = load i32*, i32** %15
%17 = load i32, i32* %16
ret i32 %17
}