#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EBADF;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int mkstemp(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int do_test(void) {
  char tmpl[] = "/tmp/tst-fwrite-error.XXXXXX";
  int fd = mkstemp(tmpl);
  if (fd == -1) {
    printf("mkstemp failed with errno %d\n", errno);
    return 1;
  }
  FILE *fp = fdopen(fd, "w");
  if (fp == NULL) {
    printf("fdopen failed with errno %d\n", errno);
    return 1;
  }
  char buf[] = "world";
  setvbuf(fp, NULL, _IONBF, 0);
  close(fd);
  unlink(tmpl);
  errno = 0;
  int ret = fwrite(buf, 1, sizeof(buf), fp);
  if (ret != 0) {
    printf("fwrite returned %d\n", ret);
    return 1;
  }
  if (errno != EBADF) {
    printf("Errno is not EBADF: %d\n", errno);
    return 1;
  }
  if (ferror(fp) == 0) {
    printf("ferror not set\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0xe71(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe6e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe6b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe69(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xe64(%rip),%al
mov    %al,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <mkstemp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11f4 <do_test+0x74>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132f <do_test+0x1af>
mov    -0x34(%rbp),%edi
lea    0xe3d(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1235 <do_test+0xb5>
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132f <do_test+0x1af>
mov    0xe1f(%rip),%eax
mov    %eax,-0x46(%rbp)
mov    0xe19(%rip),%ax
mov    %ax,-0x42(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1090 <setvbuf@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2d5f(%rip),%rax
movl   $0x0,(%rax)
lea    -0x46(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
mov    $0x6,%edx
call   10a0 <fwrite@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     12c0 <do_test+0x140>
mov    -0x4c(%rbp),%esi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132f <do_test+0x1af>
mov    0x2d11(%rip),%rax
mov    (%rax),%eax
mov    0x2d00(%rip),%rcx
cmp    (%rcx),%eax
je     12fb <do_test+0x17b>
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132f <do_test+0x1af>
mov    -0x40(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
jne    1328 <do_test+0x1a8>
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132f <do_test+0x1af>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
movups 0xe70(%rip),%xmm0
movups %xmm0,0x1d(%rsp)
movaps 0xe57(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rdi
call   1080 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1242 <do_test+0xb2>
mov    %eax,%ebp
lea    0xe71(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     1254 <do_test+0xc4>
mov    %rax,%rbx
movw   $0x64,0xc(%rsp)
movl   $0x6c726f77,0x8(%rsp)
6c
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %ecx,%ecx
call   10a0 <setvbuf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    0x2dc3(%rip),%rbp
movl   $0x0,0x0(%rbp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
test   %eax,%eax
je     1266 <do_test+0xd6>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
jmp    129b <do_test+0x10b>
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    0xdcb(%rip),%rdi
jmp    129b <do_test+0x10b>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0xdd9(%rip),%rdi
jmp    129b <do_test+0x10b>
mov    0x0(%rbp),%esi
mov    0x2d60(%rip),%rax
cmp    (%rax),%esi
jne    1294 <do_test+0x104>
mov    %rbx,%rdi
call   1050 <ferror@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    12a7 <do_test+0x117>
lea    0xdff(%rip),%rdi
call   1040 <puts@plt>
jmp    12a2 <do_test+0x112>
lea    0xdd9(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.do_test.tmpl = [29 x i8] c"/tmp/tst-fwrite-error.XXXXXX\00"
@.str = [30 x i8] c"mkstemp failed with errno %d\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [29 x i8] c"fdopen failed with errno %d\0A\00"
@__const.do_test.buf = [6 x i8] c"world\00"
@_IONBF = global i32 0
@.str.3 = [20 x i8] c"fwrite returned %d\0A\00"
@EBADF = global i32 0
@.str.4 = [24 x i8] c"Errno is not EBADF: %d\0A\00"
@.str.5 = [16 x i8] c"ferror not set\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca [29 x i8]
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [6 x i8]
%6 = alloca i32
%7 = bitcast [29 x i8]* %2 to i8*
%8 = getelementptr inbounds [29 x i8], [29 x i8]* %2, i64 0, i64 0
%9 = call i32 @mkstemp(i8* %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i32, i32* @errno
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 1, i32* %1
br label %54
15: ; preds = %0
%16 = load i32, i32* %3
%17 = call i32* @fdopen(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @errno
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %21)
store i32 1, i32* %1
br label %54
23: ; preds = %15
%24 = bitcast [6 x i8]* %5 to i8*
%25 = load i32*, i32** %4
%26 = load i32, i32* @_IONBF
%27 = call i32 @setvbuf(i32* %25, i32* null, i32 %26, i32 0)
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [29 x i8], [29 x i8]* %2, i64 0, i64 0
%31 = call i32 @unlink(i8* %30)
store i32 0, i32* @errno
%32 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%33 = load i32*, i32** %4
%34 = call i32 @fwrite(i8* %32, i32 1, i32 6, i32* %33)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %23
%38 = load i32, i32* %6
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %38)
store i32 1, i32* %1
br label %54
40: ; preds = %23
%41 = load i32, i32* @errno
%42 = load i32, i32* @EBADF
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %45)
store i32 1, i32* %1
br label %54
47: ; preds = %40
%48 = load i32*, i32** %4
%49 = call i64 @ferror(i32* %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %54
53: ; preds = %47
store i32 0, i32* %1
br label %54
54: ; preds = %53, %51, %44, %37, %20, %12
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@__const.do_test.tmpl = [29 x i8] c"/tmp/tst-fwrite-error.XXXXXX\00"
@.str = [30 x i8] c"mkstemp failed with errno %d\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [29 x i8] c"fdopen failed with errno %d\0A\00"
@__const.do_test.buf = [6 x i8] c"world\00"
@_IONBF = global i32 0
@.str.3 = [20 x i8] c"fwrite returned %d\0A\00"
@EBADF = global i32 0
@.str.4 = [24 x i8] c"Errno is not EBADF: %d\0A\00"
@str = [15 x i8] c"ferror not set\00"
define i32 @do_test() {
%1 = alloca [29 x i8]
%2 = alloca [6 x i8]
%3 = getelementptr inbounds [29 x i8], [29 x i8]* %1, i64 0, i64 0
%4 = call i32 @mkstemp(i8* %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %38
9: ; preds = %0
%10 = call i32* @fdopen(i32 %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %38
15: ; preds = %9
%16 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%17 = load i32, i32* @_IONBF
%18 = call i32 @setvbuf(i32* %10, i32* null, i32 %17, i32 0)
%19 = call i32 @close(i32 %4)
%20 = call i32 @unlink(i8* %3)
store i32 0, i32* @errno
%21 = call i32 @fwrite(i8* %16, i32 1, i32 6, i32* %10)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %36
25: ; preds = %15
%26 = load i32, i32* @errno
%27 = load i32, i32* @EBADF
%28 = icmp eq i32 %26, %27
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %26)
br label %36
31: ; preds = %25
%32 = call i64 @ferror(i32* %10)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %31, %34, %29, %23
%37 = phi i32 [ 1, %23 ], [ 1, %29 ], [ 1, %34 ], [ 0, %31 ]
br label %38
38: ; preds = %12, %36, %6
%39 = phi i32 [ 1, %6 ], [ 1, %12 ], [ %37, %36 ]
ret i32 %39
}