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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  int pd[2], i;
  char buffer[1024];
  FILE *w, *r;
  if (pipe(pd) < 0) {
    perror("pipe()");
    exit(EXIT_FAILURE);
  }
  switch (fork()) {
  case 0:
    (void)close(pd[0]);
    w = fdopen(pd[1], "w");
    for (i = 0; i < 10; i++) {
      (void)snprintf(buffer, 1024,
                     "Hi mum, this is message %d from your child\n", i);
      fprintf(w, "%s", buffer);
    }
    fclose(w);
    exit(EXIT_SUCCESS);
  }
  (void)close(pd[1]);
  r = fdopen(pd[0], "r");
  for (i = 0; i < 10; i++) {
    fgets(buffer, sizeof(buffer), r);
    (void)printf("%s", buffer);
  }
  fclose(r);
  (void)wait(NULL);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11ff <main+0x3f>
lea    0xe14(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    129f <main+0xdf>
jmp    1213 <main+0x53>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xde2(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x418(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jge    1285 <main+0xc5>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%ecx
mov    $0x400,%esi
lea    0xdb1(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x418(%rbp),%rdi
lea    -0x410(%rbp),%rdx
lea    0xdc3(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1238 <main+0x78>
mov    -0x418(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    0xd87(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x420(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jge    1307 <main+0x147>
lea    -0x410(%rbp),%rdi
mov    -0x420(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
lea    -0x410(%rbp),%rsi
lea    0xd41(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12c4 <main+0x104>
mov    -0x420(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10d0 <wait@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     1342 <main+0x182>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     135c <main+0x19c>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0xe36(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1090 <fgets@plt>
lea    0xe0f(%rip),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xcb7(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
lea    0xc97(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,%rbx
lea    0xc8a(%rip),%r15
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x1,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x2,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x3,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x4,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x5,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x6,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x7,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x8,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    $0x9,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2b08(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"pipe()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [44 x i8] c"Hi mum, this is message %d from your child\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32*
%6 = alloca i32*
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = call i32 (...) @fork()
switch i32 %15, label %41 [
i32 0, label %16
]
16: ; preds = %14
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32* @fdopen(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %5
store i32 0, i32* %3
br label %23
23: ; preds = %33, %16
%24 = load i32, i32* %3
%25 = icmp slt i32 %24, 10
br i1 %25, label %26, label %36
26: ; preds = %23
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = load i32, i32* %3
%29 = call i32 @snprintf(i8* %27, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %5
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %26
%34 = load i32, i32* %3
%35 = add nsw i32 %34, 1
store i32 %35, i32* %3
br label %23
36: ; preds = %23
%37 = load i32*, i32** %5
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %14
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32* @fdopen(i32 %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %47, i32** %6
store i32 0, i32* %3
br label %48
48: ; preds = %57, %41
%49 = load i32, i32* %3
%50 = icmp slt i32 %49, 10
br i1 %50, label %51, label %60
51: ; preds = %48
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%53 = load i32*, i32** %6
%54 = call i32 @fgets(i8* %52, i32 1024, i32* %53)
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%56 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %51
%58 = load i32, i32* %3
%59 = add nsw i32 %58, 1
store i32 %59, i32* %3
br label %48
60: ; preds = %48
%61 = load i32*, i32** %6
%62 = call i32 @fclose(i32* %61)
%63 = call i32 @wait(i32* null)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"pipe()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [44 x i8] c"Hi mum, this is message %d from your child\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1024 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = call i32 (...) @fork()
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %12
%16 = load i32, i32* %5
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 0)
%22 = call i32 @fputs(i8* %4, i32* %20)
%23 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 1)
%24 = call i32 @fputs(i8* %4, i32* %20)
%25 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 2)
%26 = call i32 @fputs(i8* %4, i32* %20)
%27 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 3)
%28 = call i32 @fputs(i8* %4, i32* %20)
%29 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 4)
%30 = call i32 @fputs(i8* %4, i32* %20)
%31 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 5)
%32 = call i32 @fputs(i8* %4, i32* %20)
%33 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 6)
%34 = call i32 @fputs(i8* %4, i32* %20)
%35 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 7)
%36 = call i32 @fputs(i8* %4, i32* %20)
%37 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 8)
%38 = call i32 @fputs(i8* %4, i32* %20)
%39 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 9)
%40 = call i32 @fputs(i8* %4, i32* %20)
%41 = call i32 @fclose(i32* %20)
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %12
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %5
%49 = call i32* @fdopen(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%52 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%53 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%54 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%55 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%56 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%57 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%58 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%59 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%60 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%61 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%62 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%63 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%64 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%65 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%66 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%67 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%68 = call i32 @fgets(i8* %4, i32 1024, i32* %49)
%69 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%70 = call i32 @fclose(i32* %49)
%71 = call i32 @wait(i32* null)
ret i32 0
}