#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 */
scalar_t__ _IO_fopen(char *, char *);
scalar_t__ __libc_malloc(int);
char *getenv(char *);
int getpid();
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
FILE *getFile() {
  static int open_pending = 0;
  static FILE *destination = NULL;
  char *logFile;
  char *tmp;
  if (open_pending) {
    return destination;
  }
  if (destination != NULL) {
    return destination;
  }
  open_pending = 1;
  tmp = getenv("LIBFIG_OUTPUT");
  if (!tmp) {
    do
      logFile = (char *)__libc_malloc(sizeof(char) * 256);
    while (!logFile);
    sprintf(logFile, "/tmp/libfig.out.%d", getpid());
  } else {
    do
      logFile = (char *)__libc_malloc(sizeof(char) * (strlen(tmp) + 8));
    while (!logFile);
    sprintf(logFile, "%s.%d", tmp, getpid());
  }
  while (destination == NULL) {
    destination = (FILE *)_IO_fopen(logFile, "a");
  }
  open_pending = 0;
  return destination;
}


x86 assembly (clang, -O0):
<getFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
cmpl   $0x0,0x2ee9(%rip)
je     1185 <getFile+0x25>
mov    0x2ee4(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12bc <getFile+0x15c>
cmpq   $0x0,0x2ed3(%rip)
je     11a3 <getFile+0x43>
mov    0x2ec6(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12bc <getFile+0x15c>
movl   $0x1,0x2eab(%rip)
lea    0xe4c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1215 <getFile+0xb5>
jmp    11cd <getFile+0x6d>
mov    $0x100,%edi
call   1060 <__libc_malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11cd <getFile+0x6d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    1278 <getFile+0x118>
jmp    121a <getFile+0xba>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x8,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <__libc_malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    121a <getFile+0xba>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    127d <getFile+0x11d>
cmpq   $0x0,0x2ddb(%rip)
jne    12a7 <getFile+0x147>
mov    -0x10(%rbp),%rdi
lea    0xd91(%rip),%rsi
call   1070 <_IO_fopen@plt>
mov    %rax,0x2dbe(%rip)
jmp    127d <getFile+0x11d>
movl   $0x0,0x2da7(%rip)
mov    0x2da8(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFile>:
push   %r14
push   %rbx
push   %rax
mov    0x2ef5(%rip),%rax
cmpb   $0x1,0x2ee6(%rip)
je     123e <getFile+0xde>
test   %rax,%rax
jne    123e <getFile+0xde>
movb   $0x1,0x2ed0(%rip)
lea    0xe71(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11e0 <getFile+0x80>
mov    %rax,%r14
nopl   0x0(%rax)
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x8(%rax),%edi
call   1060 <__libc_malloc@plt>
test   %rax,%rax
je     11a0 <getFile+0x40>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    120c <getFile+0xac>
nopw   0x0(%rax,%rax,1)
mov    $0x100,%edi
call   1060 <__libc_malloc@plt>
test   %rax,%rax
je     11e0 <getFile+0x80>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe0e(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    0x2e4d(%rip),%rax
test   %rax,%rax
jne    1237 <getFile+0xd7>
lea    0xe08(%rip),%r14
nop
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <_IO_fopen@plt>
mov    %rax,0x2e2e(%rip)
test   %rax,%rax
je     1220 <getFile+0xc0>
movb   $0x0,0x2e1a(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@getFile.open_pending = internal global i32 0
@getFile.destination = internal global i32* null
@.str = [14 x i8] c"LIBFIG_OUTPUT\00"
@.str.1 = [19 x i8] c"/tmp/libfig.out.%d\00"
@.str.2 = [6 x i8] c"%s.%d\00"
@.str.3 = [2 x i8] c"a\00"
define i32* @getFile() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i8*
%4 = load i32, i32* @getFile.open_pending
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i32*, i32** @getFile.destination
store i32* %7, i32** %1
br label %58
8: ; preds = %0
%9 = load i32*, i32** @getFile.destination
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = load i32*, i32** @getFile.destination
store i32* %12, i32** %1
br label %58
13: ; preds = %8
store i32 1, i32* @getFile.open_pending
%14 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %29, label %17
17: ; preds = %13
br label %18
18: ; preds = %21, %17
%19 = call i64 @__libc_malloc(i32 256)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %2
br label %21
21: ; preds = %18
%22 = load i8*, i8** %2
%23 = icmp ne i8* %22, null
%24 = xor i1 %23, true
br i1 %24, label %18, label %25
25: ; preds = %21
%26 = load i8*, i8** %2
%27 = call i32 (...) @getpid()
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %48
29: ; preds = %13
br label %30
30: ; preds = %39, %29
%31 = load i8*, i8** %3
%32 = call i32 @strlen(i8* %31)
%33 = add nsw i32 %32, 8
%34 = sext i32 %33 to i64
%35 = mul i64 1, %34
%36 = trunc i64 %35 to i32
%37 = call i64 @__libc_malloc(i32 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %2
br label %39
39: ; preds = %30
%40 = load i8*, i8** %2
%41 = icmp ne i8* %40, null
%42 = xor i1 %41, true
br i1 %42, label %30, label %43
43: ; preds = %39
%44 = load i8*, i8** %2
%45 = load i8*, i8** %3
%46 = call i32 (...) @getpid()
%47 = call i32 (i8*, i8*, ...) @sprintf(i8* %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %45, i32 %46)
br label %48
48: ; preds = %43, %25
br label %49
49: ; preds = %52, %48
%50 = load i32*, i32** @getFile.destination
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i8*, i8** %2
%54 = call i64 @_IO_fopen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%55 = inttoptr i64 %54 to i32*
store i32* %55, i32** @getFile.destination
br label %49
56: ; preds = %49
store i32 0, i32* @getFile.open_pending
%57 = load i32*, i32** @getFile.destination
store i32* %57, i32** %1
br label %58
58: ; preds = %56, %11, %6
%59 = load i32*, i32** %1
ret i32* %59
}

LLVM IR (-O2):
@getFile.open_pending = internal unnamed_addr global i1 false
@getFile.destination = internal unnamed_addr global i32* null
@.str = [14 x i8] c"LIBFIG_OUTPUT\00"
@.str.1 = [19 x i8] c"/tmp/libfig.out.%d\00"
@.str.2 = [6 x i8] c"%s.%d\00"
@.str.3 = [2 x i8] c"a\00"
define i32* @getFile() {
%1 = load i1, i1* @getFile.open_pending
%2 = load i32*, i32** @getFile.destination
%3 = xor i1 %1, true
%4 = icmp eq i32* %2, null
%5 = select i1 %3, i1 %4, i1 false
br i1 %5, label %6, label %37
6: ; preds = %0
store i1 true, i1* @getFile.open_pending
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %16
9: ; preds = %6, %9
%10 = tail call i64 @__libc_malloc(i32 256)
%11 = icmp eq i64 %10, 0
br i1 %11, label %9, label %12
12: ; preds = %9
%13 = inttoptr i64 %10 to i8*
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %25
16: ; preds = %6, %16
%17 = tail call i32 @strlen(i8* %7)
%18 = add nsw i32 %17, 8
%19 = tail call i64 @__libc_malloc(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %16, label %21
21: ; preds = %16
%22 = inttoptr i64 %19 to i8*
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %7, i32 %23)
br label %25
25: ; preds = %21, %12
%26 = phi i8* [ %22, %21 ], [ %13, %12 ]
%27 = load i32*, i32** @getFile.destination
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %35
29: ; preds = %25, %29
%30 = tail call i64 @_IO_fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = inttoptr i64 %30 to i32*
store i32* %31, i32** @getFile.destination
%32 = icmp eq i64 %30, 0
br i1 %32, label %29, label %33
33: ; preds = %29
%34 = inttoptr i64 %30 to i32*
br label %35
35: ; preds = %33, %25
%36 = phi i32* [ %27, %25 ], [ %34, %33 ]
store i1 false, i1* @getFile.open_pending
br label %37
37: ; preds = %0, %35
%38 = phi i32* [ %36, %35 ], [ %2, %0 ]
ret i32* %38
}