#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 EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd[2];
  if (pipe(fd) == 0) {
    printf("fd[0]=%d\n", fd[0]);
    printf("fd[1]=%d\n", fd[1]);
    char *text = "Ja Hallo erstmal, ...\n";
    int write_ret = write(fd[1], text, strlen(text));
    if (write_ret == -1) {
      perror("Fehler beim Schreiben in die Pipe");
    } else {
      printf("%d Zeichen geschrieben.\n", write_ret);
    }
    close(fd[1]);
    printf("\nLese aus der Pipe ...\n");
    char buf[51];
    int read_ret;
    while ((read_ret = read(fd[0], buf, 50)) > 0) {
      buf[read_ret] = '\0';
      printf("%s", buf);
    }
    if (read_ret == -1) {
      perror("Fehler beim Lesen der Pipe");
    }
    close(fd[0]);
  } else {
    perror("Fehler beim Erzeugen der Pipe");
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jne    1293 <main+0x123>
mov    -0x18(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe4f(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    120c <main+0x9c>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
jmp    121d <main+0xad>
mov    -0x24(%rbp),%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x32,%edx
call   1080 <read@plt>
mov    %eax,-0x64(%rbp)
cmp    $0x0,%eax
jle    1270 <main+0x100>
movslq -0x64(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rsi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1233 <main+0xc3>
cmpl   $0xffffffff,-0x64(%rbp)
jne    1286 <main+0x116>
lea    0xe00(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    129f <main+0x12f>
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
jne    1278 <main+0xf8>
mov    0x8(%rsp),%esi
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rsp),%esi
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rsp),%ebx
lea    0xe4b(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1286 <main+0x106>
lea    0xe60(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x32,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    1259 <main+0xd9>
lea    0xe3d(%rip),%r14
lea    0x10(%rsp),%rbx
xchg   %ax,%ax
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x32,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     1230 <main+0xb0>
cmp    $0xffffffff,%eax
je     1297 <main+0x117>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
jmp    1267 <main+0xe7>
lea    0xd9e(%rip),%rdi
call   10a0 <perror@plt>
jmp    11f6 <main+0x76>
lea    0xdcb(%rip),%rdi
call   10a0 <perror@plt>
jmp    125e <main+0xde>

LLVM IR (-O0):
@.str = [10 x i8] c"fd[0]=%d\0A\00"
@.str.1 = [10 x i8] c"fd[1]=%d\0A\00"
@.str.2 = [23 x i8] c"Ja Hallo erstmal, ...\0A\00"
@.str.3 = [34 x i8] c"Fehler beim Schreiben in die Pipe\00"
@.str.4 = [25 x i8] c"%d Zeichen geschrieben.\0A\00"
@.str.5 = [24 x i8] c"\0ALese aus der Pipe ...\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [27 x i8] c"Fehler beim Lesen der Pipe\00"
@.str.8 = [30 x i8] c"Fehler beim Erzeugen der Pipe\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [51 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%12 = call i64 @pipe(i32* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %60
14: ; preds = %2
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8** %7
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%22 = load i32, i32* %21
%23 = load i8*, i8** %7
%24 = load i8*, i8** %7
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @write(i32 %22, i8* %23, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %14
%30 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %34
31: ; preds = %14
%32 = load i32, i32* %8
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %31, %29
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %39
39: ; preds = %45, %34
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds [51 x i8], [51 x i8]* %9, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 50)
store i32 %43, i32* %10
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [51 x i8], [51 x i8]* %9, i64 0, i64 %47
store i8 0, i8* %48
%49 = getelementptr inbounds [51 x i8], [51 x i8]* %9, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %49)
br label %39
51: ; preds = %39
%52 = load i32, i32* %10
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
br label %62
60: ; preds = %2
%61 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %62
62: ; preds = %60, %56
%63 = load i32, i32* @EXIT_SUCCESS
ret i32 %63
}

LLVM IR (-O2):
@.str = [10 x i8] c"fd[0]=%d\0A\00"
@.str.1 = [10 x i8] c"fd[1]=%d\0A\00"
@.str.2 = [23 x i8] c"Ja Hallo erstmal, ...\0A\00"
@.str.3 = [34 x i8] c"Fehler beim Schreiben in die Pipe\00"
@.str.4 = [25 x i8] c"%d Zeichen geschrieben.\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [27 x i8] c"Fehler beim Lesen der Pipe\00"
@.str.8 = [30 x i8] c"Fehler beim Erzeugen der Pipe\00"
@EXIT_SUCCESS = global i32 0
@str = [23 x i8] c"\0ALese aus der Pipe ...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [51 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %47
9: ; preds = %2
%10 = load i32, i32* %6
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%13 = load i32, i32* %12
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %12
%16 = call i32 @strlen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @write(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %9
%20 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %23
21: ; preds = %9
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %17)
br label %23
23: ; preds = %21, %19
%24 = load i32, i32* %12
%25 = call i32 @close(i32 %24)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%27 = getelementptr inbounds [51 x i8], [51 x i8]* %4, i64 0, i64 0
%28 = load i32, i32* %6
%29 = call i32 @read(i32 %28, i8* %27, i32 50)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %23, %31
%32 = phi i32 [ %37, %31 ], [ %29, %23 ]
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds [51 x i8], [51 x i8]* %4, i64 0, i64 %33
store i8 0, i8* %34
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %27)
%36 = load i32, i32* %6
%37 = call i32 @read(i32 %36, i8* %27, i32 50)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %31, label %39
39: ; preds = %31, %23
%40 = phi i32 [ %29, %23 ], [ %37, %31 ]
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
br label %49
47: ; preds = %2
%48 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
%50 = load i32, i32* @EXIT_SUCCESS
ret i32 %50
}