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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int fs_writecontents(char *, char const *, int const, int const);
char *malloc(size_t);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
unsigned int strlen(char const *);
bool fs_writecontents_safe(const char *dir, const char *fn, const char *cnt,
                           const int len, const int mode) {
  char *buf = NULL, *ptr = NULL;
  bool res;
  unsigned int dlen, reqlen;
  dlen = strlen(dir) + 1;
  reqlen = dlen + strlen(fn) + 1;
  if (NULL == (ptr = malloc((size_t)reqlen)))
    return 1;
  buf = ptr;
  strcpy(ptr, dir);
  *(ptr + dlen - 1) = '/';
  *(ptr + dlen) = '\0';
  strcat(ptr, fn);
  for (ptr = buf + dlen; '\0' != *ptr; ptr++)
    if ('/' == *ptr)
      *ptr = '_';
  res = fs_writecontents(buf, cnt, len, mode);
  free(buf);
  return res;
}


x86 assembly (clang, -O0):
<fs_writecontents_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11de <fs_writecontents_safe+0x7e>
movl   $0x1,-0x4(%rbp)
jmp    128c <fs_writecontents_safe+0x12c>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%ecx
movb   $0x2f,-0x1(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
je     1267 <fs_writecontents_safe+0x107>
mov    -0x38(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x2f,%eax
cmp    %ecx,%eax
jne    1251 <fs_writecontents_safe+0xf1>
mov    -0x38(%rbp),%rax
movb   $0x5f,(%rax)
jmp    1256 <fs_writecontents_safe+0xf6>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1225 <fs_writecontents_safe+0xc5>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
call   1070 <fs_writecontents@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_writecontents_safe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,0x4(%rsp)
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %r13,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r14,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1202 <fs_writecontents_safe+0xa2>
mov    %rax,%rbp
add    $0x1,%r14d
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r14d,%eax
mov    %rbp,%rbx
add    %rax,%rbx
movw   $0x2f,-0x1(%rbp,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
call   1080 <strcat@plt>
jmp    11d7 <fs_writecontents_safe+0x77>
nopl   0x0(%rax)
movb   $0x5f,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
je     11d0 <fs_writecontents_safe+0x70>
test   %al,%al
jne    11d3 <fs_writecontents_safe+0x73>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    0x4(%rsp),%ecx
call   1070 <fs_writecontents@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    1207 <fs_writecontents_safe+0xa7>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fs_writecontents_safe(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i8* null, i8** %12
store i8* null, i8** %13
%17 = load i8*, i8** %7
%18 = call i32 @strlen(i8* %17)
%19 = add i32 %18, 1
store i32 %19, i32* %15
%20 = load i32, i32* %15
%21 = load i8*, i8** %8
%22 = call i32 @strlen(i8* %21)
%23 = add i32 %20, %22
%24 = add i32 %23, 1
store i32 %24, i32* %16
%25 = load i32, i32* %16
%26 = zext i32 %25 to i64
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %13
%28 = icmp eq i8* null, %27
br i1 %28, label %29, label %30
29: ; preds = %5
store i32 1, i32* %6
br label %76
30: ; preds = %5
%31 = load i8*, i8** %13
store i8* %31, i8** %12
%32 = load i8*, i8** %13
%33 = load i8*, i8** %7
%34 = call i32 @strcpy(i8* %32, i8* %33)
%35 = load i8*, i8** %13
%36 = load i32, i32* %15
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = getelementptr inbounds i8, i8* %38, i64 -1
store i8 47, i8* %39
%40 = load i8*, i8** %13
%41 = load i32, i32* %15
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %13
%45 = load i8*, i8** %8
%46 = call i32 @strcat(i8* %44, i8* %45)
%47 = load i8*, i8** %12
%48 = load i32, i32* %15
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8* %50, i8** %13
br label %51
51: ; preds = %64, %30
%52 = load i8*, i8** %13
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 0, %54
br i1 %55, label %56, label %67
56: ; preds = %51
%57 = load i8*, i8** %13
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 47, %59
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = load i8*, i8** %13
store i8 95, i8* %62
br label %63
63: ; preds = %61, %56
br label %64
64: ; preds = %63
%65 = load i8*, i8** %13
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %13
br label %51
67: ; preds = %51
%68 = load i8*, i8** %12
%69 = load i8*, i8** %9
%70 = load i32, i32* %10
%71 = load i32, i32* %11
%72 = call i32 @fs_writecontents(i8* %68, i8* %69, i32 %70, i32 %71)
store i32 %72, i32* %14
%73 = load i8*, i8** %12
%74 = call i32 @free(i8* %73)
%75 = load i32, i32* %14
store i32 %75, i32* %6
br label %76
76: ; preds = %67, %29
%77 = load i32, i32* %6
ret i32 %77
}

LLVM IR (-O2):
define i32 @fs_writecontents_safe(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = tail call i32 @strlen(i8* %0)
%7 = add i32 %6, 1
%8 = tail call i32 @strlen(i8* %1)
%9 = add i32 %8, 1
%10 = add i32 %9, %7
%11 = zext i32 %10 to i64
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %29, label %14
14: ; preds = %5
%15 = tail call i32 @strcpy(i8* %12, i8* %0)
%16 = zext i32 %7 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = getelementptr inbounds i8, i8* %17, i64 -1
store i8 47, i8* %18
store i8 0, i8* %17
%19 = tail call i32 @strcat(i8* %12, i8* %1)
br label %20
20: ; preds = %24, %14
%21 = phi i8* [ %17, %14 ], [ %25, %24 ]
%22 = load i8, i8* %21
switch i8 %22, label %24 [
i8 0, label %26
i8 47, label %23
]
23: ; preds = %20
store i8 95, i8* %21
br label %24
24: ; preds = %20, %23
%25 = getelementptr inbounds i8, i8* %21, i64 1
br label %20
26: ; preds = %20
%27 = tail call i32 @fs_writecontents(i8* %12, i8* %2, i32 %3, i32 %4)
%28 = tail call i32 @free(i8* %12)
br label %29
29: ; preds = %5, %26
%30 = phi i32 [ %27, %26 ], [ 1, %5 ]
ret i32 %30
}