#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 */

/* Variables and functions */
int wait_nohang(int *);
int wait_pids_nohang(int const *pids, unsigned int len, int *wstat) {
  int r;
  for (;;) {
    int w;
    if ((r = wait_nohang(&w)) > 0) {
      unsigned int i = 0;
      for (; i < len; i++)
        if (r == pids[i])
          break;
      if (i < len) {
        *wstat = w;
        return 1 + i;
      }
      continue;
    }
    break;
  }
  return r;
}


x86 assembly (clang, -O0):
<wait_pids_nohang>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1030 <wait_nohang@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    119e <wait_pids_nohang+0x8e>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1176 <wait_pids_nohang+0x66>
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    -0x2c(%rbp),%edx
cmp    (%rcx,%rdx,4),%eax
jne    1163 <wait_pids_nohang+0x53>
jmp    1176 <wait_pids_nohang+0x66>
jmp    1168 <wait_pids_nohang+0x58>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    113f <wait_pids_nohang+0x2f>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1199 <wait_pids_nohang+0x89>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11a9 <wait_pids_nohang+0x99>
jmp    1123 <wait_pids_nohang+0x13>
jmp    11a3 <wait_pids_nohang+0x93>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_pids_nohang>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbx
lea    0xc(%rsp),%rdi
call   1030 <wait_nohang@plt>
test   %eax,%eax
jle    117c <wait_pids_nohang+0x6c>
mov    %r12d,%ebp
lea    0xc(%rsp),%r15
jmp    114c <wait_pids_nohang+0x3c>
nopl   (%rax)
mov    %r15,%rdi
call   1030 <wait_nohang@plt>
test   %eax,%eax
jle    117c <wait_pids_nohang+0x6c>
test   %r12d,%r12d
je     1140 <wait_pids_nohang+0x30>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    (%rbx,%rcx,4),%eax
je     1170 <wait_pids_nohang+0x60>
add    $0x1,%rcx
cmp    %rcx,%rbp
jne    1160 <wait_pids_nohang+0x50>
jmp    1140 <wait_pids_nohang+0x30>
mov    0xc(%rsp),%eax
mov    %eax,(%r14)
add    $0x1,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @wait_pids_nohang(i32* nocapture %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @wait_nohang(i32* %4)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %28
8: ; preds = %3
%9 = icmp eq i32 %1, 0
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %8, %25
%12 = phi i32 [ %6, %8 ], [ %26, %25 ]
br i1 %9, label %25, label %13
13: ; preds = %11, %18
%14 = phi i64 [ %19, %18 ], [ 0, %11 ]
%15 = getelementptr inbounds i32, i32* %0, i64 %14
%16 = load i32, i32* %15
%17 = icmp eq i32 %12, %16
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = add nuw nsw i64 %14, 1
%20 = icmp eq i64 %19, %10
br i1 %20, label %25, label %13
21: ; preds = %13
%22 = trunc i64 %14 to i32
%23 = load i32, i32* %4
store i32 %23, i32* %2
%24 = add nuw i32 %22, 1
br label %30
25: ; preds = %18, %11
%26 = call i32 @wait_nohang(i32* %4)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %11, label %28
28: ; preds = %25, %3
%29 = phi i32 [ %6, %3 ], [ %26, %25 ]
br label %30
30: ; preds = %21, %28
%31 = phi i32 [ %29, %28 ], [ %24, %21 ]
ret i32 %31
}