#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*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ *fdopen(int, char *);
int fprintf(int /*<<< orphan*/ *, char *, ...);
int mkstemp(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setlinebuf(int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(char *);
int do_test(void) {
  char tmpl[] = "/tmp/tst-put-error.XXXXXX";
  int fd = mkstemp(tmpl);
  if (fd == -1)
    error(EXIT_FAILURE, errno, "cannot create temporary file");
  FILE *fp = fdopen(fd, "w");
  if (fp == NULL)
    error(EXIT_FAILURE, errno, "fdopen");
  setlinebuf(fp);
  close(fd);
  unlink(tmpl);
  int n = fprintf(fp, "hello world\n");
  printf("fprintf = %d\n", n);
  if (n >= 0)
    error(EXIT_FAILURE, 0, "first fprintf succeeded");
  n = fprintf(fp, "hello world\n");
  printf("fprintf = %d\n", n);
  if (n >= 0)
    error(EXIT_FAILURE, 0, "second fprintf succeeded");
  n = fprintf(fp, "%100s\n", "foo");
  printf("fprintf = %d\n", n);
  if (n >= 0)
    error(EXIT_FAILURE, 0, "padded fprintf succeeded");
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xe71(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe6e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe6b(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe68(%rip),%ax
mov    %ax,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11e8 <do_test+0x68>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdx
call   10a0 <error@plt>
mov    -0x24(%rbp),%edi
lea    0xe45(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1224 <do_test+0xa4>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdx
call   10a0 <error@plt>
mov    -0x30(%rbp),%rdi
call   1050 <setlinebuf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
lea    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x30(%rbp),%rdi
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xdf0(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
jl     1283 <do_test+0x103>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    0xddd(%rip),%rdx
call   10a0 <error@plt>
mov    -0x30(%rbp),%rdi
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xdab(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
jl     12c8 <do_test+0x148>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    0xdb0(%rip),%rdx
call   10a0 <error@plt>
mov    -0x30(%rbp),%rdi
lea    0xdb9(%rip),%rsi
lea    0xdb9(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd5f(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
jl     1314 <do_test+0x194>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    0xd88(%rip),%rdx
call   10a0 <error@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movups 0xe7d(%rip),%xmm0
movups %xmm0,0xa(%rsp)
movaps 0xe67(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
call   1080 <mkstemp@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ca <do_test+0x4a>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdx
call   10a0 <error@plt>
lea    0xe66(%rip),%rsi
mov    %ebp,%edi
call   1090 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11fe <do_test+0x7e>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe40(%rip),%rdx
call   10a0 <error@plt>
mov    %rbx,%rdi
call   1050 <setlinebuf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
lea    0xe24(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %eax,%ebp
lea    0xe1e(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebp,%ebp
js     1251 <do_test+0xd1>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rdx
xor    %esi,%esi
call   10a0 <error@plt>
lea    0xde8(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %eax,%ebp
lea    0xde2(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebp,%ebp
js     128d <do_test+0x10d>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rdx
xor    %esi,%esi
call   10a0 <error@plt>
lea    0xdf8(%rip),%rsi
lea    0xdf8(%rip),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %eax,%ebx
lea    0xd9f(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebx,%ebx
js     12d0 <do_test+0x150>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rdx
xor    %esi,%esi
call   10a0 <error@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.do_test.tmpl = [26 x i8] c"/tmp/tst-put-error.XXXXXX\00"
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [29 x i8] c"cannot create temporary file\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"fdopen\00"
@.str.3 = [13 x i8] c"hello world\0A\00"
@.str.4 = [14 x i8] c"fprintf = %d\0A\00"
@.str.5 = [24 x i8] c"first fprintf succeeded\00"
@.str.6 = [25 x i8] c"second fprintf succeeded\00"
@.str.7 = [7 x i8] c"%100s\0A\00"
@.str.8 = [4 x i8] c"foo\00"
@.str.9 = [25 x i8] c"padded fprintf succeeded\00"
define i32 @do_test() {
%1 = alloca [26 x i8]
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = bitcast [26 x i8]* %1 to i8*
%6 = getelementptr inbounds [26 x i8], [26 x i8]* %1, i64 0, i64 0
%7 = call i32 @mkstemp(i8* %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @EXIT_FAILURE
%12 = load i32, i32* @errno
%13 = call i32 @error(i32 %11, i32 %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %10, %0
%15 = load i32, i32* %2
%16 = call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %3
%17 = load i32*, i32** %3
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @EXIT_FAILURE
%21 = load i32, i32* @errno
%22 = call i32 @error(i32 %20, i32 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %19, %14
%24 = load i32*, i32** %3
%25 = call i32 @setlinebuf(i32* %24)
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [26 x i8], [26 x i8]* %1, i64 0, i64 0
%29 = call i32 @unlink(i8* %28)
%30 = load i32*, i32** %3
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %4
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %23
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @error(i32 %37, i32 0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %39
39: ; preds = %36, %23
%40 = load i32*, i32** %3
%41 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 %41, i32* %4
%42 = load i32, i32* %4
%43 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %4
%45 = icmp sge i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @error(i32 %47, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %49
49: ; preds = %46, %39
%50 = load i32*, i32** %3
%51 = call i32 (i32*, i8*, ...) @fprintf(i32* %50, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
store i32 %51, i32* %4
%52 = load i32, i32* %4
%53 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %4
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %49
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @error(i32 %57, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
br label %59
59: ; preds = %56, %49
ret i32 0
}

LLVM IR (-O2):
@__const.do_test.tmpl = [26 x i8] c"/tmp/tst-put-error.XXXXXX\00"
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [29 x i8] c"cannot create temporary file\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"fdopen\00"
@.str.3 = [13 x i8] c"hello world\0A\00"
@.str.4 = [14 x i8] c"fprintf = %d\0A\00"
@.str.5 = [24 x i8] c"first fprintf succeeded\00"
@.str.6 = [25 x i8] c"second fprintf succeeded\00"
@.str.7 = [7 x i8] c"%100s\0A\00"
@.str.8 = [4 x i8] c"foo\00"
@.str.9 = [25 x i8] c"padded fprintf succeeded\00"
define i32 @do_test() {
%1 = alloca [26 x i8]
%2 = getelementptr inbounds [26 x i8], [26 x i8]* %1, i64 0, i64 0
%3 = call i32 @mkstemp(i8* %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @EXIT_FAILURE
%7 = load i32, i32* @errno
%8 = call i32 @error(i32 %6, i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %5, %0
%10 = call i32* @fdopen(i32 %3, 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 %16
12: ; preds = %9
%13 = load i32, i32* @EXIT_FAILURE
%14 = load i32, i32* @errno
%15 = call i32 @error(i32 %13, i32 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %12, %9
%17 = call i32 @setlinebuf(i32* %10)
%18 = call i32 @close(i32 %3)
%19 = call i32 @unlink(i8* %2)
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = icmp sgt i32 %20, -1
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @error(i32 %24, i32 0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %26
26: ; preds = %23, %16
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = icmp sgt i32 %27, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @error(i32 %31, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %33
33: ; preds = %30, %26
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%35 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = icmp sgt i32 %34, -1
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @error(i32 %38, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
br label %40
40: ; preds = %37, %33
ret i32 0
}