#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*/ pid_t;
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
int __sched_getaffinity(int /*<<< orphan*/, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, size_t);
int sched_getaffinity(pid_t pid, size_t setsize, cpu_set_t *set) {
  int ret = __sched_getaffinity(pid, setsize, set);
  if (ret >= 0) {
    if ((size_t)ret < setsize) {
      memset((char *)set + ret, '\0', setsize - (size_t)ret);
    }
    ret = 0;
  }
  return ret;
}


x86 assembly (clang, -O0):
<sched_getaffinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <__sched_getaffinity@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1182 <sched_getaffinity+0x62>
movslq -0x1c(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    117b <sched_getaffinity+0x5b>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
movslq -0x1c(%rbp),%rax
sub    %rax,%rdx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sched_getaffinity>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
call   1030 <__sched_getaffinity@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1153 <sched_getaffinity+0x33>
mov    %ebp,%eax
xor    %ebp,%ebp
cmp    %rbx,%rax
jae    1153 <sched_getaffinity+0x33>
add    %rax,%r14
sub    %rax,%rbx
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @sched_getaffinity(i32 %0, i64 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%8 = load i32, i32* %4
%9 = load i64, i64* %5
%10 = load i32*, i32** %6
%11 = call i32 @__sched_getaffinity(i32 %8, i64 %9, i32* %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %3
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = load i64, i64* %5
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %30
19: ; preds = %14
%20 = load i32*, i32** %6
%21 = bitcast i32* %20 to i8*
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i64, i64* %5
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = sub i64 %25, %27
%29 = call i32 @memset(i8* %24, i8 signext 0, i64 %28)
br label %30
30: ; preds = %19, %14
store i32 0, i32* %7
br label %31
31: ; preds = %30, %3
%32 = load i32, i32* %7
ret i32 %32
}

LLVM IR (-O2):
define i32 @sched_getaffinity(i32 %0, i64 %1, i32* %2) {
%4 = tail call i32 @__sched_getaffinity(i32 %0, i64 %1, i32* %2)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = zext i32 %4 to i64
%8 = icmp ult i64 %7, %1
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = bitcast i32* %2 to i8*
%11 = getelementptr inbounds i8, i8* %10, i64 %7
%12 = sub i64 %1, %7
%13 = tail call i32 @memset(i8* %11, i8 signext 0, i64 %12)
br label %14
14: ; preds = %6, %9, %3
%15 = phi i32 [ %4, %3 ], [ 0, %9 ], [ 0, %6 ]
ret i32 %15
}