#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 scalar_t__ uint16_t;

/* Variables and functions */
scalar_t__ *calloc(size_t, int);
int /*<<< orphan*/ free(scalar_t__ *);
scalar_t__ tls1_ec_nid2curve_id(int const);
int tls1_set_groups(uint16_t **out_group_ids, size_t *out_group_ids_len,
                    const int *groups, size_t ngroups) {
  uint16_t *group_ids;
  size_t i;
  group_ids = calloc(ngroups, sizeof(uint16_t));
  if (group_ids == NULL)
    return 0;
  for (i = 0; i < ngroups; i++) {
    group_ids[i] = tls1_ec_nid2curve_id(groups[i]);
    if (group_ids[i] == 0) {
      free(group_ids);
      return 0;
    }
  }
  free(*out_group_ids);
  *out_group_ids = group_ids;
  *out_group_ids_len = ngroups;
  return 1;
}


x86 assembly (clang, -O0):
<tls1_set_groups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1171 <tls1_set_groups+0x41>
movl   $0x0,-0x4(%rbp)
jmp    120d <tls1_set_groups+0xdd>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11e4 <tls1_set_groups+0xb4>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1050 <tls1_ec_nid2curve_id@plt>
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11ce <tls1_set_groups+0x9e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    120d <tls1_set_groups+0xdd>
jmp    11d3 <tls1_set_groups+0xa3>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1179 <tls1_set_groups+0x49>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tls1_set_groups>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rcx,%rdi
mov    $0x8,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     11a8 <tls1_set_groups+0x78>
mov    %rax,%rbx
test   %r13,%r13
je     118b <tls1_set_groups+0x5b>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12,%rbp,4),%edi
call   1050 <tls1_ec_nid2curve_id@plt>
mov    %rax,(%rbx,%rbp,8)
test   %rax,%rax
je     11a0 <tls1_set_groups+0x70>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1170 <tls1_set_groups+0x40>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %rbx,(%r15)
mov    %r13,(%r14)
mov    $0x1,%eax
jmp    11aa <tls1_set_groups+0x7a>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @tls1_set_groups(i64** %0, i64* %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i64**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64*
%11 = alloca i64
store i64** %0, i64*** %6
store i64* %1, i64** %7
store i32* %2, i32** %8
store i64 %3, i64* %9
%12 = load i64, i64* %9
%13 = call i64* @calloc(i64 %12, i32 8)
store i64* %13, i64** %10
%14 = load i64*, i64** %10
%15 = icmp eq i64* %14, null
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 0, i32* %5
br label %51
17: ; preds = %4
store i64 0, i64* %11
br label %18
18: ; preds = %40, %17
%19 = load i64, i64* %11
%20 = load i64, i64* %9
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %43
22: ; preds = %18
%23 = load i32*, i32** %8
%24 = load i64, i64* %11
%25 = getelementptr inbounds i32, i32* %23, i64 %24
%26 = load i32, i32* %25
%27 = call i64 @tls1_ec_nid2curve_id(i32 %26)
%28 = load i64*, i64** %10
%29 = load i64, i64* %11
%30 = getelementptr inbounds i64, i64* %28, i64 %29
store i64 %27, i64* %30
%31 = load i64*, i64** %10
%32 = load i64, i64* %11
%33 = getelementptr inbounds i64, i64* %31, i64 %32
%34 = load i64, i64* %33
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %22
%37 = load i64*, i64** %10
%38 = call i32 @free(i64* %37)
store i32 0, i32* %5
br label %51
39: ; preds = %22
br label %40
40: ; preds = %39
%41 = load i64, i64* %11
%42 = add i64 %41, 1
store i64 %42, i64* %11
br label %18
43: ; preds = %18
%44 = load i64**, i64*** %6
%45 = load i64*, i64** %44
%46 = call i32 @free(i64* %45)
%47 = load i64*, i64** %10
%48 = load i64**, i64*** %6
store i64* %47, i64** %48
%49 = load i64, i64* %9
%50 = load i64*, i64** %7
store i64 %49, i64* %50
store i32 1, i32* %5
br label %51
51: ; preds = %43, %36, %16
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
define i32 @tls1_set_groups(i64** nocapture %0, i64* nocapture writeonly %1, i32* nocapture %2, i64 %3) {
%5 = tail call i64* @calloc(i64 %3, i32 8)
%6 = icmp eq i64* %5, null
br i1 %6, label %24, label %7
7: ; preds = %4
%8 = icmp eq i64 %3, 0
br i1 %8, label %21, label %11
9: ; preds = %11
%10 = icmp eq i64 %18, %3
br i1 %10, label %21, label %11
11: ; preds = %7, %9
%12 = phi i64 [ %18, %9 ], [ 0, %7 ]
%13 = getelementptr inbounds i32, i32* %2, i64 %12
%14 = load i32, i32* %13
%15 = tail call i64 @tls1_ec_nid2curve_id(i32 %14)
%16 = getelementptr inbounds i64, i64* %5, i64 %12
store i64 %15, i64* %16
%17 = icmp eq i64 %15, 0
%18 = add nuw i64 %12, 1
br i1 %17, label %19, label %9
19: ; preds = %11
%20 = tail call i32 @free(i64* %5)
br label %24
21: ; preds = %9, %7
%22 = load i64*, i64** %0
%23 = tail call i32 @free(i64* %22)
store i64* %5, i64** %0
store i64 %3, i64* %1
br label %24
24: ; preds = %4, %21, %19
%25 = phi i32 [ 0, %19 ], [ 1, %21 ], [ 0, %4 ]
ret i32 %25
}