#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*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int create_shell_file() {
  printf("create shell file\n");
  FILE *fp = NULL;
  char contents[256] = {0};
  strcpy(contents, "#! /bin/sh\n"
                   "nvram set $2=\"$1\"\n"
                   "nvram commit");
  if ((fp = fopen("/tmp/smartsync/sambaclient/script/write_nvram", "w")) ==
      NULL) {
    fprintf(stderr, "create shell file error");
    return -1;
  }
  fprintf(fp, "%s", contents);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<create_shell_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
lea    0xe8e(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x110(%rbp),%rdi
lea    0xe73(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xe91(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ea <create_shell_file+0x8a>
mov    0x2e13(%rip),%rax
movslq (%rax),%rdi
lea    0xe96(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <create_shell_file+0xb3>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rdx
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_shell_file>:
push   %rbx
sub    $0x100,%rsp
lea    0xf03(%rip),%rdi
call   1040 <puts@plt>
xorps  %xmm0,%xmm0
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)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe1b(%rip),%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
lea    0xe36(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     121f <create_shell_file+0xbf>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    1244 <create_shell_file+0xe4>
mov    0x2dba(%rip),%rax
movslq (%rax),%rcx
lea    0xe2a(%rip),%rdi
mov    $0x17,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"create shell file\0A\00"
@.str.1 = [42 x i8] c"#! /bin/sh\0Anvram set $2=\22$1\22\0Anvram commit\00"
@.str.2 = [46 x i8] c"/tmp/smartsync/sambaclient/script/write_nvram\00"
@.str.3 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.4 = [24 x i8] c"create shell file error\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @create_shell_file() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %2
%5 = bitcast [256 x i8]* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32* @fopen(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %8, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i32*
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %21
15: ; preds = %0
%16 = load i32*, i32** %2
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%18 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %17)
%19 = load i32*, i32** %2
%20 = call i32 @fclose(i32* %19)
store i32 0, i32* %1
br label %21
21: ; preds = %15, %10
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str.1 = [42 x i8] c"#! /bin/sh\0Anvram set $2=\22$1\22\0Anvram commit\00"
@.str.2 = [46 x i8] c"/tmp/smartsync/sambaclient/script/write_nvram\00"
@.str.3 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.4 = [24 x i8] c"create shell file error\00"
@str = [18 x i8] c"create shell file\00"
define i32 @create_shell_file() {
%1 = alloca [256 x i8]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32* @fopen(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i32*
%11 = call i64 @fwrite(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i64 23, i64 1, i32* %10)
br label %15
12: ; preds = %0
%13 = call i32 @fputs(i8* %3, i32* %5)
%14 = call i32 @fclose(i32* %5)
br label %15
15: ; preds = %12, %7
%16 = phi i32 [ -1, %7 ], [ 0, %12 ]
ret i32 %16
}