#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 size_t uint64_t;
typedef int uint32_t;

/* Variables and functions */
char *GetRealPath(char *);
int /*<<< orphan*/ O_WRONLY;
char *basename(char *);
int /*<<< orphan*/ close(int);
char *dirname(char *);
int errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
size_t pwrite(int, char *, size_t, size_t);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *);
char *strdup(char *);
char *strerror(int);
int strlen(char *);
char *writeInt(char *, int);
char *writeString(char *, char *, int);
int WriteFile(char *fileName, char *md5, uint64_t offset, char *data,
              uint32_t dataLen, char **buf, uint32_t *bufLen) {
  int err = 0;
  char *errStr = "";
  char *filePath = GetRealPath(fileName);
  uint32_t totalLen = 0;
  char *writeIndex = NULL;
  char tempFilePath[256] = {'\0'};
  int fd = -1;
  size_t toWrite = dataLen;
  size_t writed = 0;
  char *_tempFilePath = strdup(filePath);
  char *_tempFileName = strdup(fileName);
  sprintf(tempFilePath, "%s/.%s_%s", dirname(_tempFilePath), md5,
          basename(_tempFileName));
  free(_tempFilePath);
  free(_tempFileName);
  do {
    fd = open(tempFilePath, O_WRONLY);
    if (fd < 0) {
      err = errno;
      errStr = strerror(err);
      break;
    }
    while (toWrite > 0) {
      writed = pwrite(fd, data + writed, toWrite, offset);
      if (writed < 0) {
        err = errno;
        errStr = strerror(err);
        break;
      }
      toWrite -= writed;
      offset += writed;
    }
  } while (0);
  totalLen += 4 + 4 + strlen(errStr);
  *buf = malloc(totalLen + 4);
  writeIndex = *buf;
  *bufLen = totalLen + 4;
  writeIndex = writeInt(writeIndex, totalLen);
  writeIndex = writeInt(writeIndex, err);
  writeIndex = writeString(writeIndex, errStr, strlen(errStr));
  if (fd > 0) {
    close(fd);
    fd = -1;
  }
  if (filePath != NULL) {
    free(filePath);
  }
  return err;
}


x86 assembly (clang, -O0):
<WriteFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    0xde4(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1110 <GetRealPath@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movq   $0x0,-0x58(%rbp)
lea    -0x160(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
movl   $0xffffffff,-0x164(%rbp)
ff ff ff
movslq -0x24(%rbp),%rax
mov    %rax,-0x170(%rbp)
movq   $0x0,-0x178(%rbp)
mov    -0x48(%rbp),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x180(%rbp)
mov    -0x8(%rbp),%rdi
call   10f0 <strdup@plt>
mov    %rax,-0x188(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x1a0(%rbp)
mov    -0x180(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x198(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x190(%rbp)
mov    -0x188(%rbp),%rdi
call   1090 <basename@plt>
mov    -0x1a0(%rbp),%rdi
mov    -0x198(%rbp),%rdx
mov    -0x190(%rbp),%rcx
mov    %rax,%r8
lea    0xd11(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x180(%rbp),%rdi
call   1040 <free@plt>
mov    -0x188(%rbp),%rdi
call   1040 <free@plt>
lea    -0x160(%rbp),%rdi
mov    0x2cb6(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x164(%rbp)
cmpl   $0x0,-0x164(%rbp)
jge    1351 <WriteFile+0x161>
mov    0x2ca5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1100 <strerror@plt>
mov    %rax,-0x40(%rbp)
jmp    13e8 <WriteFile+0x1f8>
jmp    1356 <WriteFile+0x166>
cmpq   $0x0,-0x170(%rbp)
jbe    13e3 <WriteFile+0x1f3>
mov    -0x164(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    -0x178(%rbp),%rsi
mov    -0x170(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   10c0 <pwrite@plt>
mov    %rax,-0x178(%rbp)
cmpq   $0x0,-0x178(%rbp)
jae    13b7 <WriteFile+0x1c7>
mov    0x2c3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1100 <strerror@plt>
mov    %rax,-0x40(%rbp)
jmp    13e3 <WriteFile+0x1f3>
mov    -0x178(%rbp),%rcx
mov    -0x170(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x170(%rbp)
mov    -0x178(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1356 <WriteFile+0x166>
jmp    13e8 <WriteFile+0x1f8>
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x8,%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%edi
add    $0x4,%edi
call   10b0 <malloc@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x4c(%rbp),%ecx
add    $0x4,%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rdi
mov    -0x4c(%rbp),%esi
call   1030 <writeInt@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1030 <writeInt@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x1b0(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x1a8(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1b0(%rbp),%rdi
mov    -0x1a8(%rbp),%rsi
mov    %eax,%edx
call   10a0 <writeString@plt>
mov    %rax,-0x58(%rbp)
cmpl   $0x0,-0x164(%rbp)
jle    14a0 <WriteFile+0x2b0>
mov    -0x164(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x164(%rbp)
ff ff ff
cmpq   $0x0,-0x48(%rbp)
je     14b4 <WriteFile+0x2c4>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %r9,0x18(%rsp)
mov    %r8d,0xc(%rsp)
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,0x10(%rsp)
mov    %rdi,%r15
call   1100 <GetRealPath@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
mov    %rax,(%rsp)
mov    %rax,%rdi
call   10e0 <strdup@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   10e0 <strdup@plt>
mov    %rax,%rbp
mov    %r13,%rdi
call   1060 <dirname@plt>
mov    %rax,%r15
mov    %rbp,%rdi
call   1080 <basename@plt>
lea    0xd4d(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    %r15,%rdx
mov    0x10(%rsp),%rcx
mov    %rax,%r8
call   10d0 <sprintf@plt>
mov    %r13,%rdi
call   1040 <free@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10c0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     132d <WriteFile+0x14d>
lea    0xd10(%rip),%r14
mov    0xc(%rsp),%eax
test   %eax,%eax
je     1344 <WriteFile+0x164>
movslq %eax,%r15
xor    %r13d,%r13d
xor    %eax,%eax
nopl   0x0(%rax)
add    %r12,%rax
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
call   10b0 <pwrite@plt>
add    %rax,%rbx
sub    %rax,%r15
jne    1310 <WriteFile+0x130>
jmp    1347 <WriteFile+0x167>
mov    0x2cac(%rip),%rax
mov    (%rax),%r13d
mov    %r13d,%edi
call   10f0 <strerror@plt>
mov    %rax,%r14
jmp    1347 <WriteFile+0x167>
xor    %r13d,%r13d
mov    (%rsp),%r12
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0x8(%rbx),%r15d
add    $0xc,%ebx
mov    %ebx,%edi
call   10a0 <malloc@plt>
mov    0x18(%rsp),%rcx
mov    %rax,(%rcx)
mov    0x160(%rsp),%rcx
mov    %ebx,(%rcx)
mov    %rax,%rdi
mov    %r15d,%esi
call   1030 <writeInt@plt>
mov    %rax,%rdi
mov    %r13d,%esi
call   1030 <writeInt@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1090 <writeString@plt>
test   %ebp,%ebp
jle    13ae <WriteFile+0x1ce>
mov    %ebp,%edi
call   1070 <close@plt>
test   %r12,%r12
je     13bb <WriteFile+0x1db>
mov    %r12,%rdi
call   1040 <free@plt>
mov    %r13d,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [10 x i8] c"%s/.%s_%s\00"
@O_WRONLY = global i32 0
@errno = global i32 0
define i32 @WriteFile(i8* %0, i8* %1, i64 %2, i8* %3, i32 %4, i8** %5, i32* %6) {
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8**
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i32
%19 = alloca i8*
%20 = alloca [256 x i8]
%21 = alloca i32
%22 = alloca i64
%23 = alloca i64
%24 = alloca i8*
%25 = alloca i8*
store i8* %0, i8** %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i8* %3, i8** %11
store i32 %4, i32* %12
store i8** %5, i8*** %13
store i32* %6, i32** %14
store i32 0, i32* %15
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %16
%26 = load i8*, i8** %8
%27 = call i8* @GetRealPath(i8* %26)
store i8* %27, i8** %17
store i32 0, i32* %18
store i8* null, i8** %19
%28 = bitcast [256 x i8]* %20 to i8*
store i32 -1, i32* %21
%29 = load i32, i32* %12
%30 = sext i32 %29 to i64
store i64 %30, i64* %22
store i64 0, i64* %23
%31 = load i8*, i8** %17
%32 = call i8* @strdup(i8* %31)
store i8* %32, i8** %24
%33 = load i8*, i8** %8
%34 = call i8* @strdup(i8* %33)
store i8* %34, i8** %25
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %20, i64 0, i64 0
%36 = load i8*, i8** %24
%37 = call i8* @dirname(i8* %36)
%38 = load i8*, i8** %9
%39 = load i8*, i8** %25
%40 = call i8* @basename(i8* %39)
%41 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %37, i8* %38, i8* %40)
%42 = load i8*, i8** %24
%43 = call i32 @free(i8* %42)
%44 = load i8*, i8** %25
%45 = call i32 @free(i8* %44)
br label %46
46: ; preds = %7
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %20, i64 0, i64 0
%48 = load i32, i32* @O_WRONLY
%49 = call i32 @open(i8* %47, i32 %48)
store i32 %49, i32* %21
%50 = load i32, i32* %21
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %46
%53 = load i32, i32* @errno
store i32 %53, i32* %15
%54 = load i32, i32* %15
%55 = call i8* @strerror(i32 %54)
store i8* %55, i8** %16
br label %82
56: ; preds = %46
br label %57
57: ; preds = %74, %56
%58 = load i64, i64* %22
%59 = icmp ugt i64 %58, 0
br i1 %59, label %60, label %81
60: ; preds = %57
%61 = load i32, i32* %21
%62 = load i8*, i8** %11
%63 = load i64, i64* %23
%64 = getelementptr inbounds i8, i8* %62, i64 %63
%65 = load i64, i64* %22
%66 = load i64, i64* %10
%67 = call i64 @pwrite(i32 %61, i8* %64, i64 %65, i64 %66)
store i64 %67, i64* %23
%68 = load i64, i64* %23
%69 = icmp ult i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %60
%71 = load i32, i32* @errno
store i32 %71, i32* %15
%72 = load i32, i32* %15
%73 = call i8* @strerror(i32 %72)
store i8* %73, i8** %16
br label %81
74: ; preds = %60
%75 = load i64, i64* %23
%76 = load i64, i64* %22
%77 = sub i64 %76, %75
store i64 %77, i64* %22
%78 = load i64, i64* %23
%79 = load i64, i64* %10
%80 = add i64 %79, %78
store i64 %80, i64* %10
br label %57
81: ; preds = %70, %57
br label %82
82: ; preds = %81, %52
%83 = load i8*, i8** %16
%84 = call i32 @strlen(i8* %83)
%85 = add nsw i32 8, %84
%86 = load i32, i32* %18
%87 = add nsw i32 %86, %85
store i32 %87, i32* %18
%88 = load i32, i32* %18
%89 = add nsw i32 %88, 4
%90 = call i8* @malloc(i32 %89)
%91 = load i8**, i8*** %13
store i8* %90, i8** %91
%92 = load i8**, i8*** %13
%93 = load i8*, i8** %92
store i8* %93, i8** %19
%94 = load i32, i32* %18
%95 = add nsw i32 %94, 4
%96 = load i32*, i32** %14
store i32 %95, i32* %96
%97 = load i8*, i8** %19
%98 = load i32, i32* %18
%99 = call i8* @writeInt(i8* %97, i32 %98)
store i8* %99, i8** %19
%100 = load i8*, i8** %19
%101 = load i32, i32* %15
%102 = call i8* @writeInt(i8* %100, i32 %101)
store i8* %102, i8** %19
%103 = load i8*, i8** %19
%104 = load i8*, i8** %16
%105 = load i8*, i8** %16
%106 = call i32 @strlen(i8* %105)
%107 = call i8* @writeString(i8* %103, i8* %104, i32 %106)
store i8* %107, i8** %19
%108 = load i32, i32* %21
%109 = icmp sgt i32 %108, 0
br i1 %109, label %110, label %113
110: ; preds = %82
%111 = load i32, i32* %21
%112 = call i32 @close(i32 %111)
store i32 -1, i32* %21
br label %113
113: ; preds = %110, %82
%114 = load i8*, i8** %17
%115 = icmp ne i8* %114, null
br i1 %115, label %116, label %119
116: ; preds = %113
%117 = load i8*, i8** %17
%118 = call i32 @free(i8* %117)
br label %119
119: ; preds = %116, %113
%120 = load i32, i32* %15
ret i32 %120
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [10 x i8] c"%s/.%s_%s\00"
@O_WRONLY = global i32 0
@errno = global i32 0
define i32 @WriteFile(i8* %0, i8* %1, i64 %2, i8* nocapture %3, i32 %4, i8** nocapture writeonly %5, i32* nocapture writeonly %6) {
%8 = alloca [256 x i8]
%9 = tail call i8* @GetRealPath(i8* %0)
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%11 = tail call i8* @strdup(i8* %9)
%12 = tail call i8* @strdup(i8* %0)
%13 = tail call i8* @dirname(i8* %11)
%14 = tail call i8* @basename(i8* %12)
%15 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %1, i8* %14)
%16 = tail call i32 @free(i8* %11)
%17 = tail call i32 @free(i8* %12)
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* %10, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %7
%22 = icmp eq i32 %4, 0
br i1 %22, label %37, label %23
23: ; preds = %21
%24 = sext i32 %4 to i64
br label %28
25: ; preds = %7
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
br label %37
28: ; preds = %23, %28
%29 = phi i64 [ %35, %28 ], [ %2, %23 ]
%30 = phi i64 [ %33, %28 ], [ 0, %23 ]
%31 = phi i64 [ %34, %28 ], [ %24, %23 ]
%32 = getelementptr inbounds i8, i8* %3, i64 %30
%33 = tail call i64 @pwrite(i32 %19, i8* %32, i64 %31, i64 %29)
%34 = sub i64 %31, %33
%35 = add i64 %33, %29
%36 = icmp eq i64 %34, 0
br i1 %36, label %37, label %28
37: ; preds = %28, %21, %25
%38 = phi i8* [ %27, %25 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %21 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %28 ]
%39 = phi i32 [ %26, %25 ], [ 0, %21 ], [ 0, %28 ]
%40 = tail call i32 @strlen(i8* %38)
%41 = add nsw i32 %40, 8
%42 = add nsw i32 %40, 12
%43 = tail call i8* @malloc(i32 %42)
store i8* %43, i8** %5
store i32 %42, i32* %6
%44 = tail call i8* @writeInt(i8* %43, i32 %41)
%45 = tail call i8* @writeInt(i8* %44, i32 %39)
%46 = tail call i32 @strlen(i8* %38)
%47 = tail call i8* @writeString(i8* %45, i8* %38, i32 %46)
%48 = icmp sgt i32 %19, 0
br i1 %48, label %49, label %51
49: ; preds = %37
%50 = tail call i32 @close(i32 %19)
br label %51
51: ; preds = %49, %37
%52 = icmp eq i8* %9, null
br i1 %52, label %55, label %53
53: ; preds = %51
%54 = tail call i32 @free(i8* %9)
br label %55
55: ; preds = %53, %51
ret i32 %39
}