#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 */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
void readfile(int fd) {
  static char buf[1024];
  ssize_t n;
  for (;;) {
    n = read(fd, buf, 1024);
    if (n < 0) {
      perror("readfile: read()");
    }
    if (!n) {
      break;
    }
    write(STDOUT_FILENO, buf, n);
  }
  buf[0] = '\n';
  write(STDOUT_FILENO, buf, 1);
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    0x2f0b(%rip),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    116a <readfile+0x3a>
lea    0xe9b(%rip),%rdi
call   1050 <perror@plt>
cmpq   $0x0,-0x10(%rbp)
jne    117a <readfile+0x4a>
jmp    119a <readfile+0x6a>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
lea    0x2ec0(%rip),%rsi
call   1030 <write@plt>
jmp    113b <readfile+0xb>
movb   $0xa,0x2eaf(%rip)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0x2e9f(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebp
lea    0x2f0f(%rip),%r14
lea    0xeb8(%rip),%r15
mov    0x2ea1(%rip),%r12
jmp    116e <readfile+0x3e>
mov    %r15,%rdi
call   1050 <perror@plt>
nopl   0x0(%rax)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1151 <readfile+0x21>
jne    1160 <readfile+0x30>
movb   $0xa,0x2ec2(%rip)
lea    0x2ebb(%rip),%rsi
mov    (%r12),%edi
mov    $0x1,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <write@plt>

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

LLVM IR (-O2):
@readfile.buf = internal global [1024 x i8] zeroinitializer
@.str = [17 x i8] c"readfile: read()\00"
@STDOUT_FILENO = global i32 0
define void @readfile(i32 %0) {
br label %2
2: ; preds = %9, %1
%3 = tail call i64 @read(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readfile.buf, i64 0, i64 0), i32 1024)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %9
7: ; preds = %2
%8 = icmp eq i64 %3, 0
br i1 %8, label %13, label %9
9: ; preds = %5, %7
%10 = load i32, i32* @STDOUT_FILENO
%11 = trunc i64 %3 to i32
%12 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readfile.buf, i64 0, i64 0), i32 %11)
br label %2
13: ; preds = %7
store i8 10, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readfile.buf, i64 0, i64 0)
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @write(i32 %14, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @readfile.buf, i64 0, i64 0), i32 1)
ret void
}