#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*/ F_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ check_alloc(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ stderr;
unsigned int strlen(char *);
char *test_outfile(char *name) {
  char *nname;
  unsigned int len, i = 1;
  FILE *file;
  if (access(name, F_OK) != -1) {
    len = strlen(name);
    check_alloc(nname = (char *)malloc(sizeof(char) * (len + 4)));
    sprintf(nname, "%s", name);
    while (access(nname, F_OK) != -1) {
      sprintf(nname, "%s.%u", name, i);
      i++;
    }
    if (i > 1) {
      free(name);
      name = nname;
    }
  }
  file = fopen(name, "a");
  if (file == NULL) {
    fprintf(stderr, "Couldn't open %s for writing. Exiting\n", name);
    exit(11);
  }
  fclose(file);
  return name;
}


x86 assembly (clang, -O0):
<test_outfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0xffffffff,%eax
je     1271 <test_outfile+0xd1>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x4,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rdi
mov    %rdi,-0x10(%rbp)
call   10c0 <check_alloc@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0xffffffff,%eax
je     1251 <test_outfile+0xb1>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    120f <test_outfile+0x6f>
cmpl   $0x1,-0x18(%rbp)
jbe    126c <test_outfile+0xcc>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1271 <test_outfile+0xd1>
mov    -0x8(%rbp),%rdi
lea    0xd8d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12b3 <test_outfile+0x113>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd67(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xb,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_outfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2e0e(%rip),%r12
mov    (%r12),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     1256 <test_outfile+0xa6>
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x4(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10d0 <check_alloc@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     1256 <test_outfile+0xa6>
mov    $0x1,%ebp
lea    0xde9(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   10b0 <sprintf@plt>
add    $0x1,%ebp
mov    (%r12),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
jne    1220 <test_outfile+0x70>
cmp    $0x2,%ebp
jb     1256 <test_outfile+0xa6>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%r14
lea    0xda9(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     127e <test_outfile+0xce>
mov    %rax,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    $0xb,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@F_OK = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [6 x i8] c"%s.%u\00"
@.str.2 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.3 = [39 x i8] c"Couldn't open %s for writing. Exiting\0A\00"
define i8* @test_outfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %2
store i32 1, i32* %5
%7 = load i8*, i8** %2
%8 = load i32, i32* @F_OK
%9 = call i32 @access(i8* %7, i32 %8)
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %45
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = add i32 %14, 4
%16 = zext i32 %15 to i64
%17 = mul i64 1, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %3
%21 = call i32 @check_alloc(i8* %20)
%22 = load i8*, i8** %3
%23 = load i8*, i8** %2
%24 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %30, %11
%26 = load i8*, i8** %3
%27 = load i32, i32* @F_OK
%28 = call i32 @access(i8* %26, i32 %27)
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %37
30: ; preds = %25
%31 = load i8*, i8** %3
%32 = load i8*, i8** %2
%33 = load i32, i32* %5
%34 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %32, i32 %33)
%35 = load i32, i32* %5
%36 = add i32 %35, 1
store i32 %36, i32* %5
br label %25
37: ; preds = %25
%38 = load i32, i32* %5
%39 = icmp ugt i32 %38, 1
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i8*, i8** %2
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** %3
store i8* %43, i8** %2
br label %44
44: ; preds = %40, %37
br label %45
45: ; preds = %44, %1
%46 = load i8*, i8** %2
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %47, i32** %6
%48 = load i32*, i32** %6
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %2
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 11)
unreachable
55: ; preds = %45
%56 = load i32*, i32** %6
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@F_OK = global i32 0
@.str.1 = [6 x i8] c"%s.%u\00"
@.str.2 = [2 x i8] c"a\00"
@stderr = global i32 0
@.str.3 = [39 x i8] c"Couldn't open %s for writing. Exiting\0A\00"
define i8* @test_outfile(i8* %0) {
%2 = load i32, i32* @F_OK
%3 = tail call i32 @access(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %26, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
%7 = add i32 %6, 4
%8 = tail call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i32 @check_alloc(i8* %9)
%11 = tail call i8* @strcpy(i8* dereferenceable(1) %9, i8* dereferenceable(1) %0)
%12 = load i32, i32* @F_OK
%13 = tail call i32 @access(i8* %9, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %26, label %15
15: ; preds = %5, %15
%16 = phi i32 [ %18, %15 ], [ 1, %5 ]
%17 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %16)
%18 = add i32 %16, 1
%19 = load i32, i32* @F_OK
%20 = tail call i32 @access(i8* %9, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %15
22: ; preds = %15
%23 = icmp ugt i32 %18, 1
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = tail call i32 @free(i8* %0)
br label %26
26: ; preds = %5, %22, %24, %1
%27 = phi i8* [ %9, %24 ], [ %0, %22 ], [ %0, %1 ], [ %0, %5 ]
%28 = tail call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%33 = tail call i32 @exit(i32 11)
unreachable
34: ; preds = %26
%35 = tail call i32 @fclose(i32* %28)
ret i8* %27
}