#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  FILE *fp;
  char *line = NULL;
  size_t len = 0;
  ssize_t read;
  fp = fopen("/etc/motd", "r");
  if (fp == NULL)
    exit(EXIT_FAILURE);
  while ((read = getline(&line, &len, fp)) != -1) {
    printf("Retrieved line of length %zu (n=%zu):\n", read, len);
    printf("%s", line);
  }
  if (line)
    free(line);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119f <main+0x4f>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11a4 <main+0x54>
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     11ed <main+0x9d>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a4 <main+0x54>
cmpq   $0x0,-0x18(%rbp)
je     1201 <main+0xb1>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe8b(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1217 <main+0xc7>
mov    %rax,%rbx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   1060 <getline@plt>
cmp    $0xffffffff,%eax
je     11f2 <main+0xa2>
lea    0xe63(%rip),%r14
lea    0xe83(%rip),%r15
mov    %rsp,%r12
lea    0x8(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <getline@plt>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x70>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1200 <main+0xb0>
call   1030 <free@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/etc/motd\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [39 x i8] c"Retrieved line of length %zu (n=%zu):\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
store i8* null, i8** %3
store i64 0, i64* %4
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
br label %13
13: ; preds = %17, %12
%14 = load i32*, i32** %2
%15 = call i32 @getline(i8** %3, i64* %4, i32* %14)
store i32 %15, i32* %5
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* %5
%19 = load i64, i64* %4
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %18, i64 %19)
%21 = load i8*, i8** %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %21)
br label %13
23: ; preds = %13
%24 = load i8*, i8** %3
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i8*, i8** %3
%28 = call i32 @free(i8* %27)
br label %29
29: ; preds = %26, %23
%30 = load i32, i32* @EXIT_SUCCESS
ret i32 %30
}

LLVM IR (-O2):
@.str = [10 x i8] c"/etc/motd\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [39 x i8] c"Retrieved line of length %zu (n=%zu):\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i8*
%2 = alloca i64
%3 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @getline(i8** %1, i64* %2, i32* %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %21, label %13
10: ; preds = %0
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %7, %13
%14 = phi i32 [ %19, %13 ], [ %8, %7 ]
%15 = load i64, i64* %2
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %14, i64 %15)
%17 = load i8*, i8** %1
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = call i32 @getline(i8** %1, i64* %2, i32* %5)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %13
21: ; preds = %13, %7
%22 = load i8*, i8** %1
%23 = icmp eq i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @free(i8* %22)
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* @EXIT_SUCCESS
ret i32 %27
}