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

/* Variables and functions */
int close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int write(int, char *, int);
void fsort() {
  int pfd[2], fd, nread;
  char buf[512];
  if (pipe(pfd) == -1)
    exit(-1);
  if ((fd = open("/home//haozhe26/ECS150/Test/foo", 0)) == -1)
    perror("not open");
  while ((nread = read(fd, buf, sizeof(buf))) != 0) {
    if (nread == -1)
      exit(-1);
    printf("buf is %s", buf);
    if (write(pfd[1], buf, nread) == -1)
      exit(-1);
  }
  if (close(fd) == -1 || close(pfd[1]) == -1) {
    exit(-1);
  }
  while ((nread = read(pfd[0], buf, sizeof(buf))) != 0) {
    if (nread == -1)
      exit(-1);
    if (write(1, buf, nread) == -1)
      exit(-1);
  }
  if (close(pfd[0]) == -1)
    exit(-1);
}


x86 assembly (clang, -O0):
<fsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
lea    -0x8(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11a7 <fsort+0x27>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xe52(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11cd <fsort+0x4d>
lea    0xe58(%rip),%rdi
call   1090 <perror@plt>
jmp    11d2 <fsort+0x52>
mov    -0xc(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     1243 <fsort+0xc3>
cmpl   $0xffffffff,-0x10(%rbp)
jne    1206 <fsort+0x86>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x210(%rbp),%rsi
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    123e <fsort+0xbe>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    11d2 <fsort+0x52>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     1265 <fsort+0xe5>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    126f <fsort+0xef>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    1274 <fsort+0xf4>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     12d4 <fsort+0x154>
cmpl   $0xffffffff,-0x10(%rbp)
jne    12a8 <fsort+0x128>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x210(%rbp),%rsi
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12cf <fsort+0x14f>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    1274 <fsort+0xf4>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12ef <fsort+0x16f>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     128d <fsort+0x10d>
lea    0xe59(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     127c <fsort+0xfc>
lea    0x10(%rsp),%rbx
lea    0xe63(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
test   %eax,%eax
je     120f <fsort+0x8f>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     128d <fsort+0x10d>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11d0 <fsort+0x50>
jmp    128d <fsort+0x10d>
mov    %r15d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     128d <fsort+0x10d>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     128d <fsort+0x10d>
lea    0x10(%rsp),%rbx
nop
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1260 <fsort+0xe0>
cmp    $0xffffffff,%eax
je     128d <fsort+0x10d>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1230 <fsort+0xb0>
jmp    128d <fsort+0x10d>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     128d <fsort+0x10d>
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9d(%rip),%rdi
call   1090 <perror@plt>
jmp    11ba <fsort+0x3a>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"/home//haozhe26/ECS150/Test/foo\00"
@.str.1 = [9 x i8] c"not open\00"
@.str.2 = [10 x i8] c"buf is %s\00"
define void @fsort() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = call i32 @open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %11, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
br label %16
16: ; preds = %37, %15
%17 = load i32, i32* %2
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 512)
store i32 %19, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %16
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %21
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%28 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%32 = load i32, i32* %3
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %26
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %26
br label %16
38: ; preds = %16
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %47, label %42
42: ; preds = %38
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %42, %38
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %42
br label %50
50: ; preds = %68, %49
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%52 = load i32, i32* %51
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 512)
store i32 %54, i32* %3
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %69
56: ; preds = %50
%57 = load i32, i32* %3
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %56
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%63 = load i32, i32* %3
%64 = call i32 @write(i32 1, i8* %62, i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @exit(i32 -1)
unreachable
68: ; preds = %61
br label %50
69: ; preds = %50
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %69
%75 = call i32 @exit(i32 -1)
unreachable
76: ; preds = %69
ret void
}

LLVM IR (-O2):
@.str = [32 x i8] c"/home//haozhe26/ECS150/Test/foo\00"
@.str.1 = [9 x i8] c"not open\00"
@.str.2 = [10 x i8] c"buf is %s\00"
define void @fsort() {
%1 = alloca [2 x i32]
%2 = alloca [512 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = call i32 @open(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %17
17: ; preds = %21, %15
%18 = call i32 @read(i32 %11, i8* %4, i32 512)
switch i32 %18, label %21 [
i32 0, label %28
i32 -1, label %19
]
19: ; preds = %17
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%23 = load i32, i32* %16
%24 = call i32 @write(i32 %23, i8* %4, i32 %18)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %17
26: ; preds = %21
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %17
%29 = call i32 @close(i32 %11)
%30 = icmp eq i32 %29, -1
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = load i32, i32* %16
%33 = call i32 @close(i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %31, %28
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %31, %42
%38 = load i32, i32* %5
%39 = call i32 @read(i32 %38, i8* %4, i32 512)
switch i32 %39, label %42 [
i32 0, label %47
i32 -1, label %40
]
40: ; preds = %37
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %37
%43 = call i32 @write(i32 1, i8* %4, i32 %39)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %37
45: ; preds = %42
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %37
%48 = load i32, i32* %5
%49 = call i32 @close(i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %47
ret void
}