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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ send(int, char *, size_t, int /*<<< orphan*/);
size_t strlen(char *);
void read_file(int sockfd, uint8_t *name) {
  FILE *fd;
  size_t bytes_read;
  uint8_t buf[128];
  fd = fopen(name, "r");
  if (NULL == fd) {
    printf("Error: %s\n", "Unable to open file.");
    send(sockfd, "Unable to open file.", strlen("Unable to open file."), 0);
    return;
  }
  memset(buf, 0, sizeof(buf));
  while (1) {
    bytes_read = fread(buf, 1, sizeof(buf), fd);
    if (0 == bytes_read) {
      break;
    }
    send(sockfd, buf, bytes_read, 0);
  }
  fclose(fd);
  return;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe73(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11e6 <read_file+0x76>
lea    0xe59(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1070 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    0xe48(%rip),%rdi
call   1050 <strlen@plt>
mov    -0xa4(%rbp),%edi
mov    %rax,%rdx
lea    0xe33(%rip),%rsi
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    124b <read_file+0xdb>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x80,%edx
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1228 <read_file+0xb8>
jmp    1242 <read_file+0xd2>
mov    -0x4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    11f9 <read_file+0x89>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%rax
mov    %edi,%r14d
lea    0xe88(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11fe <read_file+0x9e>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11ea <read_file+0x8a>
mov    %rsp,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11c0 <read_file+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdfd(%rip),%rdi
lea    0xe01(%rip),%rbx
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    $0x14,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <send@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [11 x i8] c"Error: %s\0A\00"
@.str.2 = [21 x i8] c"Unable to open file.\00"
define void @read_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca [128 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp eq i32* null, %10
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* %3
%15 = call i64 @strlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @send(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %15, i32 0)
br label %35
17: ; preds = %2
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 0, i32 128)
br label %20
20: ; preds = %17, %27
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %5
%23 = call i64 @fread(i8* %21, i32 1, i32 128, i32* %22)
store i64 %23, i64* %6
%24 = load i64, i64* %6
%25 = icmp eq i64 0, %24
br i1 %25, label %26, label %27
26: ; preds = %20
br label %32
27: ; preds = %20
%28 = load i32, i32* %3
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%30 = load i64, i64* %6
%31 = call i32 @send(i32 %28, i8* %29, i64 %30, i32 0)
br label %20
32: ; preds = %26
%33 = load i32*, i32** %5
%34 = call i32 @fclose(i32* %33)
br label %35
35: ; preds = %32, %12
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [11 x i8] c"Error: %s\0A\00"
@.str.2 = [21 x i8] c"Unable to open file.\00"
define void @read_file(i32 %0, i8* nocapture %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @send(i32 %0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 20, i32 0)
br label %21
10: ; preds = %2
%11 = call i32 @memset(i8* %4, i32 0, i32 128)
%12 = call i64 @fread(i8* %4, i32 1, i32 128, i32* %5)
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %10, %14
%15 = phi i64 [ %17, %14 ], [ %12, %10 ]
%16 = call i32 @send(i32 %0, i8* %4, i64 %15, i32 0)
%17 = call i64 @fread(i8* %4, i32 1, i32 128, i32* %5)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %14
19: ; preds = %14, %10
%20 = call i32 @fclose(i32* %5)
br label %21
21: ; preds = %19, %7
ret void
}