#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 */
scalar_t__ EOF;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ puts(char *);
int main(void) {
  int fd[2];
  FILE *f;
  if (pipe(fd)) {
    perror("pipe");
    return 1;
  }
  f = fdopen(fd[1], "w");
  if (f == NULL) {
    perror("fdopen");
    return 1;
  }
  __set_errno(0);
  if (fputs("fnord", f) == EOF) {
    perror("fputs");
    return 1;
  }
  if (errno) {
    perror("errno gratuitously set -- TEST FAILED");
    return 1;
  }
  puts("Test succeeded.");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     119a <main+0x3a>
lea    0xe77(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124b <main+0xeb>
mov    -0x8(%rbp),%edi
lea    0xe61(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <main+0x70>
lea    0xe48(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124b <main+0xeb>
xor    %edi,%edi
call   1030 <__set_errno@plt>
mov    -0x18(%rbp),%rsi
lea    0xe2c(%rip),%rdi
call   1050 <fputs@plt>
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
jne    120f <main+0xaf>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124b <main+0xeb>
mov    0x2dc2(%rip),%rax
cmpq   $0x0,(%rax)
je     1238 <main+0xd8>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124b <main+0xeb>
lea    0xe01(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    11cb <main+0x6b>
mov    0xc(%rsp),%edi
lea    0xe86(%rip),%rsi
call   1070 <fdopen@plt>
test   %rax,%rax
je     11d4 <main+0x74>
mov    %rax,%rbx
xor    %edi,%edi
call   1030 <__set_errno@plt>
lea    0xe74(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
je     11dd <main+0x7d>
mov    0x2e23(%rip),%rax
cmpq   $0x0,(%rax)
jne    11e6 <main+0x86>
lea    0xe7e(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    11f7 <main+0x97>
lea    0xe2e(%rip),%rdi
jmp    11ed <main+0x8d>
lea    0xe2c(%rip),%rdi
jmp    11ed <main+0x8d>
lea    0xe30(%rip),%rdi
jmp    11ed <main+0x8d>
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"fdopen\00"
@.str.3 = [6 x i8] c"fnord\00"
@EOF = global i64 0
@.str.4 = [6 x i8] c"fputs\00"
@errno = global i64 0
@.str.5 = [38 x i8] c"errno gratuitously set -- TEST FAILED\00"
@.str.6 = [16 x i8] c"Test succeeded.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32*
store i32 0, i32* %1
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %32
9: ; preds = %0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%11 = load i32, i32* %10
%12 = call i32* @fdopen(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %3
%13 = load i32*, i32** %3
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %32
17: ; preds = %9
%18 = call i32 @__set_errno(i32 0)
%19 = load i32*, i32** %3
%20 = call i64 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %19)
%21 = load i64, i64* @EOF
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %32
25: ; preds = %17
%26 = load i64, i64* @errno
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %32
30: ; preds = %25
%31 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %32
32: ; preds = %30, %28, %23, %15, %7
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"fdopen\00"
@.str.3 = [6 x i8] c"fnord\00"
@EOF = global i64 0
@.str.4 = [6 x i8] c"fputs\00"
@errno = global i64 0
@.str.5 = [38 x i8] c"errno gratuitously set -- TEST FAILED\00"
@.str.6 = [16 x i8] c"Test succeeded.\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%4 = call i64 @pipe(i32* %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %29
8: ; preds = %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%10 = load i32, i32* %9
%11 = call i32* @fdopen(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %29
15: ; preds = %8
%16 = call i32 @__set_errno(i32 0)
%17 = call i64 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %11)
%18 = load i64, i64* @EOF
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %29
22: ; preds = %15
%23 = load i64, i64* @errno
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
br label %29
27: ; preds = %22
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %29
29: ; preds = %27, %25, %20, %13, %6
%30 = phi i32 [ 1, %6 ], [ 1, %13 ], [ 1, %20 ], [ 1, %25 ], [ 0, %27 ]
ret i32 %30
}