#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_addr;
typedef int /*<<< orphan*/ clnt_addr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(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*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in serv_addr;
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  serv_addr.sin_port = htons(1234);
  bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  listen(serv_sock, 20);
  struct sockaddr_in clnt_addr;
  socklen_t clnt_addr_size = sizeof(clnt_addr);
  int clnt_sock =
      accept(serv_sock, (struct sockaddr *)&clnt_addr, &clnt_addr_size);
  int count;
  char buffer[100];
  FILE *fp;
  fp = fopen("file-server", "r");
  while ((count = fread(buffer, 1, 100, fp)) > 0) {
    send(clnt_sock, buffer, count, 0);
  }
  shutdown(clnt_sock, SHUT_WR);
  recv(clnt_sock, buffer, 100, 0);
  fclose(fp);
  close(clnt_sock);
  close(serv_sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xdc8(%rip),%rdi
call   10b0 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    $0x4d2,%edi
call   1070 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   10c0 <listen@plt>
movl   $0xc,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x30(%rbp)
lea    0xd7e(%rip),%rdi
lea    0xd81(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0xa8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x64,%edx
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jle    12e1 <main+0x101>
mov    -0x30(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x34(%rbp),%edx
xor    %ecx,%ecx
call   1080 <send@plt>
jmp    129f <main+0xbf>
mov    -0x30(%rbp),%edi
mov    0x2cf5(%rip),%rax
mov    (%rax),%esi
call   1060 <shutdown@plt>
mov    -0x30(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    -0xa8(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%edi
call   10a0 <close@plt>
mov    -0x8(%rbp),%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    0x2df4(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
lea    0xdcd(%rip),%rdi
call   10b0 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    $0x4d2,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    %r14d,%edi
mov    $0x14,%esi
call   10c0 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10f0 <accept@plt>
mov    %eax,%r15d
lea    0xd7f(%rip),%rdi
lea    0xd82(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,%rbx
lea    0x30(%rsp),%rdi
mov    $0x1,%esi
mov    $0x64,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    12e8 <main+0x108>
lea    0x30(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x64,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jg     12c0 <main+0xe0>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1060 <shutdown@plt>
lea    0x30(%rsp),%rsi
mov    %r15d,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
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
@IPPROTO_TCP = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [12 x i8] c"file-server\00"
@.str.2 = [2 x i8] c"r\00"
@SHUT_WR = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca i32*
store i32 0, i32* %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @IPPROTO_TCP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %2
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = call i32 @htons(i32 1234)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i32 @bind(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = load i32, i32* %2
%26 = call i32 @listen(i32 %25, i32 20)
store i32 12, i32* %5
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i32 @accept(i32 %27, %struct.sockaddr* %28, i32* %5)
store i32 %29, i32* %6
%30 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %9
br label %31
31: ; preds = %36, %0
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%33 = load i32*, i32** %9
%34 = call i32 @fread(i8* %32, i32 1, i32 100, i32* %33)
store i32 %34, i32* %7
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i32, i32* %6
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%39 = load i32, i32* %7
%40 = call i32 @send(i32 %37, i8* %38, i32 %39, i32 0)
br label %31
41: ; preds = %31
%42 = load i32, i32* %6
%43 = load i32, i32* @SHUT_WR
%44 = call i32 @shutdown(i32 %42, i32 %43)
%45 = load i32, i32* %6
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%47 = call i32 @recv(i32 %45, i8* %46, i32 100, i32 0)
%48 = load i32*, i32** %9
%49 = call i32 @fclose(i32* %48)
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %2
%53 = call i32 @close(i32 %52)
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
@IPPROTO_TCP = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [12 x i8] c"file-server\00"
@.str.2 = [2 x i8] c"r\00"
@SHUT_WR = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = bitcast %struct.sockaddr_in* %1 to i8*
%10 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = call i32 @htons(i32 1234)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i32 @bind(i32 %8, %struct.sockaddr* %17, i32 12)
%19 = call i32 @listen(i32 %8, i32 20)
%20 = bitcast %struct.sockaddr_in* %2 to i8*
%21 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @accept(i32 %8, %struct.sockaddr* %22, i32* %3)
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%25 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @fread(i8* %24, i32 1, i32 100, i32* %25)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %0, %28
%29 = phi i32 [ %31, %28 ], [ %26, %0 ]
%30 = call i32 @send(i32 %23, i8* %24, i32 %29, i32 0)
%31 = call i32 @fread(i8* %24, i32 1, i32 100, i32* %25)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %28, label %33
33: ; preds = %28, %0
%34 = load i32, i32* @SHUT_WR
%35 = call i32 @shutdown(i32 %23, i32 %34)
%36 = call i32 @recv(i32 %23, i8* %24, i32 100, i32 0)
%37 = call i32 @fclose(i32* %25)
%38 = call i32 @close(i32 %23)
%39 = call i32 @close(i32 %8)
ret i32 0
}