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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void print_file(int connfd, const char *filename) {
  printf("reading file: [%s]\n", filename);
  int filefd = open(filename, O_RDONLY);
  if (filefd <= 0) {
    printf("File not found\n");
  }
  char buf[100];
  ssize_t n;
  while ((n = read(filefd, buf, 100)) > 0) {
    printf(".");
    write(connfd, buf, n);
  }
  printf("\n");
  close(filefd);
}


x86 assembly (clang, -O0):
<print_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11a1 <print_file+0x51>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a6 <print_file+0x56>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x84(%rbp)
cmp    $0x0,%eax
jle    11eb <print_file+0x9b>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x84(%rbp),%edx
call   1030 <write@plt>
jmp    11a6 <print_file+0x56>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xe79(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jg     11b2 <print_file+0x42>
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x64,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    11fd <print_file+0x8d>
mov    %eax,%ebp
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11d0 <print_file+0x60>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%edi
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <close@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"reading file: [%s]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"File not found\0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @print_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
br label %18
18: ; preds = %23, %17
%19 = load i32, i32* %5
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 100)
store i32 %21, i32* %7
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %3
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%27 = load i32, i32* %7
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
br label %18
29: ; preds = %18
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"reading file: [%s]\0A\00"
@O_RDONLY = global i32 0
@str = [15 x i8] c"File not found\00"
define void @print_file(i32 %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %1, i32 %5)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%12 = call i32 @read(i32 %6, i8* %11, i32 100)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10, %14
%15 = phi i32 [ %18, %14 ], [ %12, %10 ]
%16 = tail call i32 @putchar(i32 46)
%17 = call i32 @write(i32 %0, i8* %11, i32 %15)
%18 = call i32 @read(i32 %6, i8* %11, i32 100)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %14, label %20
20: ; preds = %14, %10
%21 = tail call i32 @putchar(i32 10)
%22 = tail call i32 @close(i32 %6)
ret void
}