#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 BTIC1H_Work_GetTidCountNb(int);
int /*<<< orphan*/ thSleep(int);
void BTIC1H_Work_WaitTaskComplete(int tid) {
  static int iysf = 128;
  int i, iys, iyc;
  i = BTIC1H_Work_GetTidCountNb(tid);
  iys = i;
  iyc = (iys * iysf) >> 8;
  while (i > 0) {
    if (i > iyc) {
      thSleep(1);
      i = BTIC1H_Work_GetTidCountNb(tid);
      if (!i) {
        iysf += 32;
        if (iysf > 255)
          iysf = 255;
      }
      continue;
    }
    thSleep(0);
    i = BTIC1H_Work_GetTidCountNb(tid);
    if (i) {
      iysf--;
      if (iysf < 1)
        iysf = 1;
      iyc = (iys * iysf) >> 8;
    }
  }
}


x86 assembly (clang, -O0):
<BTIC1H_Work_WaitTaskComplete>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <BTIC1H_Work_GetTidCountNb@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
imul   0x2eea(%rip),%eax
sar    $0x8,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    120b <BTIC1H_Work_WaitTaskComplete+0xeb>
mov    -0x8(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    11b4 <BTIC1H_Work_WaitTaskComplete+0x94>
mov    $0x1,%edi
call   1030 <thSleep@plt>
mov    -0x4(%rbp),%edi
call   1040 <BTIC1H_Work_GetTidCountNb@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11af <BTIC1H_Work_WaitTaskComplete+0x8f>
mov    0x2ea9(%rip),%eax
add    $0x20,%eax
mov    %eax,0x2ea0(%rip)
cmpl   $0xff,0x2e96(%rip)
jle    11aa <BTIC1H_Work_WaitTaskComplete+0x8a>
movl   $0xff,0x2e86(%rip)
jmp    11af <BTIC1H_Work_WaitTaskComplete+0x8f>
jmp    114c <BTIC1H_Work_WaitTaskComplete+0x2c>
xor    %edi,%edi
call   1030 <thSleep@plt>
mov    -0x4(%rbp),%edi
call   1040 <BTIC1H_Work_GetTidCountNb@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1206 <BTIC1H_Work_WaitTaskComplete+0xe6>
mov    0x2e5a(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2e51(%rip)
cmpl   $0x1,0x2e4a(%rip)
jge    11f6 <BTIC1H_Work_WaitTaskComplete+0xd6>
movl   $0x1,0x2e3a(%rip)
mov    -0xc(%rbp),%eax
imul   0x2e30(%rip),%eax
sar    $0x8,%eax
mov    %eax,-0x10(%rbp)
jmp    114c <BTIC1H_Work_WaitTaskComplete+0x2c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BTIC1H_Work_WaitTaskComplete>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
call   1040 <BTIC1H_Work_GetTidCountNb@plt>
test   %eax,%eax
jle    11ba <BTIC1H_Work_WaitTaskComplete+0x9a>
mov    %eax,%r15d
mov    0x2ef1(%rip),%ebp
mov    $0x2,%r14d
imul   %r15d,%ebp
sar    $0x8,%ebp
nopl   0x0(%rax)
cmp    %ebp,%eax
jle    1170 <BTIC1H_Work_WaitTaskComplete+0x50>
mov    $0x1,%edi
call   1030 <thSleep@plt>
mov    %ebx,%edi
call   1040 <BTIC1H_Work_GetTidCountNb@plt>
test   %eax,%eax
je     119e <BTIC1H_Work_WaitTaskComplete+0x7e>
jg     1150 <BTIC1H_Work_WaitTaskComplete+0x30>
jmp    11ba <BTIC1H_Work_WaitTaskComplete+0x9a>
nopl   (%rax)
xor    %edi,%edi
call   1030 <thSleep@plt>
mov    %ebx,%edi
call   1040 <BTIC1H_Work_GetTidCountNb@plt>
test   %eax,%eax
je     11ba <BTIC1H_Work_WaitTaskComplete+0x9a>
mov    0x2ea8(%rip),%ebp
cmp    $0x3,%ebp
cmovl  %r14d,%ebp
add    $0xffffffff,%ebp
mov    %ebp,0x2e98(%rip)
test   %eax,%eax
jg     1145 <BTIC1H_Work_WaitTaskComplete+0x25>
jmp    11ba <BTIC1H_Work_WaitTaskComplete+0x9a>
mov    0x2e8c(%rip),%eax
cmp    $0xdf,%eax
mov    $0xdf,%ecx
cmovl  %eax,%ecx
add    $0x20,%ecx
mov    %ecx,0x2e76(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BTIC1H_Work_WaitTaskComplete.iysf = internal global i32 128
define void @BTIC1H_Work_WaitTaskComplete(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @BTIC1H_Work_GetTidCountNb(i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%11 = mul nsw i32 %9, %10
%12 = ashr i32 %11, 8
store i32 %12, i32* %5
br label %13
13: ; preds = %51, %33, %1
%14 = load i32, i32* %3
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %52
16: ; preds = %13
%17 = load i32, i32* %3
%18 = load i32, i32* %5
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %34
20: ; preds = %16
%21 = call i32 @thSleep(i32 1)
%22 = load i32, i32* %2
%23 = call i32 @BTIC1H_Work_GetTidCountNb(i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp ne i32 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %20
%27 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%28 = add nsw i32 %27, 32
store i32 %28, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%29 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%30 = icmp sgt i32 %29, 255
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 255, i32* @BTIC1H_Work_WaitTaskComplete.iysf
br label %32
32: ; preds = %31, %26
br label %33
33: ; preds = %32, %20
br label %13
34: ; preds = %16
%35 = call i32 @thSleep(i32 0)
%36 = load i32, i32* %2
%37 = call i32 @BTIC1H_Work_GetTidCountNb(i32 %36)
store i32 %37, i32* %3
%38 = load i32, i32* %3
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %51
40: ; preds = %34
%41 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%42 = add nsw i32 %41, -1
store i32 %42, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%43 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%44 = icmp slt i32 %43, 1
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 1, i32* @BTIC1H_Work_WaitTaskComplete.iysf
br label %46
46: ; preds = %45, %40
%47 = load i32, i32* %4
%48 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%49 = mul nsw i32 %47, %48
%50 = ashr i32 %49, 8
store i32 %50, i32* %5
br label %51
51: ; preds = %46, %34
br label %13
52: ; preds = %13
ret void
}

LLVM IR (-O2):
@BTIC1H_Work_WaitTaskComplete.iysf = internal unnamed_addr global i32 128
define void @BTIC1H_Work_WaitTaskComplete(i32 %0) {
%2 = tail call i32 @BTIC1H_Work_GetTidCountNb(i32 %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %35
4: ; preds = %1
%5 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
br label %6
6: ; preds = %4, %29
%7 = phi i32 [ %33, %29 ], [ %5, %4 ]
%8 = phi i32 [ %27, %29 ], [ %2, %4 ]
%9 = mul nsw i32 %7, %2
%10 = ashr i32 %9, 8
br label %11
11: ; preds = %6, %23
%12 = phi i32 [ %8, %6 ], [ %16, %23 ]
%13 = icmp sgt i32 %12, %10
br i1 %13, label %14, label %25
14: ; preds = %11
%15 = tail call i32 @thSleep(i32 1)
%16 = tail call i32 @BTIC1H_Work_GetTidCountNb(i32 %0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%20 = icmp slt i32 %19, 223
%21 = select i1 %20, i32 %19, i32 223
%22 = add nsw i32 %21, 32
store i32 %22, i32* @BTIC1H_Work_WaitTaskComplete.iysf
br label %35
23: ; preds = %14
%24 = icmp sgt i32 %16, 0
br i1 %24, label %11, label %35
25: ; preds = %11
%26 = tail call i32 @thSleep(i32 0)
%27 = tail call i32 @BTIC1H_Work_GetTidCountNb(i32 %0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %25
%30 = load i32, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%31 = icmp sgt i32 %30, 2
%32 = select i1 %31, i32 %30, i32 2
%33 = add nsw i32 %32, -1
store i32 %33, i32* @BTIC1H_Work_WaitTaskComplete.iysf
%34 = icmp sgt i32 %27, 0
br i1 %34, label %6, label %35
35: ; preds = %25, %29, %23, %18, %1
ret void
}