#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 /*<<< orphan*/ F_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sprintf(char *, char *, int);
int strlen(char *);
int edit_file(int cpu, char *path, char *text) {
  if (path == NULL || text == NULL) {
    printf("Invalid argument parsed to edit_file, please report this error.\n");
    return 6;
  }
  if (strlen(path) < 1 || strlen(text) < 1) {
    printf("Invalid argument parsed to edit_file, please report this error.\n");
    return 6;
  }
  char *new_path = (char *)malloc(sizeof(char) * (strlen(path) + 3));
  if (sprintf(new_path, path, cpu) < 0) {
    printf("Unable to format string '%s'\n", path);
    free(new_path);
    return 3;
  }
  if (access(new_path, F_OK) != -1) {
    FILE *buf = fopen(new_path, "w");
    if (buf == NULL) {
      printf("Unable to open '%s' for writing!\n", new_path);
      free(new_path);
      return 4;
    }
    if (fprintf(buf, "%s", text) < 0) {
      printf("Unable to write data (%s) into '%s'\n", text, new_path);
      fclose(buf);
      free(new_path);
      return 5;
    }
    fclose(buf);
    free(new_path);
    return 0;
  } else {
    printf("%s: File not found!\n", new_path);
    return 7;
  }
}


x86 assembly (clang, -O0):
<edit_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b9 <edit_file+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    11d3 <edit_file+0x43>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x6,-0x4(%rbp)
jmp    1359 <edit_file+0x1c9>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jl     11f7 <edit_file+0x67>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jge    1211 <edit_file+0x81>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x6,-0x4(%rbp)
jmp    1359 <edit_file+0x1c9>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x3,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   10b0 <sprintf@plt>
cmp    $0x0,%rax
jge    126f <edit_file+0xdf>
mov    -0x10(%rbp),%rsi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x3,-0x4(%rbp)
jmp    1359 <edit_file+0x1c9>
mov    -0x20(%rbp),%rdi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     1340 <edit_file+0x1b0>
mov    -0x20(%rbp),%rdi
lea    0xdca(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d0 <edit_file+0x140>
mov    -0x20(%rbp),%rsi
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x4,-0x4(%rbp)
jmp    1359 <edit_file+0x1c9>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xda4(%rip),%rsi
call   1070 <fprintf@plt>
cmp    $0x0,%rax
jge    1322 <edit_file+0x192>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x5,-0x4(%rbp)
jmp    1359 <edit_file+0x1c9>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1359 <edit_file+0x1c9>
mov    -0x20(%rbp),%rsi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x7,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<edit_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     126e <edit_file+0xce>
mov    %rdx,%r15
test   %rdx,%rdx
je     126e <edit_file+0xce>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    126e <edit_file+0xce>
mov    %r15,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    126e <edit_file+0xce>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x3(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   10c0 <sprintf@plt>
test   %rax,%rax
js     128a <edit_file+0xea>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
je     12aa <edit_file+0x10a>
lea    0xdf3(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12c2 <edit_file+0x122>
mov    %rax,%rbx
lea    0xdfc(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1080 <fprintf@plt>
test   %rax,%rax
js     12e2 <edit_file+0x142>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    127f <edit_file+0xdf>
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x6,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x3,%eax
jmp    127f <edit_file+0xdf>
lea    0xdb9(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x7,%eax
jmp    127f <edit_file+0xdf>
lea    0xd57(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x4,%eax
jmp    127f <edit_file+0xdf>
lea    0xd5c(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x5,%eax
jmp    127f <edit_file+0xdf>

LLVM IR (-O0):
@.str = [65 x i8] c"Invalid argument parsed to edit_file, please report this error.\0A\00"
@.str.1 = [30 x i8] c"Unable to format string '%s'\0A\00"
@F_OK = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [34 x i8] c"Unable to open '%s' for writing!\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [37 x i8] c"Unable to write data (%s) into '%s'\0A\00"
@.str.6 = [21 x i8] c"%s: File not found!\0A\00"
define i32 @edit_file(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i8*, i8** %7
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12, %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0))
store i32 6, i32* %4
br label %82
17: ; preds = %12
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
%20 = icmp slt i32 %19, 1
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i8*, i8** %7
%23 = call i32 @strlen(i8* %22)
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %21, %17
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0))
store i32 6, i32* %4
br label %82
27: ; preds = %21
%28 = load i8*, i8** %6
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %29, 3
%31 = sext i32 %30 to i64
%32 = mul i64 1, %31
%33 = trunc i64 %32 to i32
%34 = call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %8
%36 = load i8*, i8** %8
%37 = load i8*, i8** %6
%38 = load i32, i32* %5
%39 = call i64 @sprintf(i8* %36, i8* %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %27
%42 = load i8*, i8** %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = load i8*, i8** %8
%45 = call i32 @free(i8* %44)
store i32 3, i32* %4
br label %82
46: ; preds = %27
%47 = load i8*, i8** %8
%48 = load i32, i32* @F_OK
%49 = call i32 @access(i8* %47, i32 %48)
%50 = icmp ne i32 %49, -1
br i1 %50, label %51, label %79
51: ; preds = %46
%52 = load i8*, i8** %8
%53 = call i32* @fopen(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %53, i32** %9
%54 = load i32*, i32** %9
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load i8*, i8** %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = load i8*, i8** %8
%60 = call i32 @free(i8* %59)
store i32 4, i32* %4
br label %82
61: ; preds = %51
%62 = load i32*, i32** %9
%63 = load i8*, i8** %7
%64 = call i64 @fprintf(i32* %62, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %74
66: ; preds = %61
%67 = load i8*, i8** %7
%68 = load i8*, i8** %8
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %67, i8* %68)
%70 = load i32*, i32** %9
%71 = call i32 @fclose(i32* %70)
%72 = load i8*, i8** %8
%73 = call i32 @free(i8* %72)
store i32 5, i32* %4
br label %82
74: ; preds = %61
%75 = load i32*, i32** %9
%76 = call i32 @fclose(i32* %75)
%77 = load i8*, i8** %8
%78 = call i32 @free(i8* %77)
store i32 0, i32* %4
br label %82
79: ; preds = %46
%80 = load i8*, i8** %8
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %80)
store i32 7, i32* %4
br label %82
82: ; preds = %79, %74, %66, %56, %41, %25, %15
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
@.str.1 = [30 x i8] c"Unable to format string '%s'\0A\00"
@F_OK = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [34 x i8] c"Unable to open '%s' for writing!\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [37 x i8] c"Unable to write data (%s) into '%s'\0A\00"
@.str.6 = [21 x i8] c"%s: File not found!\0A\00"
@str.7 = [64 x i8] c"Invalid argument parsed to edit_file, please report this error.\00"
define i32 @edit_file(i32 %0, i8* %1, i8* %2) {
%4 = icmp eq i8* %1, null
%5 = icmp eq i8* %2, null
%6 = or i1 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str.7, i64 0, i64 0))
br label %49
9: ; preds = %3
%10 = tail call i32 @strlen(i8* %1)
%11 = icmp slt i32 %10, 1
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i32 @strlen(i8* %2)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %12, %9
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str.7, i64 0, i64 0))
br label %49
17: ; preds = %12
%18 = tail call i32 @strlen(i8* %1)
%19 = add nsw i32 %18, 3
%20 = tail call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
%22 = tail call i64 @sprintf(i8* %21, i8* %1, i32 %0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%26 = tail call i32 @free(i8* %21)
br label %49
27: ; preds = %17
%28 = load i32, i32* @F_OK
%29 = tail call i32 @access(i8* %21, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %47, label %31
31: ; preds = %27
%32 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%36 = tail call i32 @free(i8* %21)
br label %49
37: ; preds = %31
%38 = tail call i64 @fprintf(i32* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %2, i8* %21)
%42 = tail call i32 @fclose(i32* %32)
%43 = tail call i32 @free(i8* %21)
br label %49
44: ; preds = %37
%45 = tail call i32 @fclose(i32* %32)
%46 = tail call i32 @free(i8* %21)
br label %49
47: ; preds = %27
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %21)
br label %49
49: ; preds = %24, %47, %44, %40, %34, %15, %7
%50 = phi i32 [ 6, %7 ], [ 6, %15 ], [ 3, %24 ], [ 7, %47 ], [ 4, %34 ], [ 5, %40 ], [ 0, %44 ]
ret i32 %50
}