#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__ {
  void *s_addr;
};
struct sockaddr_in {
  void *sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ reusePort;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INADDR_ANY;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEPORT;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
void *htons(int);
int /*<<< orphan*/ listen(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int sockfd, client_fd, client_len, fd, n;
  int reusePort = 1;
  struct sockaddr_in server_addr, client_addr;
  char filename[256], buff[1024];
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &reusePort, sizeof(reusePort));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htons(INADDR_ANY);
  server_addr.sin_port = htons(5600);
  printf("[SERVER] Created socket\n");
  bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
  printf("[SERVER] Bind socket\n");
  printf("[SERVER] Waiting for client\n");
  listen(sockfd, 8);
  client_fd = accept(sockfd, (struct sockaddr *)&client_addr, &client_len);
  if (client_fd < 0) {
    printf("error\n");
  }
  printf("[SERVER] New connection accepted\n");
  n = read(client_fd, filename, 256);
  filename[n] = '\0';
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    printf("[SERVER] File not found\n");
    strcpy(buff, "File not found\n");
    write(client_fd, buff, strlen(buff));
  } else {
    n = read(fd, buff, 1024);
    buff[n] = '\0';
    write(client_fd, buff, n);
    printf("[SERVER] Transfer successful\n");
  }
  close(client_fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x560,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1070 <htons@plt>
mov    %rax,-0x30(%rbp)
mov    $0x15e0,%edi
call   1070 <htons@plt>
mov    %rax,-0x38(%rbp)
lea    0xd94(%rip),%rdi
call   1080 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   10c0 <bind@plt>
lea    0xd90(%rip),%rdi
call   1080 <printf@plt>
lea    0xd9a(%rip),%rdi
call   1080 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   10b0 <listen@plt>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12d0 <main+0xf0>
lea    0xd81(%rip),%rdi
call   1080 <printf@plt>
lea    0xd7c(%rip),%rdi
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x150(%rbp),%rsi
mov    $0x100,%edx
call   10a0 <read@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movb   $0x0,-0x150(%rbp,%rax,1)
lea    -0x150(%rbp),%rdi
mov    0x2ccb(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    137c <main+0x19c>
lea    0xd4d(%rip),%rdi
call   1080 <printf@plt>
lea    -0x550(%rbp),%rdi
lea    0xd43(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x55c(%rbp)
lea    -0x550(%rbp),%rax
mov    %rax,-0x558(%rbp)
lea    -0x550(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x55c(%rbp),%edi
mov    -0x558(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
jmp    13bd <main+0x1dd>
mov    -0x14(%rbp),%edi
lea    -0x550(%rbp),%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movb   $0x0,-0x550(%rbp,%rax,1)
mov    -0xc(%rbp),%edi
lea    -0x550(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <write@plt>
lea    0xcd6(%rip),%rdi
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x548,%rsp
movl   $0x1,0x8(%rsp)
mov    0x2de7(%rip),%r14
mov    (%r14),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebx
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    (%r14),%eax
mov    %eax,0x20(%rsp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1080 <htons@plt>
mov    %rax,0x18(%rsp)
mov    $0x15e0,%edi
call   1080 <htons@plt>
mov    %rax,0x10(%rsp)
lea    0xdae(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   10c0 <bind@plt>
lea    0xda9(%rip),%rdi
call   1040 <puts@plt>
lea    0xdb2(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    $0x8,%esi
call   10b0 <listen@plt>
lea    0x28(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10e0 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12bf <main+0xdf>
lea    0xdf5(%rip),%rdi
call   1040 <puts@plt>
lea    0xd93(%rip),%rdi
call   1040 <puts@plt>
lea    0x40(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   10a0 <read@plt>
cltq
movb   $0x0,0x40(%rsp,%rax,1)
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10d0 <open@plt>
test   %eax,%eax
jns    1334 <main+0x154>
lea    0xd95(%rip),%rdi
call   1040 <puts@plt>
lea    0xcf2(%rip),%rsi
lea    0x140(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <write@plt>
jmp    136c <main+0x18c>
lea    0x140(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
movslq %eax,%rdx
movb   $0x0,0x140(%rsp,%rdx,1)
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <write@plt>
lea    0xd13(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEPORT = global i32 0
@INADDR_ANY = global i32 0
@.str = [25 x i8] c"[SERVER] Created socket\0A\00"
@.str.1 = [22 x i8] c"[SERVER] Bind socket\0A\00"
@.str.2 = [29 x i8] c"[SERVER] Waiting for client\0A\00"
@.str.3 = [7 x i8] c"error\0A\00"
@.str.4 = [34 x i8] c"[SERVER] New connection accepted\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [25 x i8] c"[SERVER] File not found\0A\00"
@.str.6 = [16 x i8] c"File not found\0A\00"
@.str.7 = [30 x i8] c"[SERVER] Transfer successful\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 i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca [256 x i8]
%11 = alloca [1024 x i8]
store i32 0, i32* %1
store i32 1, i32* %7
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEPORT
%18 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %7, i32 4)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i8* @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i8* %22, i8** %24
%25 = call i8* @htons(i32 5600)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i8* %25, i8** %26
%27 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 24)
%31 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* %2
%34 = call i32 @listen(i32 %33, i32 8)
%35 = load i32, i32* %2
%36 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%37 = call i32 @accept(i32 %35, %struct.sockaddr* %36, i32* %4)
store i32 %37, i32* %3
%38 = load i32, i32* %3
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %0
%41 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %0
%43 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32, i32* %3
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 256)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %48
store i8 0, i8* %49
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%51 = load i32, i32* @O_RDONLY
%52 = call i32 @open(i8* %50, i32 %51)
store i32 %52, i32* %5
%53 = load i32, i32* %5
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %64
55: ; preds = %42
%56 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%58 = call i32 @strcpy(i8* %57, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%59 = load i32, i32* %3
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%62 = call i32 @strlen(i8* %61)
%63 = call i32 @write(i32 %59, i8* %60, i32 %62)
br label %76
64: ; preds = %42
%65 = load i32, i32* %5
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%67 = call i32 @read(i32 %65, i8* %66, i32 1024)
store i32 %67, i32* %6
%68 = load i32, i32* %6
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 %69
store i8 0, i8* %70
%71 = load i32, i32* %3
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%73 = load i32, i32* %6
%74 = call i32 @write(i32 %71, i8* %72, i32 %73)
%75 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %76
76: ; preds = %64, %55
%77 = load i32, i32* %3
%78 = call i32 @close(i32 %77)
%79 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEPORT = global i32 0
@INADDR_ANY = global i32 0
@O_RDONLY = global i32 0
@.str.6 = [16 x i8] c"File not found\0A\00"
@str = [24 x i8] c"[SERVER] Created socket\00"
@str.8 = [21 x i8] c"[SERVER] Bind socket\00"
@str.9 = [28 x i8] c"[SERVER] Waiting for client\00"
@str.10 = [33 x i8] c"[SERVER] New connection accepted\00"
@str.11 = [29 x i8] c"[SERVER] Transfer successful\00"
@str.12 = [24 x i8] c"[SERVER] File not found\00"
@str.13 = [6 x i8] c"error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [256 x i8]
%6 = alloca [1024 x i8]
%7 = bitcast i32* %1 to i8*
%8 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = bitcast %struct.sockaddr_in* %4 to i8*
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEPORT
%18 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %2, i32 4)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i8* @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i8* %22, i8** %23
%24 = call i8* @htons(i32 5600)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i8* %24, i8** %25
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @bind(i32 %15, %struct.sockaddr* %27, i32 24)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.9, i64 0, i64 0))
%31 = call i32 @listen(i32 %15, i32 8)
%32 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%33 = call i32 @accept(i32 %15, %struct.sockaddr* %32, i32* %1)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %0
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.13, i64 0, i64 0))
br label %37
37: ; preds = %35, %0
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.10, i64 0, i64 0))
%39 = call i32 @read(i32 %33, i8* %11, i32 256)
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* %11, i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %37
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.12, i64 0, i64 0))
%47 = call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%48 = call i32 @strlen(i8* %12)
%49 = call i32 @write(i32 %33, i8* %12, i32 %48)
br label %56
50: ; preds = %37
%51 = call i32 @read(i32 %43, i8* %12, i32 1024)
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %52
store i8 0, i8* %53
%54 = call i32 @write(i32 %33, i8* %12, i32 %51)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.11, i64 0, i64 0))
br label %56
56: ; preds = %50, %45
%57 = call i32 @close(i32 %33)
%58 = call i32 @exit(i32 0)
unreachable
}