#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getppid();
int /*<<< orphan*/ sleep(int);
int snprintf(char *, int, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int wait_display_ref(char *refid) {
  struct stat fstatbuf;
  char ref_file[256];
  if (snprintf(ref_file, 256, "/tmp/slurm-spank-x11.%s", refid) >= 256) {
    fprintf(stderr, "error: unable to build file reference\n");
    return 20;
  }
  while (stat(ref_file, &fstatbuf) == 0 && getppid() > 1) {
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<wait_display_ref>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe8a(%rip),%rdx
call   1030 <snprintf@plt>
cmp    $0x100,%eax
jl     11a7 <wait_display_ref+0x57>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x14,-0x4(%rbp)
jmp    120d <wait_display_ref+0xbd>
jmp    11ac <wait_display_ref+0x5c>
lea    -0x120(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x121(%rbp)
jne    11e4 <wait_display_ref+0x94>
mov    $0x0,%al
call   1060 <getppid@plt>
cmp    $0x1,%eax
setg   %al
mov    %al,-0x121(%rbp)
mov    -0x121(%rbp),%al
test   $0x1,%al
jne    11f7 <wait_display_ref+0xa7>
jmp    1206 <wait_display_ref+0xb6>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    11ac <wait_display_ref+0x5c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_display_ref>:
push   %r15
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    %rdi,%rcx
lea    0xe9a(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1030 <snprintf@plt>
cmp    $0xff,%eax
jle    1198 <wait_display_ref+0x48>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x14,%ebx
jmp    11e6 <wait_display_ref+0x96>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11e6 <wait_display_ref+0x96>
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <getppid@plt>
cmp    $0x2,%eax
jl     11e6 <wait_display_ref+0x96>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     11c0 <wait_display_ref+0x70>
mov    %ebx,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [24 x i8] c"/tmp/slurm-spank-x11.%s\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"error: unable to build file reference\0A\00"
define i32 @wait_display_ref(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca [256 x i8]
store i8* %0, i8** %3
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%7 = load i8*, i8** %3
%8 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = icmp sge i32 %8, 256
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 20, i32* %2
br label %26
13: ; preds = %1
br label %14
14: ; preds = %23, %13
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%16 = call i64 @stat(i8* %15, %struct.stat* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 (...) @getppid()
%20 = icmp sgt i32 %19, 1
br label %21
21: ; preds = %18, %14
%22 = phi i1 [ false, %14 ], [ %20, %18 ]
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = call i32 @sleep(i32 1)
br label %14
25: ; preds = %21
store i32 0, i32* %2
br label %26
26: ; preds = %25, %10
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [24 x i8] c"/tmp/slurm-spank-x11.%s\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"error: unable to build file reference\0A\00"
define i32 @wait_display_ref(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [256 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = icmp sgt i32 %6, 255
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i64 @stat(i8* %5, %struct.stat* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %21
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %21
14: ; preds = %8, %17
%15 = call i32 (...) @getppid()
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i32 @sleep(i32 1)
%19 = call i64 @stat(i8* %5, %struct.stat* %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %14, label %21
21: ; preds = %17, %14, %8, %11
%22 = phi i32 [ 20, %11 ], [ 0, %8 ], [ 0, %14 ], [ 0, %17 ]
ret i32 %22
}