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


x86 assembly (clang, -O0):
<lock_fclose>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e0 <lock_fclose+0xa0>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    %eax,-0x24(%rbp)
lea    -0x830(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8d
mov    $0x800,%esi
lea    0xe6d(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <remove@plt>
cmp    $0x0,%rax
je     11c5 <lock_fclose+0x85>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jne    11d5 <lock_fclose+0x95>
lea    -0x830(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <rename@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <lock_fclose+0xa7>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_fclose>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
test   %rdi,%rdi
je     11a9 <lock_fclose+0x69>
mov    %rdx,%rbp
mov    %rsi,%rbx
call   1040 <fclose@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%r8d
lea    0xe97(%rip),%rdx
mov    %rsp,%rdi
mov    $0x800,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <remove@plt>
test   %rax,%rax
je     119c <lock_fclose+0x5c>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
jne    11af <lock_fclose+0x6f>
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1060 <rename@plt>
jmp    11af <lock_fclose+0x6f>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s_%04d.tmp\00"
@errno = global i64 0
@ENOENT = global i64 0
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 [2048 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 %33
12: ; preds = %3
%13 = load i32*, i32** %5
%14 = call i32 @fclose(i32* %13)
store i32 %14, i32* %8
%15 = getelementptr inbounds [2048 x i8], [2048 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 @snprintf(i8* %15, i32 2048, 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 i64 @remove(i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %12
%24 = load i64, i64* @errno
%25 = load i64, i64* @ENOENT
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %23, %12
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%29 = load i8*, i8** %6
%30 = call i32 @rename(i8* %28, i8* %29)
br label %31
31: ; preds = %27, %23
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %34
33: ; preds = %3
store i32 1, i32* %4
br label %34
34: ; preds = %33, %31
%35 = load i32, i32* %4
ret i32 %35
}

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