#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*/ gets(char *, int);
int /*<<< orphan*/ pfd();
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int syscall(int, int, char *, int);
int write_pipe() {
  char fds[16], buf[1024];
  int fd, n, nbytes;
  pfd();
  printf("FD? ");
  fd = geti();
  printf("String? ");
  gets(buf, 1024);
  nbytes = strlen(buf);
  n = syscall(32, fd, buf, nbytes);
  if (n >= 0) {
    printf("proc %d back to Umode, wrote %d bytes to pipe\n", getpid(), n);
  } else {
    printf("write pipe failed\n");
  }
}


x86 assembly (clang, -O0):
<write_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    $0x0,%al
call   1090 <pfd@plt>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <geti@plt>
mov    %eax,-0x424(%rbp)
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   1070 <gets@plt>
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x424(%rbp),%esi
lea    -0x420(%rbp),%rdx
mov    -0x42c(%rbp),%ecx
mov    $0x20,%edi
call   1060 <syscall@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jl     1220 <write_pipe+0xb0>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x428(%rbp),%edx
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    122e <write_pipe+0xbe>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pipe>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
xor    %eax,%eax
call   10a0 <pfd@plt>
lea    0xe69(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <geti@plt>
mov    %eax,%ebp
lea    0xe57(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1080 <gets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0x20,%edi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1070 <syscall@plt>
test   %eax,%eax
js     11ff <write_pipe+0x7f>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1c(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    120b <write_pipe+0x8b>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"FD? \00"
@.str.1 = [9 x i8] c"String? \00"
@.str.2 = [47 x i8] c"proc %d back to Umode, wrote %d bytes to pipe\0A\00"
@.str.3 = [19 x i8] c"write pipe failed\0A\00"
define i32 @write_pipe() {
%1 = alloca i32
%2 = alloca [16 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 ([5 x i8], [5 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 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = call i32 @gets(i8* %11, i32 1024)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %6
%15 = load i32, i32* %4
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* %6
%18 = call i32 @syscall(i32 32, i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %0
%22 = call i32 (...) @getpid()
%23 = load i32, i32* %5
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23)
br label %27
25: ; preds = %0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [5 x i8] c"FD? \00"
@.str.1 = [9 x i8] c"String? \00"
@.str.2 = [47 x i8] c"proc %d back to Umode, wrote %d bytes to pipe\0A\00"
@str = [18 x i8] c"write pipe failed\00"
define i32 @write_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 ([5 x i8], [5 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 = call i32 @gets(i8* %2, i32 1024)
%8 = call i32 @strlen(i8* %2)
%9 = call i32 @syscall(i32 32, i32 %5, i8* %2, i32 %8)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %9)
br label %16
14: ; preds = %0
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
ret i32 undef
}