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

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
void catbyline(FILE *input, char *filename) {
  char buffer[1024];
  printf("FILE %s:\n", filename);
  for (int linenr = 1;; ++linenr) {
    char *buf = fgets(buffer, sizeof buffer, input);
    if (buf == NULL)
      break;
    printf("%6d: %s", linenr, buf);
  }
}


x86 assembly (clang, -O0):
<catbyline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xec2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x414(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    117e <catbyline+0x5e>
jmp    11ad <catbyline+0x8d>
mov    -0x414(%rbp),%esi
mov    -0x420(%rbp),%rdx
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    114f <catbyline+0x2f>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<catbyline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
lea    0xec9(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1197 <catbyline+0x77>
mov    $0x1,%ebp
lea    0xeab(%rip),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%ebp
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1170 <catbyline+0x50>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"FILE %s:\0A\00"
@.str.1 = [8 x i8] c"%6d: %s\00"
define void @catbyline(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i8*
store i32* %0, i32** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %8)
store i32 1, i32* %6
br label %10
10: ; preds = %21, %2
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%12 = load i32*, i32** %3
%13 = call i8* @fgets(i8* %11, i32 1024, i32* %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %10
br label %24
17: ; preds = %10
%18 = load i32, i32* %6
%19 = load i8*, i8** %7
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %18, i8* %19)
br label %21
21: ; preds = %17
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
br label %10
24: ; preds = %16
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"FILE %s:\0A\00"
@.str.1 = [8 x i8] c"%6d: %s\00"
define void @catbyline(i32* nocapture %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = call i8* @fgets(i8* %4, i32 1024, i32* %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %15, label %8
8: ; preds = %2, %8
%9 = phi i8* [ %13, %8 ], [ %6, %2 ]
%10 = phi i32 [ %12, %8 ], [ 1, %2 ]
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* %9)
%12 = add nuw nsw i32 %10, 1
%13 = call i8* @fgets(i8* %4, i32 1024, i32* %0)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %8
15: ; preds = %8, %2
ret void
}