#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 qsem_enter_nowait(int, int);
int /*<<< orphan*/ qsem_leave(int, int);
int /*<<< orphan*/ usleep(int);
bool qsem_enter_force(int semid, int semno, int maxwaitms, bool *forceflag) {
  int wait;
  for (wait = 0; wait < maxwaitms; wait += 10) {
    if (qsem_enter_nowait(semid, semno) == true) {
      if (forceflag != NULL)
        *forceflag = false;
      return true;
    }
    usleep(10 * 1000);
  }
  (void)(0);
  while (true) {
    qsem_leave(semid, semno);
    if (qsem_enter_nowait(semid, semno) == true)
      break;
  }
  if (forceflag != NULL)
    *forceflag = true;
  return true;
}


x86 assembly (clang, -O0):
<qsem_enter_force>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11a5 <qsem_enter_force+0x75>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <qsem_enter_nowait@plt>
cmp    $0x1,%eax
jne    118d <qsem_enter_force+0x5d>
cmpq   $0x0,-0x18(%rbp)
je     1181 <qsem_enter_force+0x51>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11ef <qsem_enter_force+0xbf>
mov    $0x2710,%edi
call   1050 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <qsem_enter_force+0x1c>
jmp    11aa <qsem_enter_force+0x7a>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <qsem_leave@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <qsem_enter_nowait@plt>
cmp    $0x1,%eax
jne    11ce <qsem_enter_force+0x9e>
jmp    11d3 <qsem_enter_force+0xa3>
jmp    11aa <qsem_enter_force+0x7a>
cmpq   $0x0,-0x18(%rbp)
je     11e8 <qsem_enter_force+0xb8>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qsem_enter_force>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %esi,%r12d
mov    %edi,%ebx
test   %edx,%edx
jle    1180 <qsem_enter_force+0x50>
mov    %edx,%r15d
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r12d,%esi
call   1040 <qsem_enter_nowait@plt>
cmp    $0x1,%eax
je     11b4 <qsem_enter_force+0x84>
mov    $0x2710,%edi
call   1050 <usleep@plt>
add    $0xa,%ebp
cmp    %r15d,%ebp
jl     1150 <qsem_enter_force+0x20>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r12d,%esi
call   1030 <qsem_leave@plt>
mov    %ebx,%edi
mov    %r12d,%esi
call   1040 <qsem_enter_nowait@plt>
cmp    $0x1,%eax
jne    1180 <qsem_enter_force+0x50>
mov    $0x1,%eax
test   %r14,%r14
je     11a6 <qsem_enter_force+0x76>
mov    %eax,(%r14)
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
test   %r14,%r14
jne    11a3 <qsem_enter_force+0x73>
jmp    11a6 <qsem_enter_force+0x76>

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

LLVM IR (-O2):
define i32 @qsem_enter_force(i32 %0, i32 %1, i32 %2, i32* writeonly %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %16
6: ; preds = %4, %12
%7 = phi i32 [ %14, %12 ], [ 0, %4 ]
%8 = tail call i32 @qsem_enter_nowait(i32 %0, i32 %1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = icmp eq i32* %3, null
br i1 %11, label %25, label %23
12: ; preds = %6
%13 = tail call i32 @usleep(i32 10000)
%14 = add nuw nsw i32 %7, 10
%15 = icmp slt i32 %14, %2
br i1 %15, label %6, label %16
16: ; preds = %12, %4
br label %17
17: ; preds = %16, %17
%18 = tail call i32 @qsem_leave(i32 %0, i32 %1)
%19 = tail call i32 @qsem_enter_nowait(i32 %0, i32 %1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %17
21: ; preds = %17
%22 = icmp eq i32* %3, null
br i1 %22, label %25, label %23
23: ; preds = %21, %10
%24 = phi i32 [ 0, %10 ], [ 1, %21 ]
store i32 %24, i32* %3
br label %25
25: ; preds = %23, %21, %10
ret i32 1
}