#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(int /*<<< orphan*/, char, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int sendFile(char *address, int port, char *filename) {
  int clientSocket;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;
  FILE *fp;
  fp = fopen(filename, "r");
  clientSocket = socket(AF_INET, SOCK_STREAM, 0);
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(port);
  serverAddr.sin_addr.s_addr = inet_addr(address);
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
  addr_size = sizeof serverAddr;
  int result = connect(clientSocket, (struct sockaddr *)&serverAddr, addr_size);
  if (1)
    printf("Connecting to receiving handler: %d\n", result);
  send(clientSocket, filename, strlen(filename) + 1, 0);
  int block = 0;
  while ((block = fread(buffer, sizeof(char), 128, fp)) > 0) {
    send(clientSocket, buffer, block, 0);
    bzero(buffer, 128);
  }
  close(clientSocket);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<sendFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x440(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x428(%rbp)
mov    -0x8(%rbp),%rdi
call   10b0 <inet_addr@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x430(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1090 <memset@plt>
movl   $0x10,-0x434(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x434(%rbp),%edx
call   10e0 <connect@plt>
mov    %eax,-0x444(%rbp)
mov    -0x444(%rbp),%esi
lea    0xd79(%rip),%rdi
call   1080 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x454(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
movl   $0x0,-0x448(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x440(%rbp),%rcx
mov    $0x1,%esi
mov    $0x80,%edx
call   1030 <fread@plt>
mov    %eax,-0x448(%rbp)
cmp    $0x0,%eax
jle    1327 <sendFile+0x157>
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x448(%rbp),%edx
xor    %ecx,%ecx
call   1070 <send@plt>
lea    -0x420(%rbp),%rdi
mov    $0x80,%esi
call   10d0 <bzero@plt>
jmp    12ce <sendFile+0xfe>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x440(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r12
lea    0xe10(%rip),%rsi
mov    %rdx,%rdi
call   10c0 <fopen@plt>
mov    %rax,%r15
mov    0x2de6(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
mov    0x0(%r13),%eax
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1060 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r12,%rdi
call   10b0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
xor    %esi,%esi
mov    $0x4,%edx
call   1090 <memset@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   10e0 <connect@plt>
lea    0xda9(%rip),%rdi
mov    %eax,%esi
call   1080 <printf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebp,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
call   1070 <send@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    12d4 <sendFile+0x104>
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %rbx,%rdi
mov    $0x80,%esi
call   10d0 <bzero@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     12a0 <sendFile+0xd0>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c"r\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [37 x i8] c"Connecting to receiving handler: %d\0A\00"
define i32 @sendFile(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %11
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %7
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 3
store i32 %19, i32* %20
%21 = load i32, i32* %5
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i8*, i8** %4
%25 = call i32 @inet_addr(i8* %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @memset(i32 %29, i8 signext 0, i32 4)
store i32 16, i32* %10
%31 = load i32, i32* %7
%32 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%33 = load i32, i32* %10
%34 = call i32 @connect(i32 %31, %struct.sockaddr* %32, i32 %33)
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %7
%38 = load i8*, i8** %6
%39 = load i8*, i8** %6
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %40, 1
%42 = call i32 @send(i32 %37, i8* %38, i32 %41, i32 0)
store i32 0, i32* %13
br label %43
43: ; preds = %48, %3
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%45 = load i32*, i32** %11
%46 = call i32 @fread(i8* %44, i32 1, i32 128, i32* %45)
store i32 %46, i32* %13
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load i32, i32* %7
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%51 = load i32, i32* %13
%52 = call i32 @send(i32 %49, i8* %50, i32 %51, i32 0)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%54 = call i32 @bzero(i8* %53, i32 128)
br label %43
55: ; preds = %43
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
%58 = load i32*, i32** %11
%59 = call i32 @fclose(i32* %58)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c"r\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [37 x i8] c"Connecting to receiving handler: %d\0A\00"
define i32 @sendFile(i8* %0, i32 %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 3
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @inet_addr(i8* %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @memset(i32 undef, i8 signext 0, i32 4)
%19 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%20 = call i32 @connect(i32 %11, %struct.sockaddr* %19, i32 16)
%21 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 @strlen(i8* %2)
%23 = add nsw i32 %22, 1
%24 = call i32 @send(i32 %11, i8* %2, i32 %23, i32 0)
%25 = call i32 @fread(i8* %6, i32 1, i32 128, i32* %8)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %3, %27
%28 = phi i32 [ %31, %27 ], [ %25, %3 ]
%29 = call i32 @send(i32 %11, i8* %6, i32 %28, i32 0)
%30 = call i32 @bzero(i8* %6, i32 128)
%31 = call i32 @fread(i8* %6, i32 1, i32 128, i32* %8)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %27, label %33
33: ; preds = %27, %3
%34 = call i32 @close(i32 %11)
%35 = call i32 @fclose(i32* %8)
ret i32 0
}