#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 */
scalar_t__ FileExists(char *);
int FindJobs(char *, int, int *);
int FlockFile(char *);
int /*<<< orphan*/ FunlockFile(int, char *);
int /*<<< orphan*/ Message(char *, int, char *);
int /*<<< orphan*/ WriteJobFile(char *, int, char **, int);
int /*<<< orphan*/ sleep(int);
int QueueJob(char *queueDir, char *lockFullFile, char **progArgs, int nProgArgs,
             int maxWait, int *nJobsWaiting) {
  int jobID = 1, waitCount = 0, fh, nJobs;
  *nJobsWaiting = 0;
  while (FileExists(lockFullFile)) {
    if (++waitCount > maxWait) {
      Message("simq", 3, "Cannot submit job - lockfile is not clearing");
    }
    sleep(1);
  }
  if ((fh = FlockFile(lockFullFile)) == (-1)) {
    Message("simq", 3, "Cannot create lock file");
  }
  nJobs = FindJobs(queueDir, 1, &jobID);
  if (nJobs)
    jobID++;
  WriteJobFile(queueDir, jobID, progArgs, nProgArgs);
  FunlockFile(fh, lockFullFile);
  *nJobsWaiting = nJobs + 1;
  return (jobID);
}


x86 assembly (clang, -O0):
<QueueJob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1080 <FileExists@plt>
cmp    $0x0,%rax
je     11f3 <QueueJob+0x83>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
cmp    -0x20(%rbp),%eax
jle    11e4 <QueueJob+0x74>
lea    0xe2d(%rip),%rdi
mov    $0x3,%esi
lea    0xe26(%rip),%rdx
call   1050 <Message@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    11a7 <QueueJob+0x37>
mov    -0x10(%rbp),%rdi
call   1070 <FlockFile@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1220 <QueueJob+0xb0>
lea    0xdf1(%rip),%rdi
mov    $0x3,%esi
lea    0xe17(%rip),%rdx
call   1050 <Message@plt>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
lea    -0x2c(%rbp),%rdx
call   1040 <FindJobs@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1248 <QueueJob+0xd8>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <WriteJobFile@plt>
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <FunlockFile@plt>
mov    -0x38(%rbp),%ecx
add    $0x1,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<QueueJob>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %ecx,0x4(%rsp)
mov    %rdx,0x8(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r13
movl   $0x1,(%rsp)
mov    %r9,0x10(%rsp)
movl   $0x0,(%r9)
mov    %rsi,%rdi
call   1080 <FileExists@plt>
test   %rax,%rax
je     1202 <QueueJob+0x92>
xor    %r14d,%r14d
lea    0xe46(%rip),%r15
lea    0xe44(%rip),%r12
jmp    11eb <QueueJob+0x7b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1080 <FileExists@plt>
add    $0x1,%r14d
test   %rax,%rax
je     1202 <QueueJob+0x92>
cmp    %ebp,%r14d
jl     11d0 <QueueJob+0x60>
mov    %r15,%rdi
mov    $0x3,%esi
mov    %r12,%rdx
call   1050 <Message@plt>
jmp    11d0 <QueueJob+0x60>
mov    %rbx,%rdi
call   1070 <FlockFile@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    122a <QueueJob+0xba>
lea    0xde7(%rip),%rdi
lea    0xe12(%rip),%rdx
mov    $0x3,%esi
call   1050 <Message@plt>
mov    %rsp,%rdx
mov    %r13,%rdi
mov    $0x1,%esi
call   1040 <FindJobs@plt>
mov    %eax,%ebp
mov    (%rsp),%esi
test   %eax,%eax
je     1249 <QueueJob+0xd9>
add    $0x1,%esi
mov    %esi,(%rsp)
mov    %r13,%rdi
mov    0x8(%rsp),%rdx
mov    0x4(%rsp),%ecx
call   1030 <WriteJobFile@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <FunlockFile@plt>
add    $0x1,%ebp
mov    0x10(%rsp),%rax
mov    %ebp,(%rax)
mov    (%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"simq\00"
@.str.1 = [45 x i8] c"Cannot submit job - lockfile is not clearing\00"
@.str.2 = [24 x i8] c"Cannot create lock file\00"
define i32 @QueueJob(i8* %0, i8* %1, i8** %2, i32 %3, i32 %4, i32* %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8** %2, i8*** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32* %5, i32** %12
store i32 1, i32* %13
store i32 0, i32* %14
%17 = load i32*, i32** %12
store i32 0, i32* %17
br label %18
18: ; preds = %29, %6
%19 = load i8*, i8** %8
%20 = call i64 @FileExists(i8* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i32, i32* %14
%24 = add nsw i32 %23, 1
store i32 %24, i32* %14
%25 = load i32, i32* %11
%26 = icmp sgt i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @Message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 3, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = call i32 @sleep(i32 1)
br label %18
31: ; preds = %18
%32 = load i8*, i8** %8
%33 = call i32 @FlockFile(i8* %32)
store i32 %33, i32* %15
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @Message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 3, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = load i8*, i8** %7
%39 = call i32 @FindJobs(i8* %38, i32 1, i32* %13)
store i32 %39, i32* %16
%40 = load i32, i32* %16
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* %13
%44 = add nsw i32 %43, 1
store i32 %44, i32* %13
br label %45
45: ; preds = %42, %37
%46 = load i8*, i8** %7
%47 = load i32, i32* %13
%48 = load i8**, i8*** %9
%49 = load i32, i32* %10
%50 = call i32 @WriteJobFile(i8* %46, i32 %47, i8** %48, i32 %49)
%51 = load i32, i32* %15
%52 = load i8*, i8** %8
%53 = call i32 @FunlockFile(i32 %51, i8* %52)
%54 = load i32, i32* %16
%55 = add nsw i32 %54, 1
%56 = load i32*, i32** %12
store i32 %55, i32* %56
%57 = load i32, i32* %13
ret i32 %57
}

LLVM IR (-O2):
@.str = [5 x i8] c"simq\00"
@.str.1 = [45 x i8] c"Cannot submit job - lockfile is not clearing\00"
@.str.2 = [24 x i8] c"Cannot create lock file\00"
define i32 @QueueJob(i8* %0, i8* %1, i8** %2, i32 %3, i32 %4, i32* nocapture writeonly %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
store i32 1, i32* %7
store i32 0, i32* %5
%9 = tail call i64 @FileExists(i8* %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %21, label %11
11: ; preds = %6, %17
%12 = phi i32 [ %13, %17 ], [ 0, %6 ]
%13 = add nuw nsw i32 %12, 1
%14 = icmp slt i32 %12, %4
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @Message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 3, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i64 @FileExists(i8* %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %11
21: ; preds = %17, %6
%22 = tail call i32 @FlockFile(i8* %1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @Message(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 3, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = call i32 @FindJobs(i8* %0, i32 1, i32* %7)
%28 = icmp eq i32 %27, 0
%29 = load i32, i32* %7
br i1 %28, label %32, label %30
30: ; preds = %26
%31 = add nsw i32 %29, 1
store i32 %31, i32* %7
br label %32
32: ; preds = %26, %30
%33 = phi i32 [ %31, %30 ], [ %29, %26 ]
%34 = call i32 @WriteJobFile(i8* %0, i32 %33, i8** %2, i32 %3)
%35 = call i32 @FunlockFile(i32 %22, i8* %1)
%36 = add nsw i32 %27, 1
store i32 %36, i32* %5
%37 = load i32, i32* %7
ret i32 %37
}