#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int);
FILE *lock_fopen(const char *filename, int *info) {
  char newfile[512];
  FILE *fp;
  int no = 0;
  do {
    sprintf(newfile, "%s_%04d.tmp", filename, ++no);
  } while ((fp = fopen(newfile, "r")) && (fclose(fp), no < 9999));
  *info = no;
  return fopen(newfile, "w");
}


x86 assembly (clang, -O0):
<lock_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x21c(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x21c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x21c(%rbp)
lea    0xe92(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x210(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x218(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21d(%rbp)
je     11c1 <lock_fopen+0x91>
mov    -0x218(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x270f,-0x21c(%rbp)
setl   %al
mov    %al,-0x21d(%rbp)
mov    -0x21d(%rbp),%al
test   $0x1,%al
jne    114d <lock_fopen+0x1d>
mov    -0x21c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x210(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1040 <fopen@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_fopen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x1,%eax
lea    0xead(%rip),%r15
mov    %rsp,%rbx
lea    0xeaf(%rip),%r13
nopl   (%rax)
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %eax,%ecx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1194 <lock_fopen+0x64>
mov    %rax,%rdi
call   1030 <fclose@plt>
lea    0x1(%rbp),%eax
cmp    $0x2710,%eax
jne    1160 <lock_fopen+0x30>
mov    %ebp,(%r14)
lea    0xe70(%rip),%rsi
mov    %rsp,%rdi
call   1040 <fopen@plt>
add    $0x208,%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 i8*
%4 = alloca i32*
%5 = alloca [512 x i8]
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %7
br label %8
8: ; preds = %23, %2
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = load i32, i32* %7
%12 = add nsw i32 %11, 1
store i32 %12, i32* %7
%13 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %12)
br label %14
14: ; preds = %8
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32*, i32** %6
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 9999
br label %23
23: ; preds = %18, %14
%24 = phi i1 [ false, %14 ], [ %22, %18 ]
br i1 %24, label %8, label %25
25: ; preds = %23
%26 = load i32, i32* %7
%27 = load i32*, i32** %4
store i32 %26, i32* %27
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret i32* %29
}

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 [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 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 @sprintf(i8* %4, i8* dereferenceable(1) 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 = tail call i32 @fclose(i32* %9)
%13 = icmp eq i32 %7, 9999
br i1 %13, label %14, label %5
14: ; preds = %5, %11
store i32 %7, i32* %1
%15 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret i32* %15
}