#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 O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int write(int, char *, int);
int create_tmp_file(char *file_name, int len) {
  int fd, wr;
  char tmp = 0;
  fd = open(file_name, O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
  if (fd == -1) {
    printf("%s\n", file_name);
    perror("error creating file");
    return -1;
  }
  while (len--) {
    wr = write(fd, &tmp, 1);
    if (wr <= 0) {
      printf("%s\n", file_name);
      perror("error writing file");
      close(fd);
      return -1;
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<create_tmp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movb   $0x0,-0x1d(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    0x2e47(%rip),%rax
or     (%rax),%edx
mov    0x2e36(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ce <create_tmp_file+0x7e>
mov    -0x10(%rbp),%rsi
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <create_tmp_file+0xf9>
jmp    11d3 <create_tmp_file+0x83>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     123a <create_tmp_file+0xea>
mov    -0x18(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1235 <create_tmp_file+0xe5>
mov    -0x10(%rbp),%rsi
lea    0xdf0(%rip),%rdi
call   1040 <printf@plt>
lea    0xdfc(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <create_tmp_file+0xf9>
jmp    11d3 <create_tmp_file+0x83>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_tmp_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e7d(%rip),%rax
mov    0x2e8e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
movb   $0x0,0x7(%rsp)
mov    0x2e3c(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1205 <create_tmp_file+0xb5>
mov    %eax,%ebx
add    $0x1,%ebp
lea    0x7(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0xffffffff,%ebp
je     11f1 <create_tmp_file+0xa1>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
jg     11b0 <create_tmp_file+0x60>
lea    0xe31(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
jmp    11fa <create_tmp_file+0xaa>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf4(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11fa <create_tmp_file+0xaa>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [20 x i8] c"error creating file\00"
@.str.2 = [19 x i8] c"error writing file\00"
define i32 @create_tmp_file(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8 0, i8* %8
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %9, i32 %12, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
25: ; preds = %2
br label %26
26: ; preds = %41, %25
%27 = load i32, i32* %5
%28 = add nsw i32 %27, -1
store i32 %28, i32* %5
%29 = icmp ne i32 %27, 0
br i1 %29, label %30, label %42
30: ; preds = %26
%31 = load i32, i32* %6
%32 = call i32 @write(i32 %31, i8* %8, i32 1)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i8*, i8** %4
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %3
br label %45
41: ; preds = %30
br label %26
42: ; preds = %26
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %35, %21
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [20 x i8] c"error creating file\00"
@.str.2 = [19 x i8] c"error writing file\00"
define i32 @create_tmp_file(i8* %0, i32 %1) {
%3 = alloca i8
store i8 0, i8* %3
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IRGRP
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %0, i32 %6, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %30
17: ; preds = %2, %20
%18 = phi i32 [ %21, %20 ], [ %1, %2 ]
%19 = icmp eq i32 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %17
%21 = add nsw i32 %18, -1
%22 = call i32 @write(i32 %12, i8* %3, i32 1)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %17
24: ; preds = %20
%25 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
%26 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @close(i32 %12)
br label %30
28: ; preds = %17
%29 = tail call i32 @close(i32 %12)
br label %30
30: ; preds = %28, %24, %14
%31 = phi i32 [ -1, %14 ], [ -1, %24 ], [ 0, %28 ]
ret i32 %31
}