#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ pexit(char *);
int read(int, char *, int);
int write(int /*<<< orphan*/, char *, int);
void readAndPrint(FILE *f) {
  char buf[(1024)];
  ssize_t numRead;
  int fd = fileno(f);
  if (fd == -1)
    pexit("fileno");
  while ((numRead = read(fd, buf, (1024))) != 0) {
    if (write(STDOUT_FILENO, buf, numRead) != numRead)
      pexit("stdout: partial write");
  }
  if (numRead == -1)
    pexit("read");
}


x86 assembly (clang, -O0):
<readAndPrint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0xffffffff,-0x418(%rbp)
jne    1177 <readAndPrint+0x37>
lea    0xe8e(%rip),%rdi
call   1060 <pexit@plt>
jmp    117c <readAndPrint+0x3c>
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
je     11da <readAndPrint+0x9a>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
je     11d5 <readAndPrint+0x95>
lea    0xe37(%rip),%rdi
call   1060 <pexit@plt>
jmp    117c <readAndPrint+0x3c>
cmpl   $0xffffffff,-0x414(%rbp)
jne    11f3 <readAndPrint+0xb3>
lea    0xe2f(%rip),%rdi
call   1060 <pexit@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readAndPrint>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
call   1050 <fileno@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1168 <readAndPrint+0x28>
lea    0xe9d(%rip),%rdi
call   1060 <pexit@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11d2 <readAndPrint+0x92>
mov    %eax,%ebp
mov    0x2e6b(%rip),%r12
mov    %rsp,%rbx
lea    0xe78(%rip),%r14
jmp    11b6 <readAndPrint+0x76>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11d2 <readAndPrint+0x92>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11a0 <readAndPrint+0x60>
mov    %r14,%rdi
call   1060 <pexit@plt>
jmp    11a0 <readAndPrint+0x60>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"fileno\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [22 x i8] c"stdout: partial write\00"
@.str.2 = [5 x i8] c"read\00"
define void @readAndPrint(i32* %0) {
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
%6 = load i32*, i32** %2
%7 = call i32 @fileno(i32* %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
br label %13
13: ; preds = %27, %12
%14 = load i32, i32* %5
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 1024)
store i32 %16, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %13
%19 = load i32, i32* @STDOUT_FILENO
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%21 = load i32, i32* %4
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
%23 = load i32, i32* %4
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @pexit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
br label %13
28: ; preds = %13
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"fileno\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [22 x i8] c"stdout: partial write\00"
define void @readAndPrint(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 = call i32 @read(i32 %4, i8* %3, i32 1024)
%10 = icmp eq i32 %9, 0
br i1 %10, label %21, label %11
11: ; preds = %8, %18
%12 = phi i32 [ %19, %18 ], [ %9, %8 ]
%13 = load i32, i32* @STDOUT_FILENO
%14 = call i32 @write(i32 %13, i8* %3, i32 %12)
%15 = icmp eq i32 %14, %12
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @pexit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = call i32 @read(i32 %4, i8* %3, i32 1024)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %11
21: ; preds = %18, %8
ret void
}