#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 */

/* Variables and functions */
int close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(void) {
  int p[2];
  if (-1 == pipe(p)) {
    perror("pipe");
    return 1;
  }
  if (-1 == close(p[0])) {
    perror("close read end");
    return 2;
  }
  printf("Hello world foo bar\n");
  fprintf(stderr, "something for you to read.\n");
  fflush(NULL);
  if (3 == write(p[1], "foo", 3)) {
    fprintf(stderr, "Write to pipe with no readers wrote all bytes!\n");
  }
  fprintf(stderr, "Write to write end of pipe with no readers did not"
                  " result in SIGPIPE?!?\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11af <main+0x3f>
lea    0xe62(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    125c <main+0xec>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11de <main+0x6e>
lea    0xe38(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    125c <main+0xec>
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <fflush@plt>
mov    -0x8(%rbp),%edi
lea    0xe33(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    %ecx,%eax
jne    1240 <main+0xd0>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     11fb <main+0x8b>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     1209 <main+0x99>
lea    0xf14(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e3b(%rip),%rbx
mov    (%rbx),%edi
lea    0xe66(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1080 <fflush@plt>
mov    0xc(%rsp),%edi
lea    0xe6b(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
jne    11e2 <main+0x72>
mov    (%rbx),%edi
lea    0xe57(%rip),%rsi
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe79(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdfe(%rip),%rdi
call   1090 <perror@plt>
jmp    11f0 <main+0x80>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
jmp    11f5 <main+0x85>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [15 x i8] c"close read end\00"
@.str.2 = [21 x i8] c"Hello world foo bar\0A\00"
@stderr = global i32 0
@.str.3 = [28 x i8] c"something for you to read.\0A\00"
@.str.4 = [4 x i8] c"foo\00"
@.str.5 = [48 x i8] c"Write to pipe with no readers wrote all bytes!\0A\00"
@.str.6 = [73 x i8] c"Write to write end of pipe with no readers did not result in SIGPIPE?!?\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
store i32 0, i32* %1
%3 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%4 = call i32 @pipe(i32* %3)
%5 = icmp eq i32 -1, %4
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %30
8: ; preds = %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = load i32, i32* %9
%11 = call i32 @close(i32 %10)
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %30
15: ; preds = %8
%16 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @fflush(i32* null)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%23 = icmp eq i32 3, %22
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %27
27: ; preds = %24, %15
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %30
30: ; preds = %27, %13, %6
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [15 x i8] c"close read end\00"
@stderr = global i32 0
@.str.3 = [28 x i8] c"something for you to read.\0A\00"
@.str.4 = [4 x i8] c"foo\00"
@.str.5 = [48 x i8] c"Write to pipe with no readers wrote all bytes!\0A\00"
@.str.6 = [73 x i8] c"Write to write end of pipe with no readers did not result in SIGPIPE?!?\0A\00"
@str = [20 x i8] c"Hello world foo bar\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 i32 @pipe(i32* %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
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 = load i32, i32* %3
%10 = call i32 @close(i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %29
14: ; preds = %8
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @fflush(i32* null)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @write(i32 %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%22 = icmp eq i32 %21, 3
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %26
26: ; preds = %23, %14
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.6, i64 0, i64 0))
br label %29
29: ; preds = %26, %12, %6
%30 = phi i32 [ 1, %6 ], [ 2, %12 ], [ 1, %26 ]
ret i32 %30
}