#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 geti();
int getpid();
int /*<<< orphan*/ pfd();
int /*<<< orphan*/ printf(char *, ...);
int syscall(int, int, char *, int);
int read_pipe() {
  char fds[32], buf[1024];
  int fd, n, nbytes;
  pfd();
  printf("enter fd: ");
  fd = geti();
  printf("nbytes: ");
  nbytes = geti();
  n = syscall(31, fd, buf, nbytes);
  if (n >= 0) {
    printf("proc %d back to Umode, read %d bytes from pipe:\n", getpid(), n);
    buf[n] = 0;
    printf("'%s'\n", buf);
  } else {
    printf("read pipe failed\n");
  }
}


x86 assembly (clang, -O0):
<read_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    $0x0,%al
call   1070 <pfd@plt>
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <geti@plt>
mov    %eax,-0x434(%rbp)
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <geti@plt>
mov    %eax,-0x43c(%rbp)
mov    -0x434(%rbp),%esi
lea    -0x430(%rbp),%rdx
mov    -0x43c(%rbp),%ecx
mov    $0x1f,%edi
call   1050 <syscall@plt>
mov    %eax,-0x438(%rbp)
cmpl   $0x0,-0x438(%rbp)
jl     120e <read_pipe+0xbe>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x438(%rbp),%edx
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x438(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121c <read_pipe+0xcc>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pipe>:
push   %rbx
sub    $0x400,%rsp
xor    %eax,%eax
call   1080 <pfd@plt>
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <geti@plt>
mov    %eax,%ebx
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <geti@plt>
mov    %rsp,%rdx
mov    $0x1f,%edi
mov    %ebx,%esi
mov    %eax,%ecx
call   1060 <syscall@plt>
test   %eax,%eax
js     11e4 <read_pipe+0x84>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe54(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xe6d(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f0 <read_pipe+0x90>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"enter fd: \00"
@.str.1 = [9 x i8] c"nbytes: \00"
@.str.2 = [49 x i8] c"proc %d back to Umode, read %d bytes from pipe:\0A\00"
@.str.3 = [6 x i8] c"'%s'\0A\00"
@.str.4 = [18 x i8] c"read pipe failed\0A\00"
define i32 @read_pipe() {
%1 = alloca i32
%2 = alloca [32 x i8]
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = call i32 (...) @pfd()
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (...) @geti()
store i32 %9, i32* %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (...) @geti()
store i32 %11, i32* %6
%12 = load i32, i32* %4
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* %6
%15 = call i32 @syscall(i32 31, i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %0
%19 = call i32 (...) @getpid()
%20 = load i32, i32* %5
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %29
27: ; preds = %0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %18
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@.str = [11 x i8] c"enter fd: \00"
@.str.1 = [9 x i8] c"nbytes: \00"
@.str.2 = [49 x i8] c"proc %d back to Umode, read %d bytes from pipe:\0A\00"
@.str.3 = [6 x i8] c"'%s'\0A\00"
@str = [17 x i8] c"read pipe failed\00"
define i32 @read_pipe() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @pfd()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 (...) @geti()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 (...) @geti()
%8 = call i32 @syscall(i32 31, i32 %5, i8* %2, i32 %7)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = call i32 (...) @getpid()
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %8)
%13 = zext i32 %8 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %2)
br label %18
16: ; preds = %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %16, %10
ret i32 undef
}