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

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int /*<<< orphan*/);
void doit(int fd) {
  while (1) {
    char buf[4096];
    memset(buf, 0, 4096);
    ssize_t r = read(fd, buf, 4096);
    if (!r)
      return;
    write(STDOUT_FILENO, buf, r);
  }
}


x86 assembly (clang, -O0):
<doit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jne    1181 <doit+0x51>
add    $0x1020,%rsp
pop    %rbp
ret
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1030 <write@plt>
jmp    113e <doit+0xe>

x86 assembly (clang, -O2):
<doit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
je     119f <doit+0x6f>
mov    0x2e87(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    1170 <doit+0x40>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
define void @doit(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %1, %14
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 4096)
%8 = load i32, i32* %2
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 4096)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %5
ret void
14: ; preds = %5
%15 = load i32, i32* @STDOUT_FILENO
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* %4
%18 = call i32 @write(i32 %15, i8* %16, i32 %17)
br label %5
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
define void @doit(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 4096)
%5 = call i32 @read(i32 %0, i8* %3, i32 4096)
%6 = icmp eq i32 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %1, %7
%8 = phi i32 [ %12, %7 ], [ %5, %1 ]
%9 = load i32, i32* @STDOUT_FILENO
%10 = call i32 @write(i32 %9, i8* %3, i32 %8)
%11 = call i32 @memset(i8* %3, i32 0, i32 4096)
%12 = call i32 @read(i32 %0, i8* %3, i32 4096)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %7
14: ; preds = %7, %1
ret void
}