#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ lseek(int, int, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int read(int, char *, int);
int /*<<< orphan*/ stat(char *, struct stat *);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int save_data_to_file(void *ptr, size_t len, int index, int file_desc,
                      char *filename) {
  struct stat st;
  stat(filename, &st);
  int size = st.st_size;
  char *before_index = malloc(index);
  if (read(file_desc, before_index, index) != index)
    return 1;
  if (lseek(file_desc, index, SEEK_SET) < 0)
    return 1;
  char *after_index = malloc(size - index);
  if (read(file_desc, after_index, (size - index)) != (size - index))
    return 1;
  if (lseek(file_desc, 0, SEEK_SET) < 0)
    return 1;
  char *all = malloc(size + index);
  memcpy(all, before_index, index);
  memcpy((char *)all + index, ptr, len);
  memcpy((char *)all + index + len, after_index, (size - index));
  if (write(file_desc, all, strlen(all)) != strlen(all))
    return 1;
  if (lseek(file_desc, 0, SEEK_SET) < 0)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<save_data_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
cmp    -0x1c(%rbp),%eax
je     11cd <save_data_to_file+0x5d>
movl   $0x1,-0x4(%rbp)
jmp    1331 <save_data_to_file+0x1c1>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    11f7 <save_data_to_file+0x87>
movl   $0x1,-0x4(%rbp)
jmp    1331 <save_data_to_file+0x1c1>
mov    -0x34(%rbp),%edi
sub    -0x1c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x34(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    -0x34(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
cmp    %ecx,%eax
je     1232 <save_data_to_file+0xc2>
movl   $0x1,-0x4(%rbp)
jmp    1331 <save_data_to_file+0x1c1>
mov    -0x20(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    125b <save_data_to_file+0xeb>
movl   $0x1,-0x4(%rbp)
jmp    1331 <save_data_to_file+0x1c1>
mov    -0x34(%rbp),%edi
add    -0x1c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <memcpy@plt>
mov    -0x50(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x50(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
add    -0x18(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x34(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1080 <memcpy@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
cmp    %rcx,%rax
je     1301 <save_data_to_file+0x191>
movl   $0x1,-0x4(%rbp)
jmp    1331 <save_data_to_file+0x1c1>
mov    -0x20(%rbp),%edi
mov    0x2cd5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    132a <save_data_to_file+0x1ba>
movl   $0x1,-0x4(%rbp)
jmp    1331 <save_data_to_file+0x1c1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_data_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r13d
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %r8,%rdi
call   1070 <stat@plt>
mov    (%rsp),%r15d
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %r13d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
mov    $0x1,%ecx
cmp    %ebp,%eax
jne    12b8 <save_data_to_file+0x148>
mov    %r14,0x10(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    %ebp,%esi
call   1050 <lseek@plt>
test   %rax,%rax
js     12b3 <save_data_to_file+0x143>
mov    %r12,0x8(%rsp)
mov    %r15d,%r12d
sub    %ebp,%r12d
mov    %r12d,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %r13d,%edi
mov    %rax,%rsi
mov    %r12d,%edx
call   1060 <read@plt>
cmp    %r12d,%eax
jne    12b3 <save_data_to_file+0x143>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   %rax,%rax
js     12b3 <save_data_to_file+0x143>
add    %ebp,%r15d
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1080 <memcpy@plt>
movslq %ebp,%rbx
add    %r15,%rbx
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rbp
mov    %ebp,%edx
call   1080 <memcpy@plt>
add    %rbp,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1080 <memcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbx
mov    $0x1,%ecx
jne    12b8 <save_data_to_file+0x148>
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,%rcx
shr    $0x3f,%rcx
jmp    12b8 <save_data_to_file+0x148>
mov    $0x1,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@SEEK_SET = global i32 0
define i32 @save_data_to_file(i8* %0, i64 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.stat
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%17 = load i8*, i8** %11
%18 = call i32 @stat(i8* %17, %struct.stat* %12)
%19 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%20 = load i32, i32* %19
store i32 %20, i32* %13
%21 = load i32, i32* %9
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %14
%23 = load i32, i32* %10
%24 = load i8*, i8** %14
%25 = load i32, i32* %9
%26 = call i32 @read(i32 %23, i8* %24, i32 %25)
%27 = load i32, i32* %9
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %5
store i32 1, i32* %6
br label %103
30: ; preds = %5
%31 = load i32, i32* %10
%32 = load i32, i32* %9
%33 = load i32, i32* @SEEK_SET
%34 = call i64 @lseek(i32 %31, i32 %32, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 1, i32* %6
br label %103
37: ; preds = %30
%38 = load i32, i32* %13
%39 = load i32, i32* %9
%40 = sub nsw i32 %38, %39
%41 = call i8* @malloc(i32 %40)
store i8* %41, i8** %15
%42 = load i32, i32* %10
%43 = load i8*, i8** %15
%44 = load i32, i32* %13
%45 = load i32, i32* %9
%46 = sub nsw i32 %44, %45
%47 = call i32 @read(i32 %42, i8* %43, i32 %46)
%48 = load i32, i32* %13
%49 = load i32, i32* %9
%50 = sub nsw i32 %48, %49
%51 = icmp ne i32 %47, %50
br i1 %51, label %52, label %53
52: ; preds = %37
store i32 1, i32* %6
br label %103
53: ; preds = %37
%54 = load i32, i32* %10
%55 = load i32, i32* @SEEK_SET
%56 = call i64 @lseek(i32 %54, i32 0, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %53
store i32 1, i32* %6
br label %103
59: ; preds = %53
%60 = load i32, i32* %13
%61 = load i32, i32* %9
%62 = add nsw i32 %60, %61
%63 = call i8* @malloc(i32 %62)
store i8* %63, i8** %16
%64 = load i8*, i8** %16
%65 = load i8*, i8** %14
%66 = load i32, i32* %9
%67 = call i32 @memcpy(i8* %64, i8* %65, i32 %66)
%68 = load i8*, i8** %16
%69 = load i32, i32* %9
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i8*, i8** %7
%73 = load i64, i64* %8
%74 = trunc i64 %73 to i32
%75 = call i32 @memcpy(i8* %71, i8* %72, i32 %74)
%76 = load i8*, i8** %16
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = load i64, i64* %8
%81 = getelementptr inbounds i8, i8* %79, i64 %80
%82 = load i8*, i8** %15
%83 = load i32, i32* %13
%84 = load i32, i32* %9
%85 = sub nsw i32 %83, %84
%86 = call i32 @memcpy(i8* %81, i8* %82, i32 %85)
%87 = load i32, i32* %10
%88 = load i8*, i8** %16
%89 = load i8*, i8** %16
%90 = call i64 @strlen(i8* %89)
%91 = call i64 @write(i32 %87, i8* %88, i64 %90)
%92 = load i8*, i8** %16
%93 = call i64 @strlen(i8* %92)
%94 = icmp ne i64 %91, %93
br i1 %94, label %95, label %96
95: ; preds = %59
store i32 1, i32* %6
br label %103
96: ; preds = %59
%97 = load i32, i32* %10
%98 = load i32, i32* @SEEK_SET
%99 = call i64 @lseek(i32 %97, i32 0, i32 %98)
%100 = icmp slt i64 %99, 0
br i1 %100, label %101, label %102
101: ; preds = %96
store i32 1, i32* %6
br label %103
102: ; preds = %96
store i32 0, i32* %6
br label %103
103: ; preds = %102, %101, %95, %58, %52, %36, %29
%104 = load i32, i32* %6
ret i32 %104
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@SEEK_SET = global i32 0
define i32 @save_data_to_file(i8* %0, i64 %1, i32 %2, i32 %3, i8* nocapture %4) {
%6 = alloca %struct.stat
%7 = bitcast %struct.stat* %6 to i8*
%8 = call i32 @stat(i8* %4, %struct.stat* %6)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i8* @malloc(i32 %2)
%12 = tail call i32 @read(i32 %3, i8* %11, i32 %2)
%13 = icmp eq i32 %12, %2
br i1 %13, label %14, label %46
14: ; preds = %5
%15 = load i32, i32* @SEEK_SET
%16 = tail call i64 @lseek(i32 %3, i32 %2, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %46, label %18
18: ; preds = %14
%19 = sub nsw i32 %10, %2
%20 = tail call i8* @malloc(i32 %19)
%21 = tail call i32 @read(i32 %3, i8* %20, i32 %19)
%22 = icmp eq i32 %21, %19
br i1 %22, label %23, label %46
23: ; preds = %18
%24 = load i32, i32* @SEEK_SET
%25 = tail call i64 @lseek(i32 %3, i32 0, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %46, label %27
27: ; preds = %23
%28 = add nsw i32 %10, %2
%29 = tail call i8* @malloc(i32 %28)
%30 = tail call i32 @memcpy(i8* %29, i8* %11, i32 %2)
%31 = sext i32 %2 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = trunc i64 %1 to i32
%34 = tail call i32 @memcpy(i8* %32, i8* %0, i32 %33)
%35 = getelementptr inbounds i8, i8* %32, i64 %1
%36 = tail call i32 @memcpy(i8* %35, i8* %20, i32 %19)
%37 = tail call i64 @strlen(i8* dereferenceable(1) %29)
%38 = tail call i64 @write(i32 %3, i8* %29, i64 %37)
%39 = tail call i64 @strlen(i8* dereferenceable(1) %29)
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %27
%42 = load i32, i32* @SEEK_SET
%43 = tail call i64 @lseek(i32 %3, i32 0, i32 %42)
%44 = lshr i64 %43, 63
%45 = trunc i64 %44 to i32
br label %46
46: ; preds = %18, %23, %41, %27, %14, %5
%47 = phi i32 [ 1, %5 ], [ 1, %14 ], [ 1, %18 ], [ 1, %23 ], [ 1, %27 ], [ %45, %41 ]
ret i32 %47
}