#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 socklen_t;
typedef int /*<<< orphan*/ serv_adr;
typedef int /*<<< orphan*/ clnt_adr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int serv_sd, clnt_sd;
  FILE *fp;
  char buf[30];
  int read_cnt;
  struct sockaddr_in serv_adr, clnt_adr;
  socklen_t clnt_adr_sz;
  if (argc != 2) {
    printf("Usage: %s <port>\n", argv[0]);
    exit(1);
  }
  fp = fopen("file_server.c", "rb");
  serv_sd = socket(PF_INET, SOCK_STREAM, 0);
  memset(&serv_adr, 0, sizeof(serv_adr));
  serv_adr.sin_family = AF_INET;
  serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_adr.sin_port = htons(atoi(argv[1]));
  bind(serv_sd, (struct sockaddr *)&serv_adr, sizeof(serv_adr));
  listen(serv_sd, 5);
  clnt_adr_sz = sizeof(clnt_adr);
  clnt_sd = accept(serv_sd, (struct sockaddr *)&clnt_adr, &clnt_adr_sz);
  while (1) {
    read_cnt = fread((void *)buf, 1, 30, fp);
    if (read_cnt < 30) {
      write(clnt_sd, buf, read_cnt);
      break;
    }
    write(clnt_sd, buf, 30);
  }
  shutdown(clnt_sd, SHUT_WR);
  read(clnt_sd, buf, 30);
  printf("Message from client: %s \n", buf);
  fclose(fp);
  close(clnt_sd);
  close(serv_sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     124d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdc2(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    0xdbe(%rip),%rdi
lea    0xdc5(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,%edi
call   1070 <htons@plt>
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
movl   $0xc,-0x64(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
lea    -0x64(%rbp),%rdx
call   1100 <accept@plt>
mov    %eax,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1e,%edx
call   1030 <fread@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1e,-0x44(%rbp)
jge    1334 <main+0x124>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1040 <write@plt>
jmp    134a <main+0x13a>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1e,%edx
call   1040 <write@plt>
jmp    12fc <main+0xec>
mov    -0x18(%rbp),%edi
mov    0x2c8c(%rip),%rax
mov    (%rax),%esi
call   1060 <shutdown@plt>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1e,%edx
call   10c0 <read@plt>
lea    -0x40(%rbp),%rsi
lea    0xcac(%rip),%rdi
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%edi
call   10b0 <close@plt>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    138e <main+0x17e>
lea    0xde3(%rip),%rdi
lea    0xdea(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,%r12
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rbp),%rdi
call   1110 <atoi@plt>
mov    %eax,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x40(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   1100 <accept@plt>
mov    %eax,%ebp
lea    0x20(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1e,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1e,%eax
jl     1329 <main+0x119>
lea    0x20(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1e,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1e,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1e,%eax
jge    1300 <main+0xf0>
lea    0x20(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <shutdown@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1e,%edx
call   10c0 <read@plt>
lea    0xcc3(%rip),%rdi
mov    %rbx,%rsi
call   1080 <printf@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %ebp,%edi
call   10b0 <close@plt>
mov    %r14d,%edi
call   10b0 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%rbp),%rsi
lea    0xc67(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@.str.1 = [14 x i8] c"file_server.c\00"
@.str.2 = [3 x i8] c"rb\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SHUT_WR = global i32 0
@.str.3 = [26 x i8] c"Message from client: %s \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [30 x i8]
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %8
%24 = load i32, i32* @PF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %6
%27 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = call i32 @htonl(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = load i32, i32* %6
%44 = call i32 @listen(i32 %43, i32 5)
store i32 12, i32* %13
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %13)
store i32 %47, i32* %7
br label %48
48: ; preds = %22, %59
%49 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%50 = load i32*, i32** %8
%51 = call i32 @fread(i8* %49, i32 1, i32 30, i32* %50)
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = icmp slt i32 %52, 30
br i1 %53, label %54, label %59
54: ; preds = %48
%55 = load i32, i32* %7
%56 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%57 = load i32, i32* %10
%58 = call i32 @write(i32 %55, i8* %56, i32 %57)
br label %63
59: ; preds = %48
%60 = load i32, i32* %7
%61 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%62 = call i32 @write(i32 %60, i8* %61, i32 30)
br label %48
63: ; preds = %54
%64 = load i32, i32* %7
%65 = load i32, i32* @SHUT_WR
%66 = call i32 @shutdown(i32 %64, i32 %65)
%67 = load i32, i32* %7
%68 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%69 = call i32 @read(i32 %67, i8* %68, i32 30)
%70 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%71 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = load i32*, i32** %8
%73 = call i32 @fclose(i32* %72)
%74 = load i32, i32* %7
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@.str.1 = [14 x i8] c"file_server.c\00"
@.str.2 = [3 x i8] c"rb\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SHUT_WR = global i32 0
@.str.3 = [26 x i8] c"Message from client: %s \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [30 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = icmp eq i32 %0, 2
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i8*, i8** %1
%14 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%33 = call i32 @bind(i32 %20, %struct.sockaddr* %32, i32 12)
%34 = call i32 @listen(i32 %20, i32 5)
store i32 12, i32* %6
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i32 @accept(i32 %20, %struct.sockaddr* %35, i32* %6)
%37 = call i32 @fread(i8* %7, i32 1, i32 30, i32* %17)
%38 = icmp slt i32 %37, 30
br i1 %38, label %39, label %49
39: ; preds = %49, %16
%40 = phi i32 [ %37, %16 ], [ %51, %49 ]
%41 = call i32 @write(i32 %36, i8* %7, i32 %40)
%42 = load i32, i32* @SHUT_WR
%43 = call i32 @shutdown(i32 %36, i32 %42)
%44 = call i32 @read(i32 %36, i8* %7, i32 30)
%45 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%46 = call i32 @fclose(i32* %17)
%47 = call i32 @close(i32 %36)
%48 = call i32 @close(i32 %20)
ret i32 0
49: ; preds = %16, %49
%50 = call i32 @write(i32 %36, i8* %7, i32 30)
%51 = call i32 @fread(i8* %7, i32 1, i32 30, i32* %17)
%52 = icmp slt i32 %51, 30
br i1 %52, label %39, label %49
}