#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int write_get_nvram_script(char *name, char *shell_dir, char *val_dir) {
  FILE *fp = NULL;
  char contents[512] = {0};
  sprintf(contents,
          "#! /bin/sh\n"
          "NV=`nvram get %s`\n"
          "if [ ! -f \"%s\" ]; then\n"
          "touch %s\n"
          "fi\n"
          "echo \"$NV\" >%s",
          name, val_dir, val_dir, val_dir);
  if ((fp = fopen(shell_dir, "w")) == NULL) {
    fprintf(stderr, "create ftpclient_get_nvram file error\n");
    return -1;
  }
  fprintf(fp, "%s", contents);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<write_get_nvram_script>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x20(%rbp),%r9
lea    0xe60(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11e7 <write_get_nvram_script+0x97>
mov    0x2e16(%rip),%rax
movslq (%rax),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <write_get_nvram_script+0xc0>
mov    -0x28(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_get_nvram_script>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rsp,%r12
xor    %r14d,%r14d
mov    $0x200,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe6c(%rip),%rsi
mov    %r12,%rdi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %rbx,%r8
mov    %rbx,%r9
call   1070 <sprintf@plt>
lea    0xea0(%rip),%rsi
mov    %r15,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d4 <write_get_nvram_script+0x74>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    %rax,%rsi
call   1040 <fputs@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11fa <write_get_nvram_script+0x9a>
mov    0x2e05(%rip),%rax
movslq (%rax),%rcx
lea    0xe6c(%rip),%rdi
mov    $0x26,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [79 x i8] c"#! /bin/sh\0ANV=`nvram get %s`\0Aif [ ! -f \22%s\22 ]; then\0Atouch %s\0Afi\0Aecho \22$NV\22 >%s\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"create ftpclient_get_nvram file error\0A\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @write_get_nvram_script(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [512 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32* null, i32** %8
%10 = bitcast [512 x i8]* %9 to i8*
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%12 = load i8*, i8** %5
%13 = load i8*, i8** %7
%14 = load i8*, i8** %7
%15 = load i8*, i8** %7
%16 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %13, i8* %14, i8* %15)
%17 = load i8*, i8** %6
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i32*
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %31
25: ; preds = %3
%26 = load i32*, i32** %8
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
store i32 0, i32* %4
br label %31
31: ; preds = %25, %20
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [79 x i8] c"#! /bin/sh\0ANV=`nvram get %s`\0Aif [ ! -f \22%s\22 ]; then\0Atouch %s\0Afi\0Aecho \22$NV\22 >%s\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"create ftpclient_get_nvram file error\0A\00"
define i32 @write_get_nvram_script(i8* %0, i8* nocapture %1, i8* %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %2, i8* %2, i8* %2)
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i32*
%13 = tail call i64 @fwrite(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i64 38, i64 1, i32* %12)
br label %17
14: ; preds = %3
%15 = call i32 @fputs(i8* %5, i32* %7)
%16 = tail call i32 @fclose(i32* %7)
br label %17
17: ; preds = %14, %9
%18 = phi i32 [ -1, %9 ], [ 0, %14 ]
ret i32 %18
}