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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int write(int, char *, int);
void writeStdinTo(FILE *f) {
  char buf[(1024)];
  int fd = fileno(f);
  if (fd == -1)
    pexit("fileno");
  while (fgets(buf, (1024), stdin) != NULL) {
    if (buf[strlen(buf) - 1] == '\n')
      buf[strlen(buf) - 1] = '\0';
    if (write(fd, buf, strlen(buf)) == -1)
      pexit("pipe: write");
  }
}


x86 assembly (clang, -O0):
<writeStdinTo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    1187 <writeStdinTo+0x37>
lea    0xe7e(%rip),%rdi
call   1070 <pexit@plt>
jmp    118c <writeStdinTo+0x3c>
lea    -0x410(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     123f <writeStdinTo+0xef>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11eb <writeStdinTo+0x9b>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x414(%rbp),%eax
mov    %eax,-0x424(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x424(%rbp),%edi
mov    -0x420(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    123a <writeStdinTo+0xea>
lea    0xdd2(%rip),%rdi
call   1070 <pexit@plt>
jmp    118c <writeStdinTo+0x3c>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeStdinTo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
call   1060 <fileno@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1175 <writeStdinTo+0x25>
lea    0xe90(%rip),%rdi
call   1070 <pexit@plt>
mov    0x2e6c(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11fa <writeStdinTo+0xaa>
lea    0xe6c(%rip),%r14
jmp    11ce <writeStdinTo+0x7e>
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11f0 <writeStdinTo+0xa0>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11fa <writeStdinTo+0xaa>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11a0 <writeStdinTo+0x50>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    11a0 <writeStdinTo+0x50>
mov    %r14,%rdi
call   1070 <pexit@plt>
jmp    11b9 <writeStdinTo+0x69>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"fileno\00"
@stdin = global i32 0
@.str.1 = [12 x i8] c"pipe: write\00"
define void @writeStdinTo(i32* %0) {
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
store i32* %0, i32** %2
%5 = load i32*, i32** %2
%6 = call i32 @fileno(i32* %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
br label %12
12: ; preds = %41, %11
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* @stdin
%15 = call i32* @fgets(i8* %13, i32 1024, i32 %14)
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %42
17: ; preds = %12
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %26, label %32
26: ; preds = %17
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %26, %17
%33 = load i32, i32* %4
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @pexit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %32
br label %12
42: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"fileno\00"
@stdin = global i32 0
@.str.1 = [12 x i8] c"pipe: write\00"
define void @writeStdinTo(i32* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @fileno(i32* %0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* @stdin
%10 = call i32* @fgets(i8* %3, i32 1024, i32 %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %34, label %12
12: ; preds = %8, %30
%13 = call i32 @strlen(i8* %3)
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 10
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = call i32 @strlen(i8* %3)
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %19, %12
%25 = call i32 @strlen(i8* %3)
%26 = call i32 @write(i32 %4, i8* %3, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @pexit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = load i32, i32* @stdin
%32 = call i32* @fgets(i8* %3, i32 1024, i32 %31)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %12
34: ; preds = %30, %8
ret void
}