#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*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fputc(int /*<<< orphan*/, int /*<<< orphan*/ *);
int getNullFd(char *);
int /*<<< orphan*/ porting_dbg(char *, int /*<<< orphan*/ *, int, int, int,
                               int /*<<< orphan*/, char const *, int);
int setferror(const char *F, int L, FILE *fp) {
  int fd, sfd, nfd;
  sfd = nfd = -1;
  fd = fileno(fp);
  sfd = dup(fd);
  nfd = getNullFd("Setferror");
  dup2(nfd, fd);
  fputc(0, fp);
  fflush(fp);
  dup2(sfd, fd);
  close(sfd);
  porting_dbg("## Setferror(%X/%d,%d,%d)%X %s:%d", fp, fd, sfd, nfd, ferror(fp),
              F, L);
  return 0;
}


x86 assembly (clang, -O0):
<setferror>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <dup@plt>
mov    %eax,-0x20(%rbp)
lea    0xe31(%rip),%rdi
call   1040 <getNullFd@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <dup2@plt>
mov    -0x18(%rbp),%rsi
xor    %edi,%edi
call   1080 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <fflush@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    %eax,%r9d
mov    -0x8(%rbp),%r10
mov    -0xc(%rbp),%eax
lea    0xdbf(%rip),%rdi
mov    %r10,(%rsp)
mov    %eax,0x8(%rsp)
call   1090 <porting_dbg@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setferror>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    %rdx,%rdi
call   10a0 <fileno@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <dup@plt>
mov    %eax,%r12d
lea    0xe41(%rip),%rdi
call   1040 <getNullFd@plt>
mov    %eax,%r13d
mov    %eax,%edi
mov    %ebp,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
mov    %rbx,%rdi
call   10b0 <fflush@plt>
mov    %r12d,%edi
mov    %ebp,%esi
call   1050 <dup2@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
lea    0xe07(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r12d,%ecx
mov    %r13d,%r8d
mov    %eax,%r9d
push   %r15
push   %r14
call   1090 <porting_dbg@plt>
add    $0x10,%rsp
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Setferror\00"
@.str.1 = [34 x i8] c"## Setferror(%X/%d,%d,%d)%X %s:%d\00"
define i32 @setferror(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 -1, i32* %9
store i32 -1, i32* %8
%10 = load i32*, i32** %6
%11 = call i32 @fileno(i32* %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = call i32 @dup(i32 %12)
store i32 %13, i32* %8
%14 = call i32 @getNullFd(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = load i32, i32* %7
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = load i32*, i32** %6
%19 = call i32 @fputc(i32 0, i32* %18)
%20 = load i32*, i32** %6
%21 = call i32 @fflush(i32* %20)
%22 = load i32, i32* %8
%23 = load i32, i32* %7
%24 = call i32 @dup2(i32 %22, i32 %23)
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
%27 = load i32*, i32** %6
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = load i32, i32* %9
%31 = load i32*, i32** %6
%32 = call i32 @ferror(i32* %31)
%33 = load i8*, i8** %4
%34 = load i32, i32* %5
%35 = call i32 @porting_dbg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32* %27, i32 %28, i32 %29, i32 %30, i32 %32, i8* %33, i32 %34)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"Setferror\00"
@.str.1 = [34 x i8] c"## Setferror(%X/%d,%d,%d)%X %s:%d\00"
define i32 @setferror(i8* %0, i32 %1, i32* %2) {
%4 = tail call i32 @fileno(i32* %2)
%5 = tail call i32 @dup(i32 %4)
%6 = tail call i32 @getNullFd(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @dup2(i32 %6, i32 %4)
%8 = tail call i32 @fputc(i32 0, i32* %2)
%9 = tail call i32 @fflush(i32* %2)
%10 = tail call i32 @dup2(i32 %5, i32 %4)
%11 = tail call i32 @close(i32 %5)
%12 = tail call i32 @ferror(i32* %2)
%13 = tail call i32 @porting_dbg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32 %4, i32 %5, i32 %6, i32 %12, i8* %0, i32 %1)
ret i32 0
}