#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
char *basename(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sendfile(int /*<<< orphan*/ *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    perror("usage:send_file filepath <IPaddress>");
    exit(1);
  }
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("Can't allocate sockfd");
    exit(1);
  }
  struct sockaddr_in serveraddr;
  memset(&serveraddr, 0, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_port = htons(8877);
  if (inet_pton(AF_INET, argv[2], &serveraddr.sin_addr) < 0) {
    perror("IPaddress Convert Error");
    exit(1);
  }
  if (connect(sockfd, (const struct sockaddr *)&serveraddr,
              sizeof(serveraddr)) < 0) {
    perror("Connect Error");
    exit(1);
  }
  char *filename = basename(argv[1]);
  if (filename == NULL) {
    perror("Can't get filename");
    exit(1);
  }
  char buff[4096] = {0};
  strncpy(buff, filename, strlen(filename));
  if (send(sockfd, buff, 4096, 0) == -1) {
    perror("Can't send filename");
    exit(1);
  }
  FILE *fp = fopen(argv[1], "rb");
  if (fp == NULL) {
    perror("Can't open file");
    exit(1);
  }
  sendfile(fp, sockfd);
  puts("Send Success");
  fclose(fp);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1239 <main+0x39>
lea    0xdd6(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1275 <main+0x75>
lea    0xdbf(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x22ad,%edi
call   1070 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   10c0 <inet_pton@plt>
cmp    $0x0,%rax
jge    12d8 <main+0xd8>
lea    0xd72(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jge    1309 <main+0x109>
lea    0xd59(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <basename@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    133b <main+0x13b>
lea    0xd35(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1090 <memset@plt>
lea    -0x1030(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x1048(%rbp),%rdi
mov    -0x1040(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
jne    13ba <main+0x1ba>
lea    0xcc9(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xcbf(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jne    13f9 <main+0x1f9>
lea    0xca1(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x1038(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10d0 <sendfile@plt>
lea    0xc8c(%rip),%rdi
call   1040 <puts@plt>
mov    -0x1038(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x14(%rbp),%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
cmp    $0x3,%edi
jne    133a <main+0x13a>
mov    %rsi,%r14
mov    0x2dc8(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
test   %eax,%eax
js     1343 <main+0x143>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x22ad,%edi
call   1070 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
mov    0x10(%r14),%rsi
mov    %rbx,%rdx
call   10c0 <inet_pton@plt>
test   %rax,%rax
js     134c <main+0x14c>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   1110 <connect@plt>
test   %rax,%rax
js     1355 <main+0x155>
mov    0x8(%r14),%rdi
call   10b0 <basename@plt>
test   %rax,%rax
je     135e <main+0x15e>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
mov    $0x1000,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <memset@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
je     1367 <main+0x167>
mov    0x8(%r14),%rdi
lea    0xd92(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1370 <main+0x170>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15d,%esi
call   10d0 <sendfile@plt>
lea    0xd86(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcbf(%rip),%rdi
jmp    1377 <main+0x177>
lea    0xcdb(%rip),%rdi
jmp    1377 <main+0x177>
lea    0xce8(%rip),%rdi
jmp    1377 <main+0x177>
lea    0xcf7(%rip),%rdi
jmp    1377 <main+0x177>
lea    0xcfc(%rip),%rdi
jmp    1377 <main+0x177>
lea    0xd06(%rip),%rdi
jmp    1377 <main+0x177>
lea    0xd14(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [37 x i8] c"usage:send_file filepath <IPaddress>\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Can't allocate sockfd\00"
@.str.2 = [24 x i8] c"IPaddress Convert Error\00"
@.str.3 = [14 x i8] c"Connect Error\00"
@.str.4 = [19 x i8] c"Can't get filename\00"
@.str.5 = [20 x i8] c"Can't send filename\00"
@.str.6 = [3 x i8] c"rb\00"
@.str.7 = [16 x i8] c"Can't open file\00"
@.str.8 = [13 x i8] c"Send Success\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i8*
%9 = alloca [4096 x i8]
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 8877)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @AF_INET
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%36 = call i64 @inet_pton(i32 %31, i8* %34, i32* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %25
%39 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %25
%42 = load i32, i32* %6
%43 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%44 = call i64 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call i8* @basename(i8* %52)
store i8* %53, i8** %8
%54 = load i8*, i8** %8
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %59
56: ; preds = %49
%57 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %49
%60 = bitcast [4096 x i8]* %9 to i8*
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%62 = load i8*, i8** %8
%63 = load i8*, i8** %8
%64 = call i32 @strlen(i8* %63)
%65 = call i32 @strncpy(i8* %61, i8* %62, i32 %64)
%66 = load i32, i32* %6
%67 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%68 = call i32 @send(i32 %66, i8* %67, i32 4096, i32 0)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %59
%71 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %59
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = call i32* @fopen(i8* %76, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %77, i32** %10
%78 = load i32*, i32** %10
%79 = icmp eq i32* %78, null
br i1 %79, label %80, label %83
80: ; preds = %73
%81 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %73
%84 = load i32*, i32** %10
%85 = load i32, i32* %6
%86 = call i32 @sendfile(i32* %84, i32 %85)
%87 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%88 = load i32*, i32** %10
%89 = call i32 @fclose(i32* %88)
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [37 x i8] c"usage:send_file filepath <IPaddress>\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Can't allocate sockfd\00"
@.str.2 = [24 x i8] c"IPaddress Convert Error\00"
@.str.3 = [14 x i8] c"Connect Error\00"
@.str.4 = [19 x i8] c"Can't get filename\00"
@.str.5 = [20 x i8] c"Can't send filename\00"
@.str.6 = [3 x i8] c"rb\00"
@.str.7 = [16 x i8] c"Can't open file\00"
@.str.8 = [13 x i8] c"Send Success\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [4096 x i8]
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = bitcast %struct.sockaddr_in* %3 to i8*
%19 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 8877)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%28 = call i64 @inet_pton(i32 %24, i8* %26, i32* %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %17
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i64 @connect(i32 %12, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = getelementptr inbounds i8*, i8** %1, i64 1
%42 = load i8*, i8** %41
%43 = call i8* @basename(i8* %42)
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %40
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%50 = call i32 @strlen(i8* %43)
%51 = call i32 @strncpy(i8* %49, i8* %43, i32 %50)
%52 = call i32 @send(i32 %12, i8* %49, i32 4096, i32 0)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %48
%58 = load i8*, i8** %41
%59 = call i32* @fopen(i8* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%60 = icmp eq i32* %59, null
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %57
%65 = call i32 @sendfile(i32* %59, i32 %12)
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%67 = call i32 @fclose(i32* %59)
%68 = call i32 @close(i32 %12)
ret i32 0
}