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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int);
FILE *lock_fopen(const char *filename, int *info) {
  char newfile[2048];
  FILE *fp;
  int no = 0;
  do {
    snprintf(newfile, sizeof(newfile), "%s_%04d.tmp", filename, ++no);
  } while ((fp = fopen(newfile, "r")) && (fclose(fp), no < 9999));
  if (no >= 9999)
    return NULL;
  *info = no;
  return fopen(newfile, "w");
}


x86 assembly (clang, -O0):
<lock_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x82c(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x82c(%rbp),%r8d
add    $0x1,%r8d
mov    %r8d,-0x82c(%rbp)
mov    $0x800,%esi
lea    0xe8a(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x820(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x828(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x82d(%rbp)
je     11c9 <lock_fopen+0x99>
mov    -0x828(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x270f,-0x82c(%rbp)
setl   %al
mov    %al,-0x82d(%rbp)
mov    -0x82d(%rbp),%al
test   $0x1,%al
jne    114d <lock_fopen+0x1d>
cmpl   $0x270f,-0x82c(%rbp)
jl     11f4 <lock_fopen+0xc4>
movq   $0x0,-0x8(%rbp)
jmp    1217 <lock_fopen+0xe7>
mov    -0x82c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x820(%rbp),%rdi
lea    0xe00(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_fopen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r13
lea    0xeb0(%rip),%r12
lea    0x10(%rsp),%rbx
lea    0xeb0(%rip),%rbp
xor    %r15d,%r15d
nop
lea    0x1(%r15),%r14d
mov    %rbx,%rdi
mov    $0x800,%esi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %r14d,%r8d
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a0 <lock_fopen+0x70>
mov    %rax,%rdi
call   1030 <fclose@plt>
mov    %r14d,%r15d
cmp    $0x270f,%r14d
jne    1160 <lock_fopen+0x30>
jmp    11a9 <lock_fopen+0x79>
cmp    $0x270d,%r15d
jbe    11ad <lock_fopen+0x7d>
xor    %eax,%eax
jmp    11ca <lock_fopen+0x9a>
add    $0x1,%r15d
mov    0x8(%rsp),%rax
mov    %r15d,(%rax)
lea    0xe4e(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1050 <fopen@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s_%04d.tmp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
define i32* @lock_fopen(i8* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [2048 x i8]
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %8
br label %9
9: ; preds = %24, %2
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = load i32, i32* %8
%13 = add nsw i32 %12, 1
store i32 %13, i32* %8
%14 = call i32 @snprintf(i8* %10, i32 2048, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %13)
br label %15
15: ; preds = %9
%16 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %7
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32*, i32** %7
%21 = call i32 @fclose(i32* %20)
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 9999
br label %24
24: ; preds = %19, %15
%25 = phi i1 [ false, %15 ], [ %23, %19 ]
br i1 %25, label %9, label %26
26: ; preds = %24
%27 = load i32, i32* %8
%28 = icmp sge i32 %27, 9999
br i1 %28, label %29, label %30
29: ; preds = %26
store i32* null, i32** %3
br label %35
30: ; preds = %26
%31 = load i32, i32* %8
%32 = load i32*, i32** %5
store i32 %31, i32* %32
%33 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %3
br label %35
35: ; preds = %30, %29
%36 = load i32*, i32** %3
ret i32* %36
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s_%04d.tmp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
define noalias i32* @lock_fopen(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %11, %2
%6 = phi i32 [ 0, %2 ], [ %7, %11 ]
%7 = add nuw nsw i32 %6, 1
%8 = call i32 @snprintf(i8* %4, i32 2048, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %7)
%9 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %5
%12 = call i32 @fclose(i32* %9)
%13 = icmp eq i32 %7, 9999
br i1 %13, label %18, label %5
14: ; preds = %5
%15 = icmp ugt i32 %6, 9997
br i1 %15, label %18, label %16
16: ; preds = %14
store i32 %7, i32* %1
%17 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %11, %14, %16
%19 = phi i32* [ %17, %16 ], [ null, %14 ], [ null, %11 ]
ret i32* %19
}