#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
unsigned int fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
unsigned int fwrite(char *, int, unsigned int, int /*<<< orphan*/ *);
char *malloc(unsigned int);
scalar_t__ memcmp(char *, char *, unsigned int);
int /*<<< orphan*/ stderr;
unsigned int strlen(char *);
int write_if_change(char *outbuf, char *filename) {
  FILE *out;
  unsigned int end;
  char *cmpbuf;
  unsigned int stat;
  out = fopen(filename, "rb");
  if (out == NULL) {
    out = fopen(filename, "wb");
    if (out == NULL) {
      fprintf(stderr, "Unable to create output file\n");
      return (1);
    }
    fputs(outbuf, out);
    fclose(out);
    return (0);
  }
  fseek(out, 0, SEEK_END);
  end = ftell(out);
  cmpbuf = malloc(end);
  if (cmpbuf == NULL) {
    fprintf(stderr, "Out of memory\n");
    fclose(out);
    return (1);
  }
  fseek(out, 0, SEEK_SET);
  stat = fread(cmpbuf, 1, end, out);
  if (stat != end) {
    fprintf(stderr, "Failed to read data\n");
    fclose(out);
    free(cmpbuf);
    return (1);
  }
  if (end == strlen(outbuf) && memcmp(cmpbuf, outbuf, end) == 0) {
    fclose(out);
    free(cmpbuf);
    return (0);
  }
  fclose(out);
  free(cmpbuf);
  out = fopen(filename, "wb");
  if (out == NULL) {
    fprintf(stderr, "Unable to create output file\n");
    return (1);
  }
  stat = fwrite(outbuf, 1, strlen(outbuf), out);
  if (strlen(outbuf) != stat) {
    fprintf(stderr, "Unable to write output file\n");
    fclose(out);
    return (1);
  }
  fclose(out);
  return (0);
}


x86 assembly (clang, -O0):
<write_if_change>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1251 <write_if_change+0x91>
mov    -0x18(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122f <write_if_change+0x6f>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1437 <write_if_change+0x277>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <fputs@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1437 <write_if_change+0x277>
mov    -0x20(%rbp),%rdi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b2 <write_if_change+0xf2>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1437 <write_if_change+0x277>
mov    -0x20(%rbp),%rdi
mov    0x2d13(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     131d <write_if_change+0x15d>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd39(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1437 <write_if_change+0x277>
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jne    1371 <write_if_change+0x1b1>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    1371 <write_if_change+0x1b1>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1437 <write_if_change+0x277>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
lea    0xc75(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    13c3 <write_if_change+0x203>
mov    0x2c2f(%rip),%rax
mov    (%rax),%edi
lea    0xc54(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1437 <write_if_change+0x277>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cmp    -0x34(%rbp),%eax
je     1427 <write_if_change+0x267>
mov    0x2bd4(%rip),%rax
mov    (%rax),%edi
lea    0xc3b(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1437 <write_if_change+0x277>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_if_change>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe28(%rip),%rsi
mov    %r12,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12ec <write_if_change+0x12c>
mov    %rax,%rbx
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     131b <write_if_change+0x15b>
mov    %rax,%r13
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    $0x1,%r15d
mov    %r13,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %ebp,%eax
jne    1340 <write_if_change+0x180>
mov    %r14,%rdi
call   1060 <strlen@plt>
cmp    %eax,%ebp
jne    1271 <write_if_change+0xb1>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
je     13ab <write_if_change+0x1eb>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1030 <free@plt>
lea    0xd7b(%rip),%rsi
mov    %r12,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1384 <write_if_change+0x1c4>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    $0x1,%r15d
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <strlen@plt>
cmp    %ebp,%eax
je     130e <write_if_change+0x14e>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1399 <write_if_change+0x1d9>
lea    0xd10(%rip),%rsi
mov    %r12,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1367 <write_if_change+0x1a7>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %r15d,%r15d
jmp    1399 <write_if_change+0x1d9>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
lea    0xcf9(%rip),%rsi
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x1,%r15d
jmp    1399 <write_if_change+0x1d9>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
lea    0xce3(%rip),%rsi
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1399 <write_if_change+0x1d9>
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
lea    0xc8f(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%r15d
jmp    1399 <write_if_change+0x1d9>
mov    0x2c4d(%rip),%rax
mov    (%rax),%edi
lea    0xc72(%rip),%rsi
call   1090 <fprintf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1316 <write_if_change+0x156>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"Unable to create output file\0A\00"
@SEEK_END = global i32 0
@.str.3 = [15 x i8] c"Out of memory\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [21 x i8] c"Failed to read data\0A\00"
@.str.5 = [29 x i8] c"Unable to write output file\0A\00"
define i32 @write_if_change(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %28
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %107
22: ; preds = %14
%23 = load i8*, i8** %4
%24 = load i32*, i32** %6
%25 = call i32 @fputs(i8* %23, i32* %24)
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
store i32 0, i32* %3
br label %107
28: ; preds = %2
%29 = load i32*, i32** %6
%30 = load i32, i32* @SEEK_END
%31 = call i32 @fseek(i32* %29, i32 0, i32 %30)
%32 = load i32*, i32** %6
%33 = call i32 @ftell(i32* %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** %8
%36 = load i8*, i8** %8
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %43
38: ; preds = %28
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
store i32 1, i32* %3
br label %107
43: ; preds = %28
%44 = load i32*, i32** %6
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %44, i32 0, i32 %45)
%47 = load i8*, i8** %8
%48 = load i32, i32* %7
%49 = load i32*, i32** %6
%50 = call i32 @fread(i8* %47, i32 1, i32 %48, i32* %49)
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = load i32, i32* %7
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %61
54: ; preds = %43
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
%59 = load i8*, i8** %8
%60 = call i32 @free(i8* %59)
store i32 1, i32* %3
br label %107
61: ; preds = %43
%62 = load i32, i32* %7
%63 = load i8*, i8** %4
%64 = call i32 @strlen(i8* %63)
%65 = icmp eq i32 %62, %64
br i1 %65, label %66, label %77
66: ; preds = %61
%67 = load i8*, i8** %8
%68 = load i8*, i8** %4
%69 = load i32, i32* %7
%70 = call i64 @memcmp(i8* %67, i8* %68, i32 %69)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %66
%73 = load i32*, i32** %6
%74 = call i32 @fclose(i32* %73)
%75 = load i8*, i8** %8
%76 = call i32 @free(i8* %75)
store i32 0, i32* %3
br label %107
77: ; preds = %66, %61
%78 = load i32*, i32** %6
%79 = call i32 @fclose(i32* %78)
%80 = load i8*, i8** %8
%81 = call i32 @free(i8* %80)
%82 = load i8*, i8** %5
%83 = call i32* @fopen(i8* %82, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %83, i32** %6
%84 = load i32*, i32** %6
%85 = icmp eq i32* %84, null
br i1 %85, label %86, label %89
86: ; preds = %77
%87 = load i32, i32* @stderr
%88 = call i32 @fprintf(i32 %87, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %107
89: ; preds = %77
%90 = load i8*, i8** %4
%91 = load i8*, i8** %4
%92 = call i32 @strlen(i8* %91)
%93 = load i32*, i32** %6
%94 = call i32 @fwrite(i8* %90, i32 1, i32 %92, i32* %93)
store i32 %94, i32* %9
%95 = load i8*, i8** %4
%96 = call i32 @strlen(i8* %95)
%97 = load i32, i32* %9
%98 = icmp ne i32 %96, %97
br i1 %98, label %99, label %104
99: ; preds = %89
%100 = load i32, i32* @stderr
%101 = call i32 @fprintf(i32 %100, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%102 = load i32*, i32** %6
%103 = call i32 @fclose(i32* %102)
store i32 1, i32* %3
br label %107
104: ; preds = %89
%105 = load i32*, i32** %6
%106 = call i32 @fclose(i32* %105)
store i32 0, i32* %3
br label %107
107: ; preds = %104, %99, %86, %72, %54, %38, %22, %19
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"Unable to create output file\0A\00"
@SEEK_END = global i32 0
@.str.3 = [15 x i8] c"Out of memory\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [21 x i8] c"Failed to read data\0A\00"
@.str.5 = [29 x i8] c"Unable to write output file\0A\00"
define i32 @write_if_change(i8* %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %14
5: ; preds = %2
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %62
11: ; preds = %5
%12 = tail call i32 @fputs(i8* %0, i32* %6)
%13 = tail call i32 @fclose(i32* %6)
br label %62
14: ; preds = %2
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @fseek(i32* %3, i32 0, i32 %15)
%17 = tail call i32 @ftell(i32* %3)
%18 = tail call i8* @malloc(i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @fclose(i32* %3)
br label %62
24: ; preds = %14
%25 = load i32, i32* @SEEK_SET
%26 = tail call i32 @fseek(i32* %3, i32 0, i32 %25)
%27 = tail call i32 @fread(i8* %18, i32 1, i32 %17, i32* %3)
%28 = icmp eq i32 %27, %17
br i1 %28, label %34, label %29
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%32 = tail call i32 @fclose(i32* %3)
%33 = tail call i32 @free(i8* %18)
br label %62
34: ; preds = %24
%35 = tail call i32 @strlen(i8* %0)
%36 = icmp eq i32 %17, %35
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = tail call i64 @memcmp(i8* %18, i8* %0, i32 %17)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = tail call i32 @fclose(i32* %3)
%42 = tail call i32 @free(i8* %18)
br label %62
43: ; preds = %37, %34
%44 = tail call i32 @fclose(i32* %3)
%45 = tail call i32 @free(i8* %18)
%46 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = tail call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %62
51: ; preds = %43
%52 = tail call i32 @strlen(i8* %0)
%53 = tail call i32 @fwrite(i8* %0, i32 1, i32 %52, i32* %46)
%54 = tail call i32 @strlen(i8* %0)
%55 = icmp eq i32 %54, %53
br i1 %55, label %60, label %56
56: ; preds = %51
%57 = load i32, i32* @stderr
%58 = tail call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%59 = tail call i32 @fclose(i32* %46)
br label %62
60: ; preds = %51
%61 = tail call i32 @fclose(i32* %46)
br label %62
62: ; preds = %60, %56, %48, %40, %29, %20, %11, %8
%63 = phi i32 [ 1, %8 ], [ 0, %11 ], [ 1, %20 ], [ 1, %29 ], [ 0, %40 ], [ 1, %48 ], [ 1, %56 ], [ 0, %60 ]
ret i32 %63
}