#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 */
typedef int uint;
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*/ server_address;
typedef int /*<<< orphan*/ client_address;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sockfd;
  int listenfd;
  uint client_len;
  int file_bytes = 0;
  char filename[256];
  char buffer[10];
  struct sockaddr_in server_address;
  struct sockaddr_in client_address;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    error("Error creating socket");
  }
  memset((char *)&server_address, 0, sizeof(server_address));
  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = INADDR_ANY;
  server_address.sin_port = htons(1337);
  if (bind(sockfd, (struct sockaddr *)&server_address, sizeof(server_address)) <
      0) {
    error("Error binding socket");
  }
  listen(sockfd, 5);
  puts("Server listening on localhost:1337...");
  client_len = sizeof(client_address);
  listenfd = accept(sockfd, (struct sockaddr *)&client_address, &client_len);
  if (listenfd < 0) {
    error("Error on incoming socket");
  }
  memset(buffer, 0, sizeof(buffer));
  FILE *f;
  f = fopen("server_file", "wb");
  if (f == NULL) {
    error("Error writing to file");
  }
  while ((file_bytes = recv(listenfd, buffer, 5, 0)) > 0) {
    printf("Bytes received %d\n", file_bytes);
    fwrite(buffer, 1, file_bytes, f);
    memset(buffer, 0, sizeof(buffer));
  }
  if (file_bytes < 0) {
    puts("File read error. Client connected but file could not be read.");
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    121b <main+0x4b>
lea    0xdea(%rip),%rdi
call   10b0 <error@plt>
lea    -0x138(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x130(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x134(%rbp)
mov    $0x539,%edi
call   1050 <htons@plt>
mov    %eax,-0x138(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x138(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1286 <main+0xb6>
lea    0xd95(%rip),%rdi
call   10b0 <error@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
lea    0xd91(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x148(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12d2 <main+0x102>
lea    0xd84(%rip),%rdi
call   10b0 <error@plt>
lea    -0x12a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
lea    0xd7e(%rip),%rdi
lea    0xd83(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x150(%rbp)
cmpq   $0x0,-0x150(%rbp)
jne    1319 <main+0x149>
lea    0xd65(%rip),%rdi
call   10b0 <error@plt>
jmp    131e <main+0x14e>
mov    -0xc(%rbp),%edi
lea    -0x12a(%rbp),%rsi
mov    $0x5,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1382 <main+0x1b2>
mov    -0x14(%rbp),%esi
lea    0xd45(%rip),%rdi
call   1060 <printf@plt>
lea    -0x12a(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x150(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
lea    -0x12a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
jmp    131e <main+0x14e>
cmpl   $0x0,-0x14(%rbp)
jge    1398 <main+0x1c8>
lea    0xd0f(%rip),%rdi
call   1040 <puts@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e03(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    120b <main+0x3b>
lea    0xdfa(%rip),%rdi
call   10b0 <error@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x539,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1262 <main+0x92>
lea    0xdb9(%rip),%rdi
call   10b0 <error@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
lea    0xdb5(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,0xc(%rsp)
lea    0x28(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10d0 <accept@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    12a8 <main+0xd8>
lea    0xdae(%rip),%rdi
call   10b0 <error@plt>
lea    0x1e(%rsp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
lea    0xdaa(%rip),%rdi
lea    0xdaf(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    12e0 <main+0x110>
lea    0xd9e(%rip),%rdi
call   10b0 <error@plt>
lea    0x1e(%rsp),%rsi
mov    %r12d,%edi
mov    $0x5,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1353 <main+0x183>
lea    0xd8e(%rip),%r15
lea    0x1e(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r13,%rcx
call   10e0 <fwrite@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x5,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1310 <main+0x140>
test   %ebx,%ebx
jns    1363 <main+0x193>
lea    0xd44(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x38,%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 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Error creating socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [21 x i8] c"Error binding socket\00"
@.str.2 = [38 x i8] c"Server listening on localhost:1337...\00"
@.str.3 = [25 x i8] c"Error on incoming socket\00"
@.str.4 = [12 x i8] c"server_file\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [22 x i8] c"Error writing to file\00"
@.str.7 = [19 x i8] c"Bytes received %d\0A\00"
@.str.8 = [62 x i8] c"File read error. Client connected but file could not be read.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca [10 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = bitcast %struct.sockaddr_in* %8 to i8*
%20 = call i32 @memset(i8* %19, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = call i32 @htons(i32 1337)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %18
%33 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %18
%35 = load i32, i32* %2
%36 = call i32 @listen(i32 %35, i32 5)
%37 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 12, i32* %4
%38 = load i32, i32* %2
%39 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%40 = call i32 @accept(i32 %38, %struct.sockaddr* %39, i32* %4)
store i32 %40, i32* %3
%41 = load i32, i32* %3
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %34
%46 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%47 = call i32 @memset(i8* %46, i32 0, i32 10)
%48 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %48, i32** %10
%49 = load i32*, i32** %10
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %51, %45
br label %54
54: ; preds = %59, %53
%55 = load i32, i32* %3
%56 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%57 = call i32 @recv(i32 %55, i8* %56, i32 5, i32 0)
store i32 %57, i32* %5
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %68
59: ; preds = %54
%60 = load i32, i32* %5
%61 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %60)
%62 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%63 = load i32, i32* %5
%64 = load i32*, i32** %10
%65 = call i32 @fwrite(i8* %62, i32 1, i32 %63, i32* %64)
%66 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%67 = call i32 @memset(i8* %66, i32 0, i32 10)
br label %54
68: ; preds = %54
%69 = load i32, i32* %5
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = call i32 @puts(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.8, i64 0, i64 0))
br label %73
73: ; preds = %71, %68
%74 = load i32, i32* %2
%75 = call i32 @close(i32 %74)
ret i32 0
}

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 = [22 x i8] c"Error creating socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [21 x i8] c"Error binding socket\00"
@.str.2 = [38 x i8] c"Server listening on localhost:1337...\00"
@.str.3 = [25 x i8] c"Error on incoming socket\00"
@.str.4 = [12 x i8] c"server_file\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [22 x i8] c"Error writing to file\00"
@.str.7 = [19 x i8] c"Bytes received %d\0A\00"
@.str.8 = [62 x i8] c"File read error. Client connected but file could not be read.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %1 to i8*
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = call i32 @memset(i8* %7, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 1337)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i64 @bind(i32 %11, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %15
%29 = call i32 @listen(i32 %11, i32 5)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 12, i32* %1
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i32 @accept(i32 %11, %struct.sockaddr* %31, i32* %1)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %28
%37 = call i32 @memset(i8* %6, i32 0, i32 10)
%38 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = call i32 @recv(i32 %32, i8* %6, i32 5, i32 0)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %42, %45
%46 = phi i32 [ %50, %45 ], [ %43, %42 ]
%47 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %46)
%48 = call i32 @fwrite(i8* %6, i32 1, i32 %46, i32* %38)
%49 = call i32 @memset(i8* %6, i32 0, i32 10)
%50 = call i32 @recv(i32 %32, i8* %6, i32 5, i32 0)
%51 = icmp sgt i32 %50, 0
br i1 %51, label %45, label %52
52: ; preds = %45, %42
%53 = phi i32 [ %43, %42 ], [ %50, %45 ]
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.8, i64 0, i64 0))
br label %57
57: ; preds = %55, %52
%58 = call i32 @close(i32 %11)
ret i32 0
}