#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_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int server, client, fd, count;
  int buflen = 1024;
  char *buf = malloc(buflen);
  char fname[256];
  struct sockaddr_in address;
  server = socket(AF_INET, SOCK_STREAM, 0);
  if (server < 0) {
    printf("Couldnot create socket");
    return -1;
  }
  printf("The socket was created\n");
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons(15000);
  printf("Binding Socket\n");
  if (bind(server, (struct sockaddr *)&address, sizeof(address)) != 0) {
    printf("Error binding address to server");
    return -1;
  }
  printf("Done\n");
  listen(server, 3);
  printf("Server started\n");
  int len = sizeof(address);
  client = accept(server, (struct sockaddr *)&address, &len);
  if (client > 0) {
    printf("Client connected\n");
    recv(client, fname, 255, 0);
    if ((fd = open(fname, O_RDONLY)) < 0) {
      perror("File Open Failed");
      return 0;
    }
    while ((count = read(fd, buf, buflen)) > 0) {
      send(client, buf, buflen, 0);
    }
    printf("Data sent\n");
    close(client);
    close(server);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x400,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1233 <main+0x63>
lea    0xdde(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <main+0x1e5>
lea    0xddd(%rip),%rdi
call   1060 <printf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    $0x3a98,%edi
call   1040 <htons@plt>
mov    %eax,-0x130(%rbp)
lea    0xdbb(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
je     12af <main+0xdf>
lea    0xda1(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <main+0x1e5>
lea    0xda9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x3,%esi
call   10a0 <listen@plt>
lea    0xd96(%rip),%rdi
call   1060 <printf@plt>
movl   $0xc,-0x134(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x130(%rbp),%rsi
lea    -0x134(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    13ae <main+0x1de>
lea    0xd6d(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    -0x120(%rbp),%rdi
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    135c <main+0x18c>
lea    0xd3c(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13b5 <main+0x1e5>
jmp    1361 <main+0x191>
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1392 <main+0x1c2>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
jmp    1361 <main+0x191>
lea    0xcff(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x128,%rsp
mov    $0x400,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    0x2de7(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1275 <main+0x95>
mov    %eax,%r14d
lea    0xe26(%rip),%rdi
call   1040 <puts@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x3a98,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0xe0f(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
je     128b <main+0xab>
lea    0xda4(%rip),%rdi
jmp    127c <main+0x9c>
lea    0xd84(%rip),%rdi
call   1070 <printf@plt>
mov    $0xffffffff,%eax
jmp    1355 <main+0x175>
lea    0xddc(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
mov    $0x3,%esi
call   10b0 <listen@plt>
lea    0xdc8(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10f0 <accept@plt>
test   %eax,%eax
jle    1353 <main+0x173>
mov    %eax,%r15d
lea    0xda6(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%rbp
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10d0 <open@plt>
test   %eax,%eax
js     1363 <main+0x183>
mov    %eax,%ebp
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    1337 <main+0x157>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    1310 <main+0x130>
lea    0xd55(%rip),%rdi
call   1040 <puts@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xccd(%rip),%rdi
call   10e0 <perror@plt>
jmp    1353 <main+0x173>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Couldnot create socket\00"
@.str.1 = [24 x i8] c"The socket was created\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [16 x i8] c"Binding Socket\0A\00"
@.str.3 = [32 x i8] c"Error binding address to server\00"
@.str.4 = [6 x i8] c"Done\0A\00"
@.str.5 = [16 x i8] c"Server started\0A\00"
@.str.6 = [18 x i8] c"Client connected\0A\00"
@O_RDONLY = global i32 0
@.str.7 = [17 x i8] c"File Open Failed\00"
@.str.8 = [11 x i8] c"Data sent\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i32 0, i32* %1
store i32 1024, i32* %6
%11 = load i32, i32* %6
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %7
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %76
20: ; preds = %0
%21 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 15000)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %27, i32* %28
%29 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* %2
%31 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %20
%35 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %76
36: ; preds = %20
%37 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* %2
%39 = call i32 @listen(i32 %38, i32 3)
%40 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
store i32 12, i32* %10
%41 = load i32, i32* %2
%42 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%43 = call i32 @accept(i32 %41, %struct.sockaddr* %42, i32* %10)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %75
46: ; preds = %36
%47 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* %3
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%50 = call i32 @recv(i32 %48, i8* %49, i32 255, i32 0)
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%52 = load i32, i32* @O_RDONLY
%53 = call i32 @open(i8* %51, i32 %52)
store i32 %53, i32* %4
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %46
%56 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %76
57: ; preds = %46
br label %58
58: ; preds = %64, %57
%59 = load i32, i32* %4
%60 = load i8*, i8** %7
%61 = load i32, i32* %6
%62 = call i32 @read(i32 %59, i8* %60, i32 %61)
store i32 %62, i32* %5
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %58
%65 = load i32, i32* %3
%66 = load i8*, i8** %7
%67 = load i32, i32* %6
%68 = call i32 @send(i32 %65, i8* %66, i32 %67, i32 0)
br label %58
69: ; preds = %58
%70 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%71 = load i32, i32* %3
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %2
%74 = call i32 @close(i32 %73)
br label %75
75: ; preds = %69, %36
store i32 0, i32* %1
br label %76
76: ; preds = %75, %55, %34, %18
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Couldnot create socket\00"
@INADDR_ANY = global i32 0
@.str.3 = [32 x i8] c"Error binding address to server\00"
@O_RDONLY = global i32 0
@.str.7 = [17 x i8] c"File Open Failed\00"
@str = [23 x i8] c"The socket was created\00"
@str.9 = [15 x i8] c"Binding Socket\00"
@str.10 = [5 x i8] c"Done\00"
@str.11 = [15 x i8] c"Server started\00"
@str.12 = [17 x i8] c"Client connected\00"
@str.13 = [10 x i8] c"Data sent\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %54
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 15000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i64 @bind(i32 %9, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %13
%26 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %54
27: ; preds = %13
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.10, i64 0, i64 0))
%29 = call i32 @listen(i32 %9, i32 3)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.11, i64 0, i64 0))
%31 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%32 = call i32 @accept(i32 %9, %struct.sockaddr* %22, i32* %3)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %53
34: ; preds = %27
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.12, i64 0, i64 0))
%36 = call i32 @recv(i32 %32, i8* %5, i32 255, i32 0)
%37 = load i32, i32* @O_RDONLY
%38 = call i32 @open(i8* %5, i32 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %34
%41 = call i32 @read(i32 %38, i8* %4, i32 1024)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %45, label %49
43: ; preds = %34
%44 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %53
45: ; preds = %40, %45
%46 = call i32 @send(i32 %32, i8* %4, i32 1024, i32 0)
%47 = call i32 @read(i32 %38, i8* %4, i32 1024)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %45, label %49
49: ; preds = %45, %40
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.13, i64 0, i64 0))
%51 = call i32 @close(i32 %32)
%52 = call i32 @close(i32 %9)
br label %53
53: ; preds = %27, %49, %43
br label %54
54: ; preds = %53, %25, %11
%55 = phi i32 [ -1, %11 ], [ -1, %25 ], [ 0, %53 ]
ret i32 %55
}