#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 fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ remove(char const *);
int /*<<< orphan*/ rename(char *, char const *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int);
int lock_fclose(FILE *fp, const char *filename, int *info) {
  int ret = 0;
  char newfile[512];
  if (fp != NULL) {
    ret = fclose(fp);
    sprintf(newfile, "%s_%04d.tmp", filename, *info);
    remove(filename);
    rename(newfile, filename);
    return ret;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<lock_fclose>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b6 <lock_fclose+0x76>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    %eax,-0x24(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
lea    0xe73(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <remove@plt>
lea    -0x230(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <rename@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <lock_fclose+0x7d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_fclose>:
test   %rdi,%rdi
je     1196 <lock_fclose+0x56>
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
call   1040 <fclose@plt>
mov    %eax,%ebp
mov    (%r14),%ecx
lea    0xe99(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <remove@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <rename@plt>
mov    %ebp,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x1,%eax
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s_%04d.tmp\00"
define i32 @lock_fclose(i32* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [512 x i8]
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 0, i32* %8
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %26
12: ; preds = %3
%13 = load i32*, i32** %5
%14 = call i32 @fclose(i32* %13)
store i32 %14, i32* %8
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%16 = load i8*, i8** %6
%17 = load i32*, i32** %7
%18 = load i32, i32* %17
%19 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %18)
%20 = load i8*, i8** %6
%21 = call i32 @remove(i8* %20)
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%23 = load i8*, i8** %6
%24 = call i32 @rename(i8* %22, i8* %23)
%25 = load i32, i32* %8
store i32 %25, i32* %4
br label %27
26: ; preds = %3
store i32 1, i32* %4
br label %27
27: ; preds = %26, %12
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s_%04d.tmp\00"
define i32 @lock_fclose(i32* %0, i8* %1, i32* nocapture %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = icmp eq i32* %0, null
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = tail call i32 @fclose(i32* %0)
%9 = load i32, i32* %2
%10 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %9)
%11 = tail call i32 @remove(i8* %1)
%12 = call i32 @rename(i8* %5, i8* %1)
br label %13
13: ; preds = %3, %7
%14 = phi i32 [ %8, %7 ], [ 1, %3 ]
ret i32 %14
}