void cat(int fd) {
  char buf[128];
  int n;
  while ((n = read(fd, buf, sizeof(buf))) > 0)
    write(1, buf, n);
  if (n < 0) {
    printf(1, "cat: read error\n");
  }
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    %eax,-0x94(%rbp)
cmp    $0x0,%eax
jle    1181 <cat+0x51>
lea    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
mov    $0x1,%edi
mov    $0x0,%al
call   1030 <write@plt>
jmp    113e <cat+0xe>
cmpl   $0x0,-0x94(%rbp)
jge    11a1 <cat+0x71>
mov    $0x1,%edi
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %rbx
sub    $0x88,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x80,%edx
xor    %eax,%eax
call   1050 <read@plt>
test   %eax,%eax
jle    1186 <cat+0x56>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    $0x80,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <read@plt>
test   %eax,%eax
jg     1160 <cat+0x30>
test   %eax,%eax
jns    119d <cat+0x6d>
lea    0xe6f(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x88,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"cat: read error\0A\00"
define void @cat(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %10, %1
%6 = load i32, i32* %2
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %6, i8* %7, i64 128)
store i32 %8, i32* %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* %4
%13 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 1, i8* %11, i32 %12)
br label %5
14: ; preds = %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* inttoptr (i64 1 to i8*), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"cat: read error\0A\00"
define void @cat(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %0, i8* %3, i64 128)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1, %6
%7 = phi i32 [ %9, %6 ], [ %4, %1 ]
%8 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 1, i8* %3, i32 %7)
%9 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i64, ...)*)(i32 %0, i8* %3, i64 128)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %6, label %11
11: ; preds = %6, %1
%12 = phi i32 [ %4, %1 ], [ %9, %6 ]
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* inttoptr (i64 1 to i8*), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
ret void
}