#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 */
struct stat {
  char *st_size;
};
typedef char *off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fstat(int, struct stat *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ send_msg(int, char *);
int sendfile(int, int, int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ snprintf(char *, int const, char *, char *, char *, ...);
int strlen(char *);
int system(char *);
void send_fs(char *compression, char *target_name, char *target, char *exclude,
             char *to_tar, int clientfd) {
  remove(target_name);
  if (to_tar == NULL) {
    to_tar = ".";
  }
  if (compression == NULL) {
    compression = "";
  }
  if (exclude == NULL) {
    exclude = "";
  }
  char *cmd = "tar -C %s %s -c %s %s -f %s";
  int cmd_length = strlen(cmd) + strlen(target_name) + strlen(exclude) +
                   strlen(target) + strlen(compression) + strlen(to_tar);
  char *formatted_cmd = malloc(cmd_length);
  snprintf(formatted_cmd, cmd_length, cmd, target, exclude, to_tar, compression,
           target_name);
  printf("EXEC: %s\n", formatted_cmd);
  int ret = system(formatted_cmd);
  if (ret != 0) {
    perror("Error: Unable to create filesystem archive");
  }
  free(formatted_cmd);
  const int buff_size = 1000;
  char buffer[buff_size];
  char final_buffer[buff_size];
  char *fmt_str = "Size: %s, Filename: %s";
  off_t nbytes;
  snprintf(buffer, buff_size, fmt_str, "%lu", "%s");
  int fd = open(target_name, O_RDONLY);
  struct stat st;
  fstat(fd, &st);
  nbytes = st.st_size;
  snprintf(final_buffer, buff_size, buffer, nbytes, target_name);
  send_msg(clientfd, final_buffer);
  int n_sent = sendfile(clientfd, fd, NULL, nbytes);
  printf("Sent %d/%zd bytes successfully\n", n_sent, nbytes);
  close(fd);
  remove(target_name);
}


x86 assembly (clang, -O0):
<send_fs>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1212 <send_fs+0x42>
lea    0xdf2(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1228 <send_fs+0x58>
lea    0xe03(%rip),%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123e <send_fs+0x6e>
lea    0xded(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xdbd(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x90(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x90(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x8c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x8c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x88(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x88(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x84(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x84(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x80(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x80(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
mov    -0x8(%rbp),%r10
mov    -0x10(%rbp),%rax
sub    $0x10,%rsp
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
mov    $0x0,%al
call   1080 <snprintf@plt>
add    $0x10,%rsp
mov    -0x48(%rbp),%rsi
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1060 <system@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1343 <send_fs+0x173>
lea    0xcea(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x3e8,-0x50(%rbp)
mov    %rsp,%rax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rdi
add    $0xfffffffffffffc10,%rdi
mov    %rdi,-0xa0(%rbp)
mov    %rdi,%rsp
mov    %rsp,%rax
add    $0xfffffffffffffc10,%rax
mov    %rax,-0x98(%rbp)
mov    %rax,%rsp
lea    0xccb(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdx
mov    $0x3e8,%esi
lea    0xcce(%rip),%rcx
lea    0xcc4(%rip),%r8
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2c33(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x6c(%rbp),%edi
lea    -0x78(%rbp),%rsi
call   10f0 <fstat@plt>
mov    -0xa0(%rbp),%rdx
mov    -0x98(%rbp),%rdi
mov    -0x78(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x3e8,%esi
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    -0x98(%rbp),%rsi
mov    -0x2c(%rbp),%edi
call   10e0 <send_msg@plt>
mov    -0x2c(%rbp),%edi
mov    -0x6c(%rbp),%esi
mov    -0x68(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <sendfile@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x7c(%rbp),%esi
mov    -0x68(%rbp),%rdx
lea    0xc46(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x6c(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fs>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9d,-0x2c(%rbp)
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdx,-0x48(%rbp)
mov    %rsi,-0x38(%rbp)
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <remove@plt>
test   %rbx,%rbx
lea    0xdf5(%rip),%rax
cmovne %rbx,%rax
mov    %rax,-0x50(%rbp)
test   %r12,%r12
lea    0xe0a(%rip),%rax
cmove  %rax,%r12
mov    %r12,-0x40(%rbp)
test   %r14,%r14
cmove  %rax,%r14
lea    0xdcf(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
mov    -0x38(%rbp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    %eax,%r13d
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %eax,%r13d
mov    %r15,%rdi
call   1050 <strlen@plt>
add    %eax,%r13d
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %eax,%r13d
mov    -0x50(%rbp),%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %eax,%r13d
mov    %r13d,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r13d,%esi
lea    0xd70(%rip),%rdx
mov    -0x48(%rbp),%rcx
mov    %r14,%r8
mov    %r12,%r9
xor    %eax,%eax
mov    %rbx,%r13
push   %rbx
push   -0x40(%rbp)
call   1080 <snprintf@plt>
add    $0x10,%rsp
lea    0xd69(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   1060 <system@plt>
test   %eax,%eax
jne    13a0 <send_fs+0x1d0>
mov    %r15,%rdi
call   1030 <free@plt>
lea    -0x3f0(%rsp),%r12
ff
mov    %r12,%rsp
lea    -0x3f0(%rsp),%r14
ff
mov    %r14,%rsp
lea    0xd5f(%rip),%rdx
lea    0xd6f(%rip),%rcx
lea    0xd65(%rip),%r8
mov    %r12,%rdi
mov    $0x3e8,%esi
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rbx
mov    %r13,%rdi
call   10c0 <open@plt>
mov    %eax,%r15d
lea    -0x58(%rbp),%rsi
mov    %eax,%edi
call   10f0 <fstat@plt>
mov    -0x58(%rbp),%r13
mov    %r14,%rdi
mov    $0x3e8,%esi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbx,%r8
mov    %rbx,%r12
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    -0x2c(%rbp),%ebx
mov    %ebx,%edi
mov    %r14,%rsi
call   10e0 <send_msg@plt>
mov    %ebx,%edi
mov    %r15d,%esi
xor    %edx,%edx
mov    %r13,%rcx
call   10b0 <sendfile@plt>
lea    0xcf9(%rip),%rdi
mov    %eax,%esi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15d,%edi
call   1090 <close@plt>
mov    %r12,%rdi
call   1040 <remove@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc81(%rip),%rdi
call   10d0 <perror@plt>
jmp    12cf <send_fs+0xff>

LLVM IR (-O0):
%struct.stat = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [28 x i8] c"tar -C %s %s -c %s %s -f %s\00"
@.str.3 = [10 x i8] c"EXEC: %s\0A\00"
@.str.4 = [43 x i8] c"Error: Unable to create filesystem archive\00"
@.str.5 = [23 x i8] c"Size: %s, Filename: %s\00"
@.str.6 = [4 x i8] c"%lu\00"
@.str.7 = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@.str.8 = [32 x i8] c"Sent %d/%zd bytes successfully\0A\00"
define void @send_fs(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i8*
%20 = alloca i8*
%21 = alloca i32
%22 = alloca %struct.stat
%23 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i32 %5, i32* %12
%24 = load i8*, i8** %8
%25 = call i32 @remove(i8* %24)
%26 = load i8*, i8** %11
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %6
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %11
br label %29
29: ; preds = %28, %6
%30 = load i8*, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %29
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %33
33: ; preds = %32, %29
%34 = load i8*, i8** %10
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %37
36: ; preds = %33
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %37
37: ; preds = %36, %33
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8** %13
%38 = load i8*, i8** %13
%39 = call i32 @strlen(i8* %38)
%40 = load i8*, i8** %8
%41 = call i32 @strlen(i8* %40)
%42 = add nsw i32 %39, %41
%43 = load i8*, i8** %10
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %42, %44
%46 = load i8*, i8** %9
%47 = call i32 @strlen(i8* %46)
%48 = add nsw i32 %45, %47
%49 = load i8*, i8** %7
%50 = call i32 @strlen(i8* %49)
%51 = add nsw i32 %48, %50
%52 = load i8*, i8** %11
%53 = call i32 @strlen(i8* %52)
%54 = add nsw i32 %51, %53
store i32 %54, i32* %14
%55 = load i32, i32* %14
%56 = call i8* @malloc(i32 %55)
store i8* %56, i8** %15
%57 = load i8*, i8** %15
%58 = load i32, i32* %14
%59 = load i8*, i8** %13
%60 = load i8*, i8** %9
%61 = load i8*, i8** %10
%62 = load i8*, i8** %11
%63 = load i8*, i8** %7
%64 = load i8*, i8** %8
%65 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %57, i32 %58, i8* %59, i8* %60, i8* %61, i8* %62, i8* %63, i8* %64)
%66 = load i8*, i8** %15
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %66)
%68 = load i8*, i8** %15
%69 = call i32 @system(i8* %68)
store i32 %69, i32* %16
%70 = load i32, i32* %16
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %37
%73 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %74
74: ; preds = %72, %37
%75 = load i8*, i8** %15
%76 = call i32 @free(i8* %75)
store i32 1000, i32* %17
%77 = call i8* @llvm.stacksave()
store i8* %77, i8** %18
%78 = alloca i8, i64 1000
%79 = alloca i8, i64 1000
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8** %19
%80 = load i8*, i8** %19
%81 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %78, i32 1000, i8* %80, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%82 = load i8*, i8** %8
%83 = load i32, i32* @O_RDONLY
%84 = call i32 @open(i8* %82, i32 %83)
store i32 %84, i32* %21
%85 = load i32, i32* %21
%86 = call i32 @fstat(i32 %85, %struct.stat* %22)
%87 = getelementptr inbounds %struct.stat, %struct.stat* %22, i32 0, i32 0
%88 = load i8*, i8** %87
store i8* %88, i8** %20
%89 = load i8*, i8** %20
%90 = load i8*, i8** %8
%91 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %79, i32 1000, i8* %78, i8* %89, i8* %90)
%92 = load i32, i32* %12
%93 = call i32 @send_msg(i32 %92, i8* %79)
%94 = load i32, i32* %12
%95 = load i32, i32* %21
%96 = load i8*, i8** %20
%97 = call i32 @sendfile(i32 %94, i32 %95, i32* null, i8* %96)
store i32 %97, i32* %23
%98 = load i32, i32* %23
%99 = load i8*, i8** %20
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %98, i8* %99)
%101 = load i32, i32* %21
%102 = call i32 @close(i32 %101)
%103 = load i8*, i8** %8
%104 = call i32 @remove(i8* %103)
%105 = load i8*, i8** %18
ret void
}

LLVM IR (-O2):
%struct.stat = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [28 x i8] c"tar -C %s %s -c %s %s -f %s\00"
@.str.3 = [10 x i8] c"EXEC: %s\0A\00"
@.str.4 = [43 x i8] c"Error: Unable to create filesystem archive\00"
@.str.5 = [23 x i8] c"Size: %s, Filename: %s\00"
@.str.6 = [4 x i8] c"%lu\00"
@.str.7 = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@.str.8 = [32 x i8] c"Sent %d/%zd bytes successfully\0A\00"
define void @send_fs(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4, i32 %5) {
%7 = alloca %struct.stat
%8 = tail call i32 @remove(i8* %1)
%9 = icmp eq i8* %4, null
%10 = select i1 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %4
%11 = icmp eq i8* %0, null
%12 = select i1 %11, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %0
%13 = icmp eq i8* %3, null
%14 = select i1 %13, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %3
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @strlen(i8* %1)
%17 = add nsw i32 %16, %15
%18 = tail call i32 @strlen(i8* %14)
%19 = add nsw i32 %17, %18
%20 = tail call i32 @strlen(i8* %2)
%21 = add nsw i32 %19, %20
%22 = tail call i32 @strlen(i8* %12)
%23 = add nsw i32 %21, %22
%24 = tail call i32 @strlen(i8* %10)
%25 = add nsw i32 %23, %24
%26 = tail call i8* @malloc(i32 %25)
%27 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %26, i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %14, i8* %10, i8* %12, i8* %1)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%29 = tail call i32 @system(i8* %26)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %6
%32 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %6
%34 = tail call i32 @free(i8* %26)
%35 = alloca [1000 x i8]
%36 = alloca [1000 x i8]
%37 = getelementptr inbounds [1000 x i8], [1000 x i8]* %36, i64 0, i64 0
%38 = getelementptr inbounds [1000 x i8], [1000 x i8]* %35, i64 0, i64 0
%39 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %38, i32 1000, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%40 = load i32, i32* @O_RDONLY
%41 = call i32 @open(i8* %1, i32 %40)
%42 = bitcast %struct.stat* %7 to i8*
%43 = call i32 @fstat(i32 %41, %struct.stat* %7)
%44 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %37, i32 1000, i8* %38, i8* %45, i8* %1)
%47 = call i32 @send_msg(i32 %5, i8* %37)
%48 = call i32 @sendfile(i32 %5, i32 %41, i32* null, i8* %45)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %48, i8* %45)
%50 = call i32 @close(i32 %41)
%51 = call i32 @remove(i8* %1)
ret void
}