#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 */
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int getpid();
int /*<<< orphan*/ *opendir(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int pipeTest() {
  struct dirent *dp;
  int pipefd[2] = {0};
  int ret = pipe(pipefd);
  printf("TESTING PIPES\n");
  printf("Returned: (%d,%d) = %d\n", pipefd[0], pipefd[1], ret);
  char buffer[128];
  sprintf(buffer, "/proc/%d/fd", getpid());
  DIR *dir = opendir(buffer);
  printf("Opened the dir (%s): %p\n", buffer, dir);
  if (dir != NULL) {
    while ((dp = readdir(dir)) != NULL) {
      printf("%s\n", dp->d_name);
    }
    closedir(dir);
  }
  FILE *readFile = fdopen(pipefd[0], "r");
  FILE *writeFile = fdopen(pipefd[1], "w");
  printf("fdopen(): (%p,%p)\n", readFile, writeFile);
  if (readFile != NULL && writeFile != NULL) {
    int one = 0;
    int two = 0;
    fprintf(writeFile, "%d and %d\n", 234, 345);
    fflush(writeFile);
    fscanf(readFile, "%d and %d", &one, &two);
    printf("Read from pipe: %d, %d\n", one, two);
    fprintf(writeFile, "more");
    fclose(readFile);
    fclose(writeFile);
  } else {
    printf("Could not do fopen()\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<pipeTest>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
lea    -0x10(%rbp),%rdi
call   1090 <pipe@plt>
mov    %eax,-0x14(%rbp)
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0xc8(%rbp),%rdi
mov    %eax,%edx
lea    0xde6(%rip),%rsi
call   10f0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rsi
mov    -0xa8(%rbp),%rdx
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x0,-0xa8(%rbp)
je     12c8 <pipeTest+0xf8>
jmp    1288 <pipeTest+0xb8>
mov    -0xa8(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     12bc <pipeTest+0xec>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1288 <pipeTest+0xb8>
mov    -0xa8(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x10(%rbp),%edi
lea    0xd7e(%rip),%rsi
call   10e0 <fdopen@plt>
mov    %rax,-0xb0(%rbp)
mov    -0xc(%rbp),%edi
lea    0xd6a(%rip),%rsi
call   10e0 <fdopen@plt>
mov    %rax,-0xb8(%rbp)
mov    -0xb0(%rbp),%rsi
mov    -0xb8(%rbp),%rdx
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x0,-0xb0(%rbp)
je     13d8 <pipeTest+0x208>
cmpq   $0x0,-0xb8(%rbp)
je     13d8 <pipeTest+0x208>
movl   $0x0,-0xbc(%rbp)
movl   $0x0,-0xc0(%rbp)
mov    -0xb8(%rbp),%rdi
lea    0xd19(%rip),%rsi
mov    $0xea,%edx
mov    $0x159,%ecx
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0xb8(%rbp),%rdi
call   10d0 <fflush@plt>
mov    -0xb0(%rbp),%rdi
lea    0xcf9(%rip),%rsi
lea    -0xbc(%rbp),%rdx
lea    -0xc0(%rbp),%rcx
call   1080 <fscanf@plt>
mov    -0xbc(%rbp),%esi
mov    -0xc0(%rbp),%edx
lea    0xcdd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xb8(%rbp),%rdi
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0xb0(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0xb8(%rbp),%rdi
call   1040 <fclose@plt>
jmp    13e6 <pipeTest+0x216>
lea    0xcba(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipeTest>:
push   %r14
push   %rbx
sub    $0x98,%rsp
movq   $0x0,(%rsp)
mov    %rsp,%rdi
call   1090 <pipe@plt>
mov    %eax,%ebx
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
lea    0xdea(%rip),%rdi
mov    %ebx,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdeb(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %eax,%edx
call   10f0 <sprintf@plt>
mov    %r14,%rdi
call   1060 <opendir@plt>
mov    %rax,%rbx
lea    0xdd6(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
test   %rbx,%rbx
je     127f <pipeTest+0x9f>
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1277 <pipeTest+0x97>
mov    (%rax),%rdi
call   1030 <puts@plt>
jmp    1260 <pipeTest+0x80>
mov    %rbx,%rdi
call   10a0 <closedir@plt>
mov    (%rsp),%edi
lea    0xdb4(%rip),%rsi
call   10e0 <fdopen@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edi
lea    0xda3(%rip),%rsi
call   10e0 <fdopen@plt>
mov    %rax,%rbx
lea    0xd96(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
test   %r14,%r14
je     1357 <pipeTest+0x177>
test   %rbx,%rbx
je     1357 <pipeTest+0x177>
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0xd73(%rip),%rsi
mov    %rbx,%rdi
mov    $0xea,%edx
mov    $0x159,%ecx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %rbx,%rdi
call   10d0 <fflush@plt>
lea    0xd5b(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
call   1080 <fscanf@plt>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
lea    0xd44(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd4e(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1100 <fwrite@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1363 <pipeTest+0x183>
lea    0xd36(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [15 x i8] c"TESTING PIPES\0A\00"
@.str.1 = [24 x i8] c"Returned: (%d,%d) = %d\0A\00"
@.str.2 = [12 x i8] c"/proc/%d/fd\00"
@.str.3 = [25 x i8] c"Opened the dir (%s): %p\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [19 x i8] c"fdopen(): (%p,%p)\0A\00"
@.str.8 = [11 x i8] c"%d and %d\0A\00"
@.str.9 = [10 x i8] c"%d and %d\00"
@.str.10 = [24 x i8] c"Read from pipe: %d, %d\0A\00"
@.str.11 = [5 x i8] c"more\00"
@.str.12 = [22 x i8] c"Could not do fopen()\0A\00"
define i32 @pipeTest() {
%1 = alloca %struct.dirent*
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = bitcast [2 x i32]* %2 to i8*
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
store i32 %12, i32* %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%17 = load i32, i32* %16
%18 = load i32, i32* %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %17, i32 %18)
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%21 = call i32 (...) @getpid()
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%24 = call i32* @opendir(i8* %23)
store i32* %24, i32** %5
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%26 = load i32*, i32** %5
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %25, i32* %26)
%28 = load i32*, i32** %5
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %43
30: ; preds = %0
br label %31
31: ; preds = %35, %30
%32 = load i32*, i32** %5
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %1
%34 = icmp ne %struct.dirent* %33, null
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load %struct.dirent*, %struct.dirent** %1
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %38)
br label %31
40: ; preds = %31
%41 = load i32*, i32** %5
%42 = call i32 @closedir(i32* %41)
br label %43
43: ; preds = %40, %0
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32* @fdopen(i32 %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %46, i32** %6
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32* @fdopen(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %49, i32** %7
%50 = load i32*, i32** %6
%51 = load i32*, i32** %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32* %50, i32* %51)
%53 = load i32*, i32** %6
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %74
55: ; preds = %43
%56 = load i32*, i32** %7
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %74
58: ; preds = %55
store i32 0, i32* %8
store i32 0, i32* %9
%59 = load i32*, i32** %7
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %59, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 234, i32 345)
%61 = load i32*, i32** %7
%62 = call i32 @fflush(i32* %61)
%63 = load i32*, i32** %6
%64 = call i32 @fscanf(i32* %63, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i32* %8, i32* %9)
%65 = load i32, i32* %8
%66 = load i32, i32* %9
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0), i32 %65, i32 %66)
%68 = load i32*, i32** %7
%69 = call i32 (i32*, i8*, ...) @fprintf(i32* %68, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0))
%70 = load i32*, i32** %6
%71 = call i32 @fclose(i32* %70)
%72 = load i32*, i32** %7
%73 = call i32 @fclose(i32* %72)
br label %76
74: ; preds = %55, %43
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.12, i64 0, i64 0))
br label %76
76: ; preds = %74, %58
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str.1 = [24 x i8] c"Returned: (%d,%d) = %d\0A\00"
@.str.2 = [12 x i8] c"/proc/%d/fd\00"
@.str.3 = [25 x i8] c"Opened the dir (%s): %p\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [19 x i8] c"fdopen(): (%p,%p)\0A\00"
@.str.8 = [11 x i8] c"%d and %d\0A\00"
@.str.9 = [10 x i8] c"%d and %d\00"
@.str.10 = [24 x i8] c"Read from pipe: %d, %d\0A\00"
@.str.11 = [5 x i8] c"more\00"
@str = [14 x i8] c"TESTING PIPES\00"
@str.13 = [21 x i8] c"Could not do fopen()\00"
define i32 @pipeTest() {
%1 = alloca i64
%2 = bitcast i64* %1 to [2 x i32]*
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%7 = bitcast i64* %1 to i32*
%8 = call i32 @pipe(i32* %7)
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* %7
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %12, i32 %8)
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%15 = call i32 (...) @getpid()
%16 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32* @opendir(i8* %14)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %14, i32* %17)
%19 = icmp eq i32* %17, null
br i1 %19, label %32, label %20
20: ; preds = %0
%21 = call %struct.dirent* @readdir(i32* %17)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %30, label %23
23: ; preds = %20, %23
%24 = phi %struct.dirent* [ %28, %23 ], [ %21, %20 ]
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @puts(i8* dereferenceable(1) %26)
%28 = call %struct.dirent* @readdir(i32* %17)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %30, label %23
30: ; preds = %23, %20
%31 = call i32 @closedir(i32* %17)
br label %32
32: ; preds = %30, %0
%33 = load i32, i32* %7
%34 = call i32* @fdopen(i32 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32, i32* %11
%36 = call i32* @fdopen(i32 %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32* %34, i32* %36)
%38 = icmp ne i32* %34, null
%39 = icmp ne i32* %36, null
%40 = and i1 %38, %39
br i1 %40, label %41, label %53
41: ; preds = %32
%42 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%43 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 234, i32 345)
%45 = call i32 @fflush(i32* %36)
%46 = call i32 @fscanf(i32* %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i32* %4, i32* %5)
%47 = load i32, i32* %4
%48 = load i32, i32* %5
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0), i32 %47, i32 %48)
%50 = call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0), i64 4, i64 1, i32* %36)
%51 = call i32 @fclose(i32* %34)
%52 = call i32 @fclose(i32* %36)
br label %55
53: ; preds = %32
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.13, i64 0, i64 0))
br label %55
55: ; preds = %53, %41
ret i32 0
}