#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 */
unsigned long crc32(unsigned char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int scanf(char *, char **);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int write_block_device(char *, char **);
int xml_write_to_mtd(char **fisroot_str, int assume_yes, char **target) {
  int ret = 0;
  int len = 0;
  char *tmp_str = NULL;
  char *affirm = NULL;
  unsigned long checksum;
  if ((!*fisroot_str) || (!*target)) {
    perror("Error writing to device: Insufficient Data\n");
    return 1;
  }
  if (!assume_yes) {
    printf("Update RedBoot non-volatile configuration - continue (y/n)? ");
    ret = scanf("%a[a-z]", &affirm);
    if (ret == 1) {
      if (strncmp(affirm, "y", 1))
        return 1;
    } else {
      return 1;
    }
    ret = 0;
    if (affirm)
      free(affirm);
  };
  len = strlen(*fisroot_str);
  tmp_str = malloc(len + 6);
  strcpy(tmp_str, *fisroot_str);
  checksum = crc32((unsigned char *)*fisroot_str, len + 1);
  *(tmp_str + len + 1) = checksum & 0xFF;
  *(tmp_str + len + 2) = (checksum >> 8) & 0xFF;
  *(tmp_str + len + 3) = (checksum >> 16) & 0xFF;
  *(tmp_str + len + 4) = (checksum >> 24) & 0xFF;
  ;
  tmp_str[len + sizeof checksum + 1] = '\0';
  ;
  ret = write_block_device(*target, &tmp_str);
  free(tmp_str);
  return ret;
}


x86 assembly (clang, -O0):
<xml_write_to_mtd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11ed <xml_write_to_mtd+0x4d>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1205 <xml_write_to_mtd+0x65>
lea    0xe0c(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1373 <xml_write_to_mtd+0x1d3>
cmpl   $0x0,-0x14(%rbp)
jne    1294 <xml_write_to_mtd+0xf4>
lea    0xe16(%rip),%rdi
call   1090 <printf@plt>
lea    0xe47(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   10b0 <scanf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    1268 <xml_write_to_mtd+0xc8>
mov    -0x38(%rbp),%rdi
lea    0xe2e(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     1263 <xml_write_to_mtd+0xc3>
movl   $0x1,-0x4(%rbp)
jmp    1373 <xml_write_to_mtd+0x1d3>
jmp    1274 <xml_write_to_mtd+0xd4>
movl   $0x1,-0x4(%rbp)
jmp    1373 <xml_write_to_mtd+0x1d3>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     128f <xml_write_to_mtd+0xef>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    1294 <xml_write_to_mtd+0xf4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
add    $0x6,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1050 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%esi
add    $0x1,%esi
call   1070 <crc32@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,0x1(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
shr    $0x8,%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,0x2(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
shr    $0x10,%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,0x3(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
shr    $0x18,%rax
and    $0xff,%rax
mov    %al,%dl
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,0x4(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,0x9(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <write_block_device@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xml_write_to_mtd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
cmpq   $0x0,(%rdi)
je     129e <xml_write_to_mtd+0xfe>
mov    %rdx,%r14
cmpq   $0x0,(%rdx)
je     129e <xml_write_to_mtd+0xfe>
mov    %rdi,%r15
test   %esi,%esi
je     123f <xml_write_to_mtd+0x9f>
mov    (%r15),%rdi
call   1080 <strlen@plt>
mov    %eax,%ebx
lea    0x6(%rbx),%edi
call   10a0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,0x10(%rsp)
mov    (%r15),%rsi
mov    %rax,%rdi
call   1050 <strcpy@plt>
mov    (%r15),%rdi
lea    0x1(%rbx),%esi
call   1070 <crc32@plt>
movslq %ebx,%rcx
mov    %al,0x1(%rbp,%rcx,1)
mov    %ah,0x2(%rbp,%rcx,1)
mov    %rax,%rdx
shr    $0x10,%rdx
mov    %dl,0x3(%rbp,%rcx,1)
shr    $0x18,%rax
mov    %al,0x4(%rbp,%rcx,1)
movb   $0x0,0x9(%rbp,%rcx,1)
mov    (%r14),%rdi
lea    0x10(%rsp),%rsi
call   1060 <write_block_device@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
jmp    12af <xml_write_to_mtd+0x10f>
lea    0xde6(%rip),%rdi
call   1090 <printf@plt>
lea    0xe17(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <scanf@plt>
mov    $0x1,%ebp
cmp    $0x1,%eax
jne    12af <xml_write_to_mtd+0x10f>
mov    0x8(%rsp),%rdi
lea    0xdff(%rip),%rsi
mov    $0x1,%ebp
mov    $0x1,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    12af <xml_write_to_mtd+0x10f>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11d1 <xml_write_to_mtd+0x31>
call   1030 <free@plt>
jmp    11d1 <xml_write_to_mtd+0x31>
lea    0xd5b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"Error writing to device: Insufficient Data\0A\00"
@.str.1 = [61 x i8] c"Update RedBoot non-volatile configuration - continue (y/n)? \00"
@.str.2 = [8 x i8] c"%a[a-z]\00"
@.str.3 = [2 x i8] c"y\00"
define i32 @xml_write_to_mtd(i8** %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i32 0, i32* %8
store i32 0, i32* %9
store i8* null, i8** %10
store i8* null, i8** %11
%13 = load i8**, i8*** %5
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i8**, i8*** %7
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16, %3
%21 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %107
22: ; preds = %16
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 0
br i1 %24, label %44, label %25
25: ; preds = %22
%26 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @scanf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %11)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i8*, i8** %11
%32 = call i64 @strncmp(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 1, i32* %4
br label %107
35: ; preds = %30
br label %37
36: ; preds = %25
store i32 1, i32* %4
br label %107
37: ; preds = %35
store i32 0, i32* %8
%38 = load i8*, i8** %11
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %11
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %40, %37
br label %44
44: ; preds = %43, %22
%45 = load i8**, i8*** %5
%46 = load i8*, i8** %45
%47 = call i32 @strlen(i8* %46)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 6
%50 = call i8* @malloc(i32 %49)
store i8* %50, i8** %10
%51 = load i8*, i8** %10
%52 = load i8**, i8*** %5
%53 = load i8*, i8** %52
%54 = call i32 @strcpy(i8* %51, i8* %53)
%55 = load i8**, i8*** %5
%56 = load i8*, i8** %55
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 1
%59 = call i64 @crc32(i8* %56, i32 %58)
store i64 %59, i64* %12
%60 = load i64, i64* %12
%61 = and i64 %60, 255
%62 = trunc i64 %61 to i8
%63 = load i8*, i8** %10
%64 = load i32, i32* %9
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = getelementptr inbounds i8, i8* %66, i64 1
store i8 %62, i8* %67
%68 = load i64, i64* %12
%69 = lshr i64 %68, 8
%70 = and i64 %69, 255
%71 = trunc i64 %70 to i8
%72 = load i8*, i8** %10
%73 = load i32, i32* %9
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %72, i64 %74
%76 = getelementptr inbounds i8, i8* %75, i64 2
store i8 %71, i8* %76
%77 = load i64, i64* %12
%78 = lshr i64 %77, 16
%79 = and i64 %78, 255
%80 = trunc i64 %79 to i8
%81 = load i8*, i8** %10
%82 = load i32, i32* %9
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %81, i64 %83
%85 = getelementptr inbounds i8, i8* %84, i64 3
store i8 %80, i8* %85
%86 = load i64, i64* %12
%87 = lshr i64 %86, 24
%88 = and i64 %87, 255
%89 = trunc i64 %88 to i8
%90 = load i8*, i8** %10
%91 = load i32, i32* %9
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %90, i64 %92
%94 = getelementptr inbounds i8, i8* %93, i64 4
store i8 %89, i8* %94
%95 = load i8*, i8** %10
%96 = load i32, i32* %9
%97 = sext i32 %96 to i64
%98 = add i64 %97, 8
%99 = add i64 %98, 1
%100 = getelementptr inbounds i8, i8* %95, i64 %99
store i8 0, i8* %100
%101 = load i8**, i8*** %7
%102 = load i8*, i8** %101
%103 = call i32 @write_block_device(i8* %102, i8** %10)
store i32 %103, i32* %8
%104 = load i8*, i8** %10
%105 = call i32 @free(i8* %104)
%106 = load i32, i32* %8
store i32 %106, i32* %4
br label %107
107: ; preds = %44, %36, %34, %20
%108 = load i32, i32* %4
ret i32 %108
}

LLVM IR (-O2):
@.str = [44 x i8] c"Error writing to device: Insufficient Data\0A\00"
@.str.1 = [61 x i8] c"Update RedBoot non-volatile configuration - continue (y/n)? \00"
@.str.2 = [8 x i8] c"%a[a-z]\00"
@.str.3 = [2 x i8] c"y\00"
define i32 @xml_write_to_mtd(i8** nocapture %0, i32 %1, i8** nocapture %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = bitcast i8** %4 to i8*
%7 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%8 = load i8*, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = load i8*, i8** %2
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10, %3
%14 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %59
15: ; preds = %10
%16 = icmp eq i32 %1, 0
br i1 %16, label %17, label %30
17: ; preds = %15
%18 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @scanf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %5)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %59
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %59
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = icmp eq i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @free(i8* %26)
br label %30
30: ; preds = %25, %28, %15
%31 = load i8*, i8** %0
%32 = call i32 @strlen(i8* %31)
%33 = add nsw i32 %32, 6
%34 = call i8* @malloc(i32 %33)
store i8* %34, i8** %4
%35 = load i8*, i8** %0
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = load i8*, i8** %0
%38 = add nsw i32 %32, 1
%39 = call i64 @crc32(i8* %37, i32 %38)
%40 = trunc i64 %39 to i8
%41 = sext i32 %32 to i64
%42 = getelementptr inbounds i8, i8* %34, i64 %41
%43 = getelementptr inbounds i8, i8* %42, i64 1
store i8 %40, i8* %43
%44 = lshr i64 %39, 8
%45 = trunc i64 %44 to i8
%46 = getelementptr inbounds i8, i8* %42, i64 2
store i8 %45, i8* %46
%47 = lshr i64 %39, 16
%48 = trunc i64 %47 to i8
%49 = getelementptr inbounds i8, i8* %42, i64 3
store i8 %48, i8* %49
%50 = lshr i64 %39, 24
%51 = trunc i64 %50 to i8
%52 = getelementptr inbounds i8, i8* %42, i64 4
store i8 %51, i8* %52
%53 = add nsw i64 %41, 9
%54 = getelementptr inbounds i8, i8* %34, i64 %53
store i8 0, i8* %54
%55 = load i8*, i8** %2
%56 = call i32 @write_block_device(i8* %55, i8** %4)
%57 = load i8*, i8** %4
%58 = call i32 @free(i8* %57)
br label %59
59: ; preds = %17, %21, %30, %13
%60 = phi i32 [ %56, %30 ], [ 1, %13 ], [ 1, %21 ], [ 1, %17 ]
ret i32 %60
}