#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
char *getenv(char *);
int snprintf(char *, int, char *, char *);
int /*<<< orphan*/ *stderr;
int write_display_ref(char *refid) {
  FILE *file;
  char *display;
  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;
  }
  display = getenv("DISPLAY");
  if (display == NULL) {
    fprintf(stderr, "error: unable to get DISPLAY value\n");
    return 10;
  }
  file = fopen(ref_file, "w");
  if (file == NULL) {
    fprintf(stderr, "error: unable to create file %s\n", ref_file);
    return 30;
  }
  fprintf(file, "%s\n", display);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<write_display_ref>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe8a(%rip),%rdx
call   1050 <snprintf@plt>
cmp    $0x100,%eax
jl     11aa <write_display_ref+0x5a>
mov    0x2e53(%rip),%rax
mov    (%rax),%rdi
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x14,-0x4(%rbp)
jmp    125c <write_display_ref+0x10c>
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e9 <write_display_ref+0x99>
mov    0x2e14(%rip),%rax
mov    (%rax),%rdi
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    125c <write_display_ref+0x10c>
lea    -0x120(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1236 <write_display_ref+0xe6>
mov    0x2dce(%rip),%rax
mov    (%rax),%rdi
lea    -0x120(%rbp),%rdx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1e,-0x4(%rbp)
jmp    125c <write_display_ref+0x10c>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_display_ref>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rcx
lea    0xe8b(%rip),%rdx
mov    %rsp,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
cmp    $0x100,%eax
jl     11b3 <write_display_ref+0x53>
mov    0x2e50(%rip),%rax
mov    (%rax),%rcx
lea    0xe7e(%rip),%rdi
mov    $0x26,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    $0x14,%ebp
jmp    1245 <write_display_ref+0xe5>
lea    0xe85(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11fe <write_display_ref+0x9e>
mov    %rax,%rbx
lea    0xe9d(%rip),%rsi
mov    %rsp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1225 <write_display_ref+0xc5>
mov    %rax,%r14
lea    0xea5(%rip),%rsi
xor    %ebp,%ebp
mov    %rax,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    1245 <write_display_ref+0xe5>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rcx
lea    0xe38(%rip),%rdi
mov    $0x23,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    $0xa,%ebp
jmp    1245 <write_display_ref+0xe5>
mov    0x2db4(%rip),%rax
mov    (%rax),%rdi
lea    0xe37(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1e,%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/tmp/slurm-spank-x11.%s\00"
@stderr = global i32* null
@.str.1 = [39 x i8] c"error: unable to build file reference\0A\00"
@.str.2 = [8 x i8] c"DISPLAY\00"
@.str.3 = [36 x i8] c"error: unable to get DISPLAY value\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [33 x i8] c"error: unable to create file %s\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @write_display_ref(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [256 x i8]
store i8* %0, i8** %3
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = icmp sge i32 %9, 256
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32*, i32** @stderr
%13 = call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 20, i32* %2
br label %36
14: ; preds = %1
%15 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32*, i32** @stderr
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 10, i32* %2
br label %36
21: ; preds = %14
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %23, i32** %4
%24 = load i32*, i32** %4
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i32*, i32** @stderr
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %28)
store i32 30, i32* %2
br label %36
30: ; preds = %21
%31 = load i32*, i32** %4
%32 = load i8*, i8** %5
%33 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %32)
%34 = load i32*, i32** %4
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %2
br label %36
36: ; preds = %30, %26, %18, %11
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [24 x i8] c"/tmp/slurm-spank-x11.%s\00"
@stderr = global i32* null
@.str.1 = [39 x i8] c"error: unable to build file reference\0A\00"
@.str.2 = [8 x i8] c"DISPLAY\00"
@.str.3 = [36 x i8] c"error: unable to get DISPLAY value\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [33 x i8] c"error: unable to create file %s\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @write_display_ref(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = icmp sgt i32 %4, 255
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32*, i32** @stderr
%8 = call i64 @fwrite(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 38, i64 1, i32* %7)
br label %24
9: ; preds = %1
%10 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32*, i32** @stderr
%14 = call i64 @fwrite(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i64 35, i64 1, i32* %13)
br label %24
15: ; preds = %9
%16 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32*, i32** @stderr
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %3)
br label %24
21: ; preds = %15
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %10)
%23 = call i32 @fclose(i32* %16)
br label %24
24: ; preds = %21, %18, %12, %6
%25 = phi i32 [ 20, %6 ], [ 10, %12 ], [ 30, %18 ], [ 0, %21 ]
ret i32 %25
}