#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int main() {
  char buf[1024];
  while (fgets(buf, sizeof buf, stdin)) {
    ssize_t l = strlen(buf);
    ssize_t n = write(1, buf, l);
    if (n != l) {
      perror("write");
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11d9 <main+0x99>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%rdx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rax
cmp    -0x418(%rbp),%rax
je     11d4 <main+0x94>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <main+0xa0>
jmp    1152 <main+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e95(%rip),%r15
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11a8 <main+0x68>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    $0x1,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbx,%rax
je     1160 <main+0x20>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
jmp    11aa <main+0x6a>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [6 x i8] c"write\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i64
%4 = alloca i64
store i32 0, i32* %1
br label %5
5: ; preds = %21, %0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = load i32, i32* @stdin
%8 = call i64 @fgets(i8* %6, i32 1024, i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %22
10: ; preds = %5
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %3
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%14 = load i64, i64* %3
%15 = call i64 @write(i32 1, i8* %13, i64 %14)
store i64 %15, i64* %4
%16 = load i64, i64* %4
%17 = load i64, i64* %3
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %23
21: ; preds = %10
br label %5
22: ; preds = %5
store i32 0, i32* %1
br label %23
23: ; preds = %22, %19
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [6 x i8] c"write\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %7, %0
%4 = load i32, i32* @stdin
%5 = call i64 @fgets(i8* %2, i32 1024, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = call i64 @strlen(i8* %2)
%9 = call i64 @write(i32 1, i8* %2, i64 %8)
%10 = icmp eq i64 %9, %8
br i1 %10, label %3, label %11
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %3, %11
%14 = phi i32 [ 1, %11 ], [ 0, %3 ]
ret i32 %14
}