#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*/ wrbuf;
typedef int /*<<< orphan*/ rdbuf;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
scalar_t__ eslib_file_read_full(char *, char *, int, size_t *);
scalar_t__ eslib_file_write_full(char *, char *, int);
int /*<<< orphan*/ free(char *);
int getpid();
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(scalar_t__);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ unlink(char *);
int test_file_write() {
  char wrbuf[256];
  char rdbuf[256];
  size_t rdlen;
  char *largefile = NULL;
  memset(wrbuf, 0, sizeof(wrbuf));
  memset(rdbuf, 0, sizeof(rdbuf));
  snprintf(wrbuf, sizeof(wrbuf), "blah\ntest...\ntest file!\n%d\n", getpid());
  if (unlink("temporarytestfile") && errno != ENOENT)
    goto failure;
  if (eslib_file_write_full("temporarytestfile", wrbuf, sizeof(wrbuf)))
    goto failure;
  if (eslib_file_read_full("temporarytestfile", rdbuf, sizeof(rdbuf), &rdlen))
    goto failure;
  if (rdlen != sizeof(wrbuf))
    goto failure;
  if (strncmp(rdbuf, wrbuf, sizeof(wrbuf)) != 0)
    goto failure;
  largefile = malloc((1024 * 1024 * 128));
  if (largefile == NULL)
    goto failure;
  memset(largefile, 'A', (1024 * 1024 * 128));
  if (unlink("temporarytestfile"))
    goto failure;
  if (eslib_file_write_full("temporarytestfile", largefile,
                            (1024 * 1024 * 128)))
    goto failure;
  free(largefile);
  if (unlink("temporarytestfile"))
    goto failure;
  return 0;
failure:
  printf("write failed in an unexpected way\n");
  printf("%s\n", strerror(errno));
  return -1;
}


x86 assembly (clang, -O0):
<test_file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movq   $0x0,-0x220(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
lea    -0x110(%rbp),%rax
mov    %rax,-0x228(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x228(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xdea(%rip),%rdx
call   1080 <snprintf@plt>
lea    0xdfa(%rip),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
je     1250 <test_file_write+0xa0>
mov    0x2da8(%rip),%rax
mov    (%rax),%rax
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%rax
je     1250 <test_file_write+0xa0>
jmp    1391 <test_file_write+0x1e1>
lea    -0x110(%rbp),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x100,%edx
call   10c0 <eslib_file_write_full@plt>
cmp    $0x0,%rax
je     1277 <test_file_write+0xc7>
jmp    1391 <test_file_write+0x1e1>
lea    -0x210(%rbp),%rsi
lea    0xd97(%rip),%rdi
mov    $0x100,%edx
lea    -0x218(%rbp),%rcx
call   10a0 <eslib_file_read_full@plt>
cmp    $0x0,%rax
je     12a5 <test_file_write+0xf5>
jmp    1391 <test_file_write+0x1e1>
cmpq   $0x100,-0x218(%rbp)
je     12bb <test_file_write+0x10b>
jmp    1391 <test_file_write+0x1e1>
lea    -0x210(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     12e2 <test_file_write+0x132>
jmp    1391 <test_file_write+0x1e1>
mov    $0x8000000,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    1306 <test_file_write+0x156>
jmp    1391 <test_file_write+0x1e1>
mov    -0x220(%rbp),%rdi
mov    $0x41,%esi
mov    $0x8000000,%edx
call   1090 <memset@plt>
lea    0xcf9(%rip),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
je     1337 <test_file_write+0x187>
jmp    1391 <test_file_write+0x1e1>
mov    -0x220(%rbp),%rsi
lea    0xcd7(%rip),%rdi
mov    $0x8000000,%edx
call   10c0 <eslib_file_write_full@plt>
cmp    $0x0,%rax
je     135e <test_file_write+0x1ae>
jmp    1391 <test_file_write+0x1e1>
mov    -0x220(%rbp),%rdi
call   1030 <free@plt>
lea    0xcab(%rip),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
je     1385 <test_file_write+0x1d5>
jmp    1391 <test_file_write+0x1e1>
movl   $0x0,-0x4(%rbp)
jmp    13c6 <test_file_write+0x216>
lea    0xc96(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2c3a(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xc99(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_file_write>:
push   %rbx
sub    $0x210,%rsp
lea    0x110(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
xor    %eax,%eax
call   1070 <getpid@plt>
lea    0xe12(%rip),%rdx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
lea    0xe18(%rip),%rdi
call   1040 <unlink@plt>
test   %rax,%rax
je     1224 <test_file_write+0x74>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%rax
jne    1242 <test_file_write+0x92>
lea    0xdf1(%rip),%rdi
lea    0x110(%rsp),%rsi
mov    $0x100,%edx
call   10c0 <eslib_file_write_full@plt>
test   %rax,%rax
je     1273 <test_file_write+0xc3>
lea    0xde5(%rip),%rdi
call   1060 <puts@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    %rax,%rdi
call   1060 <puts@plt>
mov    $0xffffffff,%eax
add    $0x210,%rsp
pop    %rbx
ret
lea    0xda2(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    $0x100,%edx
call   10a0 <eslib_file_read_full@plt>
test   %rax,%rax
jne    1242 <test_file_write+0x92>
cmpq   $0x100,0x8(%rsp)
jne    1242 <test_file_write+0x92>
lea    0x10(%rsp),%rdi
lea    0x110(%rsp),%rsi
mov    $0x100,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    1242 <test_file_write+0x92>
mov    $0x8000000,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1242 <test_file_write+0x92>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x41,%esi
mov    $0x8000000,%edx
call   1090 <memset@plt>
lea    0xd33(%rip),%rdi
call   1040 <unlink@plt>
test   %rax,%rax
jne    1242 <test_file_write+0x92>
lea    0xd1e(%rip),%rdi
mov    %rbx,%rsi
mov    $0x8000000,%edx
call   10c0 <eslib_file_write_full@plt>
test   %rax,%rax
jne    1242 <test_file_write+0x92>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xcf9(%rip),%rdi
call   1040 <unlink@plt>
test   %rax,%rax
jne    1242 <test_file_write+0x92>
xor    %eax,%eax
jmp    126a <test_file_write+0xba>

LLVM IR (-O0):
@.str = [28 x i8] c"blah\0Atest...\0Atest file!\0A%d\0A\00"
@.str.1 = [18 x i8] c"temporarytestfile\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [35 x i8] c"write failed in an unexpected way\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @test_file_write() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [256 x i8]
%4 = alloca i64
%5 = alloca i8*
store i8* null, i8** %5
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i8 signext 0, i32 256)
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i8 signext 0, i32 256)
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%11 = call i32 (...) @getpid()
%12 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i64 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
br label %63
20: ; preds = %15, %0
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%22 = call i64 @eslib_file_write_full(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %21, i32 256)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
br label %63
25: ; preds = %20
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%27 = call i64 @eslib_file_read_full(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 256, i64* %4)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
br label %63
30: ; preds = %25
%31 = load i64, i64* %4
%32 = icmp ne i64 %31, 256
br i1 %32, label %33, label %34
33: ; preds = %30
br label %63
34: ; preds = %30
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%37 = call i64 @strncmp(i8* %35, i8* %36, i32 256)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
br label %63
40: ; preds = %34
%41 = call i8* @malloc(i32 134217728)
store i8* %41, i8** %5
%42 = load i8*, i8** %5
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %45
44: ; preds = %40
br label %63
45: ; preds = %40
%46 = load i8*, i8** %5
%47 = call i32 @memset(i8* %46, i8 signext 65, i32 134217728)
%48 = call i64 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %45
br label %63
51: ; preds = %45
%52 = load i8*, i8** %5
%53 = call i64 @eslib_file_write_full(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %52, i32 134217728)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %51
br label %63
56: ; preds = %51
%57 = load i8*, i8** %5
%58 = call i32 @free(i8* %57)
%59 = call i64 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %56
br label %63
62: ; preds = %56
store i32 0, i32* %1
br label %68
63: ; preds = %61, %55, %50, %44, %39, %33, %29, %24, %19
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%65 = load i64, i64* @errno
%66 = call i8* @strerror(i64 %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %66)
store i32 -1, i32* %1
br label %68
68: ; preds = %63, %62
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
@.str = [28 x i8] c"blah\0Atest...\0Atest file!\0A%d\0A\00"
@.str.1 = [18 x i8] c"temporarytestfile\00"
@errno = global i64 0
@ENOENT = global i64 0
@str = [34 x i8] c"write failed in an unexpected way\00"
define i32 @test_file_write() {
%1 = alloca [256 x i8]
%2 = alloca [256 x i8]
%3 = alloca i64
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = bitcast i64* %3 to i8*
%7 = call i32 @memset(i8* %4, i8 signext 0, i32 256)
%8 = call i32 @memset(i8* %5, i8 signext 0, i32 256)
%9 = call i32 (...) @getpid()
%10 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i64 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %0
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %43
17: ; preds = %13, %0
%18 = call i64 @eslib_file_write_full(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 256)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %43
20: ; preds = %17
%21 = call i64 @eslib_file_read_full(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 256, i64* %3)
%22 = icmp ne i64 %21, 0
%23 = load i64, i64* %3
%24 = icmp ne i64 %23, 256
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %43, label %26
26: ; preds = %20
%27 = call i64 @strncmp(i8* %5, i8* %4, i32 256)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %26
%30 = call dereferenceable_or_null(134217728) i8* @malloc(i32 134217728)
%31 = icmp eq i8* %30, null
br i1 %31, label %43, label %32
32: ; preds = %29
%33 = call i32 @memset(i8* %30, i8 signext 65, i32 134217728)
%34 = call i64 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = call i64 @eslib_file_write_full(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %30, i32 134217728)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = call i32 @free(i8* %30)
%41 = call i64 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %48, label %43
43: ; preds = %39, %36, %32, %29, %26, %20, %17, %13
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%45 = load i64, i64* @errno
%46 = call i8* @strerror(i64 %45)
%47 = call i32 @puts(i8* dereferenceable(1) %46)
br label %48
48: ; preds = %39, %43
%49 = phi i32 [ -1, %43 ], [ 0, %39 ]
ret i32 %49
}