#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 */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ send(int, char *, size_t, int /*<<< orphan*/);
void cat(int client_sock, char *file_path) {
  FILE *fp;
  int content_length;
  char *buffer;
  int send_length;
  send_length = 0;
  fp = fopen(file_path, "r");
  if (fp == NULL) {
    printf("%d", 116);
  }
  fseek(fp, 0, SEEK_END);
  content_length = (int)ftell(fp);
  rewind(fp);
  buffer = (char *)malloc(512 * sizeof(char));
  while (send_length < content_length) {
    size_t siz;
    if (content_length < 512)
      siz = (size_t)content_length;
    else
      siz = (size_t)512;
    fread(buffer, 1, siz, fp);
    send(client_sock, buffer, siz, 0);
    send_length += siz;
    content_length -= ftell(fp);
  }
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c6 <cat+0x46>
lea    0xe46(%rip),%rdi
mov    $0x74,%esi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    127c <cat+0xfc>
cmpl   $0x200,-0x1c(%rbp)
jge    1223 <cat+0xa3>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    122b <cat+0xab>
movq   $0x200,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    -0x38(%rbp),%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
jmp    11fd <cat+0x7d>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %edi,%r14d
lea    0xe68(%rip),%rsi
mov    %rax,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b9 <cat+0x39>
lea    0xe53(%rip),%rdi
mov    $0x74,%esi
call   1050 <printf@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
xor    %r13d,%r13d
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1060 <rewind@plt>
mov    $0x200,%edi
call   1080 <malloc@plt>
test   %ebp,%ebp
jle    1248 <cat+0xc8>
mov    %rax,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x200,%ebp
movslq %ebp,%rbp
mov    $0x200,%ebx
cmovl  %rbp,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1040 <send@plt>
add    %ebx,%r13d
mov    %r15,%rdi
call   1070 <ftell@plt>
sub    %rax,%rbp
cmp    %ebp,%r13d
jl     1200 <cat+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@SEEK_END = global i32 0
define void @cat(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %8
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 116)
br label %16
16: ; preds = %14, %2
%17 = load i32*, i32** %5
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %5
%21 = call i64 @ftell(i32* %20)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %6
%23 = load i32*, i32** %5
%24 = call i32 @rewind(i32* %23)
%25 = call i64 @malloc(i32 512)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %7
br label %27
27: ; preds = %38, %16
%28 = load i32, i32* %8
%29 = load i32, i32* %6
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %58
31: ; preds = %27
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 512
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
store i64 %36, i64* %9
br label %38
37: ; preds = %31
store i64 512, i64* %9
br label %38
38: ; preds = %37, %34
%39 = load i8*, i8** %7
%40 = load i64, i64* %9
%41 = load i32*, i32** %5
%42 = call i32 @fread(i8* %39, i32 1, i64 %40, i32* %41)
%43 = load i32, i32* %3
%44 = load i8*, i8** %7
%45 = load i64, i64* %9
%46 = call i32 @send(i32 %43, i8* %44, i64 %45, i32 0)
%47 = load i64, i64* %9
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = add i64 %49, %47
%51 = trunc i64 %50 to i32
store i32 %51, i32* %8
%52 = load i32*, i32** %5
%53 = call i64 @ftell(i32* %52)
%54 = load i32, i32* %6
%55 = sext i32 %54 to i64
%56 = sub nsw i64 %55, %53
%57 = trunc i64 %56 to i32
store i32 %57, i32* %6
br label %27
58: ; preds = %27
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@SEEK_END = global i32 0
define void @cat(i32 %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 116)
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
%11 = tail call i32 @rewind(i32* %3)
%12 = tail call i64 @malloc(i32 512)
%13 = inttoptr i64 %12 to i8*
%14 = trunc i64 %10 to i32
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %7, %16
%17 = phi i32 [ %30, %16 ], [ %14, %7 ]
%18 = phi i32 [ %27, %16 ], [ 0, %7 ]
%19 = phi i64 [ %29, %16 ], [ %10, %7 ]
%20 = icmp slt i32 %17, 512
%21 = shl i64 %19, 32
%22 = ashr exact i64 %21, 32
%23 = select i1 %20, i64 %22, i64 512
%24 = tail call i32 @fread(i8* %13, i32 1, i64 %23, i32* %3)
%25 = tail call i32 @send(i32 %0, i8* %13, i64 %23, i32 0)
%26 = trunc i64 %23 to i32
%27 = add i32 %18, %26
%28 = tail call i64 @ftell(i32* %3)
%29 = sub nsw i64 %22, %28
%30 = trunc i64 %29 to i32
%31 = icmp slt i32 %27, %30
br i1 %31, label %16, label %32
32: ; preds = %16, %7
ret void
}