#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 getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int 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) ==
      -1) {
    perror("getsockopt");
    return 0;
  }
  return ((struct sctp_assoc_ids *)buf)->gaids_assoc_id[0];
}


x86 assembly (clang, -O0):
<get_asoc_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x8,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rcx
lea    -0xc(%rbp),%r8
call   1030 <getsockopt@plt>
cmp    $0xffffffff,%eax
jne    1191 <get_asoc_id+0x61>
lea    0xe80(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    119d <get_asoc_id+0x6d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<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    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rcx
call   1030 <getsockopt@plt>
cmp    $0xffffffff,%eax
je     117c <get_asoc_id+0x4c>
mov    (%rbx),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7d(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    1175 <get_asoc_id+0x45>

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 @get_asoc_id(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %3
store i32 8, i32* %4
%6 = load i32, i32* %4
%7 = call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %5
%9 = load i32, i32* %3
%10 = load i32, i32* @IPPROTO_SCTP
%11 = load i32, i32* @SCTP_GET_ASSOC_ID_LIST
%12 = load i8*, i8** %5
%13 = call i32 @getsockopt(i32 %9, i32 %10, i32 %11, i8* %12, i32* %4)
%14 = icmp eq i32 %13, -1
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 %24
17: ; preds = %1
%18 = load i8*, i8** %5
%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
store i32 %23, i32* %2
br label %24
24: ; preds = %17, %15
%25 = load i32, i32* %2
ret i32 %25
}

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 @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 i32 @getsockopt(i32 %0, i32 %6, i32 %7, i8* %5, i32* %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %17
12: ; preds = %1
%13 = inttoptr i64 %4 to %struct.sctp_assoc_ids*
%14 = getelementptr inbounds %struct.sctp_assoc_ids, %struct.sctp_assoc_ids* %13, i64 0, i32 0
%15 = load i32*, i32** %14
%16 = load i32, i32* %15
br label %17
17: ; preds = %12, %10
%18 = phi i32 [ 0, %10 ], [ %16, %12 ]
ret i32 %18
}