#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*/ tmpFilename;

/* Variables and functions */
int EIO;
int errno;
int /*<<< orphan*/ logError(char *, int, char *, char const *, int, char *);
scalar_t__ rename(char *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strerror(int);
int writeToFile(char *, char const *, int const);
int safeWriteToFile(const char *filename, const char *buff,
                    const int file_size) {
  char tmpFilename[256];
  int result;
  snprintf(tmpFilename, sizeof(tmpFilename), "%s.tmp", filename);
  if ((result = writeToFile(tmpFilename, buff, file_size)) != 0) {
    return result;
  }
  if (rename(tmpFilename, filename) != 0) {
    result = errno != 0 ? errno : EIO;
    logError("file: "
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/fatedier/studies/refs/heads/master/"
             "fastdfs-5.01/common/shared_func.c"
             ", line: %d, "
             "rename file \"%s\" to \"%s\" fail, "
             "errno: %d, error info: %s",
             1165, tmpFilename, filename, result,
             (strerror(result) != NULL ? strerror(result) : "Unkown error"));
    return result;
  }
  return 0;
}


x86 assembly (clang, -O0):
<safeWriteToFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe83(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <writeToFile@plt>
mov    %eax,-0x124(%rbp)
cmp    $0x0,%eax
je     11b2 <safeWriteToFile+0x62>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <safeWriteToFile+0x161>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0x0,%rax
je     12aa <safeWriteToFile+0x15a>
mov    0x2e05(%rip),%rax
cmpl   $0x0,(%rax)
je     11f0 <safeWriteToFile+0xa0>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
jmp    11ff <safeWriteToFile+0xaf>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    -0x128(%rbp),%eax
mov    %eax,-0x124(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x124(%rbp),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x124(%rbp),%edi
call   1070 <strerror@plt>
cmp    $0x0,%rax
je     125c <safeWriteToFile+0x10c>
mov    -0x124(%rbp),%edi
call   1070 <strerror@plt>
mov    %rax,-0x148(%rbp)
jmp    126f <safeWriteToFile+0x11f>
lea    0xe8d(%rip),%rax
mov    %rax,-0x148(%rbp)
jmp    126f <safeWriteToFile+0x11f>
mov    -0x12c(%rbp),%r8d
mov    -0x138(%rbp),%rcx
mov    -0x140(%rbp),%rdx
mov    -0x148(%rbp),%r9
lea    0xd75(%rip),%rdi
mov    $0x48d,%esi
call   1050 <logError@plt>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <safeWriteToFile+0x161>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeWriteToFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe94(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <writeToFile@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11f5 <safeWriteToFile+0xa5>
mov    %rsp,%rdi
mov    %r14,%rsi
call   1060 <rename@plt>
test   %rax,%rax
je     11d0 <safeWriteToFile+0x80>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ebp
test   %ebp,%ebp
jne    11b8 <safeWriteToFile+0x68>
mov    0x2e2a(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1070 <strerror@plt>
test   %rax,%rax
je     11d4 <safeWriteToFile+0x84>
mov    %ebp,%edi
call   1070 <strerror@plt>
mov    %rax,%r9
jmp    11db <safeWriteToFile+0x8b>
xor    %ebp,%ebp
jmp    11f5 <safeWriteToFile+0xa5>
lea    0xf15(%rip),%r9
lea    0xe25(%rip),%rdi
mov    %rsp,%rdx
mov    $0x48d,%esi
mov    %r14,%rcx
mov    %ebp,%r8d
call   1050 <logError@plt>
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s.tmp\00"
@errno = global i32 0
@EIO = global i32 0
@.str.1 = [233 x i8] c"file: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/fatedier/studies/refs/heads/master/fastdfs-5.01/common/shared_func.c, line: %d, rename file \22%s\22 to \22%s\22 fail, errno: %d, error info: %s\00"
@.str.2 = [13 x i8] c"Unkown error\00"
define i32 @safeWriteToFile(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%11 = load i8*, i8** %5
%12 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @writeToFile(i8* %13, i8* %14, i32 %15)
store i32 %16, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i32, i32* %9
store i32 %19, i32* %4
br label %49
20: ; preds = %3
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%22 = load i8*, i8** %5
%23 = call i64 @rename(i8* %21, i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %48
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i32, i32* @errno
br label %32
30: ; preds = %25
%31 = load i32, i32* @EIO
br label %32
32: ; preds = %30, %28
%33 = phi i32 [ %29, %28 ], [ %31, %30 ]
store i32 %33, i32* %9
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%35 = load i8*, i8** %5
%36 = load i32, i32* %9
%37 = load i32, i32* %9
%38 = call i8* @strerror(i32 %37)
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* %9
%42 = call i8* @strerror(i32 %41)
br label %44
43: ; preds = %32
br label %44
44: ; preds = %43, %40
%45 = phi i8* [ %42, %40 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %43 ]
%46 = call i32 @logError(i8* getelementptr inbounds ([233 x i8], [233 x i8]* @.str.1, i64 0, i64 0), i32 1165, i8* %34, i8* %35, i32 %36, i8* %45)
%47 = load i32, i32* %9
store i32 %47, i32* %4
br label %49
48: ; preds = %20
store i32 0, i32* %4
br label %49
49: ; preds = %48, %44, %18
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s.tmp\00"
@errno = global i32 0
@EIO = global i32 0
@.str.1 = [233 x i8] c"file: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/fatedier/studies/refs/heads/master/fastdfs-5.01/common/shared_func.c, line: %d, rename file \22%s\22 to \22%s\22 fail, errno: %d, error info: %s\00"
@.str.2 = [13 x i8] c"Unkown error\00"
define i32 @safeWriteToFile(i8* %0, i8* %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i32 @writeToFile(i8* %5, i8* %1, i32 %2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %3
%10 = call i64 @rename(i8* %5, i8* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = icmp eq i32 %13, 0
%15 = load i32, i32* @EIO
%16 = select i1 %14, i32 %15, i32 %13
%17 = call i8* @strerror(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %12
%20 = call i8* @strerror(i32 %16)
br label %21
21: ; preds = %12, %19
%22 = phi i8* [ %20, %19 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %12 ]
%23 = call i32 @logError(i8* getelementptr inbounds ([233 x i8], [233 x i8]* @.str.1, i64 0, i64 0), i32 1165, i8* %5, i8* %0, i32 %16, i8* %22)
br label %24
24: ; preds = %9, %3, %21
%25 = phi i32 [ %16, %21 ], [ %7, %3 ], [ 0, %9 ]
ret i32 %25
}