#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int mkstemp(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strerror(int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
char *writeToFile(char *data) {
  int filedes, errno, error_;
  filedes = -1;
  error_ = 0;
  errno = 0;
  if (data == NULL) {
    printf("Null-pointer in 'write_to_file' function\n");
    return NULL;
  }
  char *fileName = malloc(20 * sizeof(char));
  sprintf(fileName, "%s", "./clashCoSim-XXXXXX");
  if ((filedes = mkstemp(fileName)) < 1) {
    printf("Creation of temp file failed with error [%s]\n", strerror(errno));
    error_ = 1;
  } else {
    if (write(filedes, data, strlen(data)) == -1) {
      printf("Write failed with error [%s]\n", strerror(errno));
      error_ = 1;
    }
  }
  free(data);
  close(filedes);
  if (error_) {
    if (fileName != NULL)
      free(fileName);
    return NULL;
  } else {
    return fileName;
  }
}


x86 assembly (clang, -O0):
<writeToFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d7 <writeToFile+0x47>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cd <writeToFile+0x13d>
mov    $0x14,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe3a(%rip),%rsi
lea    0xe36(%rip),%rdx
call   10a0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
call   1090 <mkstemp@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x1,%eax
jge    1236 <writeToFile+0xa6>
mov    -0x18(%rbp),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x1c(%rbp)
jmp    1289 <writeToFile+0xf9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1284 <writeToFile+0xf4>
mov    -0x18(%rbp),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x1c(%rbp)
jmp    1289 <writeToFile+0xf9>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12c5 <writeToFile+0x135>
cmpq   $0x0,-0x28(%rbp)
je     12b8 <writeToFile+0x128>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cd <writeToFile+0x13d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeToFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11f5 <writeToFile+0x65>
mov    %rdi,%r14
mov    $0x14,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
movups 0xe4d(%rip),%xmm0
movups %xmm0,(%rax)
movl   $0x585858,0x10(%rax)
mov    %rax,%rdi
call   10a0 <mkstemp@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1203 <writeToFile+0x73>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     120c <writeToFile+0x7c>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1240 <writeToFile+0xb0>
lea    0xe64(%rip),%rdi
call   1040 <puts@plt>
jmp    123e <writeToFile+0xae>
lea    0xe0a(%rip),%r15
jmp    1213 <writeToFile+0x83>
lea    0xe2f(%rip),%r15
xor    %edi,%edi
call   10b0 <strerror@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Null-pointer in 'write_to_file' function\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [20 x i8] c"./clashCoSim-XXXXXX\00"
@.str.3 = [46 x i8] c"Creation of temp file failed with error [%s]\0A\00"
@.str.4 = [30 x i8] c"Write failed with error [%s]\0A\00"
define i8* @writeToFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32 -1, i32* %4
store i32 0, i32* %6
store i32 0, i32* %5
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %51
12: ; preds = %1
%13 = call i8* @malloc(i32 20)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%16 = load i8*, i8** %7
%17 = call i32 @mkstemp(i8* %16)
store i32 %17, i32* %4
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = load i32, i32* %5
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %21)
store i32 1, i32* %6
br label %35
23: ; preds = %12
%24 = load i32, i32* %4
%25 = load i8*, i8** %3
%26 = load i8*, i8** %3
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @write(i32 %24, i8* %25, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* %5
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %32)
store i32 1, i32* %6
br label %34
34: ; preds = %30, %23
br label %35
35: ; preds = %34, %19
%36 = load i8*, i8** %3
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %6
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %35
%43 = load i8*, i8** %7
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i8*, i8** %7
%47 = call i32 @free(i8* %46)
br label %48
48: ; preds = %45, %42
store i8* null, i8** %2
br label %51
49: ; preds = %35
%50 = load i8*, i8** %7
store i8* %50, i8** %2
br label %51
51: ; preds = %49, %48, %10
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@.str.2 = [20 x i8] c"./clashCoSim-XXXXXX\00"
@.str.3 = [46 x i8] c"Creation of temp file failed with error [%s]\0A\00"
@.str.4 = [30 x i8] c"Write failed with error [%s]\0A\00"
@str = [41 x i8] c"Null-pointer in 'write_to_file' function\00"
define i8* @writeToFile(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %23
5: ; preds = %1
%6 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(20) %6, i8* align 1 dereferenceable(20) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64 20, i1 false)
%7 = tail call i32 @mkstemp(i8* %6)
%8 = icmp slt i32 %7, 1
br i1 %8, label %13, label %9
9: ; preds = %5
%10 = tail call i32 @strlen(i8* %0)
%11 = tail call i32 @write(i32 %7, i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %9, %5
%14 = phi i8* [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), %5 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), %9 ]
%15 = tail call i8* @strerror(i32 0)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %14, i8* %15)
%17 = tail call i32 @free(i8* %0)
%18 = tail call i32 @close(i32 %7)
%19 = tail call i32 @free(i8* %6)
br label %23
20: ; preds = %9
%21 = tail call i32 @free(i8* %0)
%22 = tail call i32 @close(i32 %7)
br label %23
23: ; preds = %20, %13, %3
%24 = phi i8* [ null, %3 ], [ %6, %20 ], [ null, %13 ]
ret i8* %24
}