#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 */

/* Type definitions */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef scalar_t__ off_t;
typedef int /*<<< orphan*/ file_size;

/* Variables and functions */
int BUFSIZ;
int O_CREAT;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error_msg_and_exit(char *);
int open(char *, int, int);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, scalar_t__);
void receive_file(int server_socket) {
  int peer_socket;
  struct sockaddr_in peer_addr;
  socklen_t sock_len;
  char buffer[BUFSIZ];
  int received_fd;
  off_t file_size;
  off_t remain_data = 0ul;
  ssize_t len;
  peer_socket = accept(server_socket, (struct sockaddr *)&peer_addr, &sock_len);
  if (peer_socket < 0) {
    error_msg_and_exit("Error on accept");
  }
  if (recv(peer_socket, (void *)&file_size, sizeof(file_size), 0) < 0) {
    error_msg_and_exit("Error on receiving size of the file");
  }
  received_fd = open("file1", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  if (received_fd < 0) {
    error_msg_and_exit("Failed to open file");
  }
  remain_data = file_size;
  while (((len = recv(peer_socket, buffer, BUFSIZ, 0)) > 0) &&
         (remain_data > 0)) {
    write(received_fd, buffer, len);
    remain_data -= len;
  }
  if (len < 0) {
    error_msg_and_exit("Error on receiving size of the file");
  }
  close(received_fd);
}


x86 assembly (clang, -O0):
<receive_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c8 <receive_file+0x68>
lea    0xe3d(%rip),%rdi
call   1080 <error_msg_and_exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jge    11f1 <receive_file+0x91>
lea    0xe24(%rip),%rdi
call   1080 <error_msg_and_exit@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    0x2dcd(%rip),%rax
or     (%rax),%edx
lea    0xe18(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    123a <receive_file+0xda>
lea    0xe05(%rip),%rdi
call   1080 <error_msg_and_exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%rcx
mov    %rcx,-0x48(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x51(%rbp)
jle    127a <receive_file+0x11a>
cmpq   $0x0,-0x40(%rbp)
setg   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    128a <receive_file+0x12a>
jmp    12ae <receive_file+0x14e>
mov    -0x50(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    -0x48(%rbp),%rdx
call   1040 <write@plt>
mov    -0x48(%rbp),%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    1242 <receive_file+0xe2>
cmpq   $0x0,-0x48(%rbp)
jge    12c5 <receive_file+0x165>
lea    0xd50(%rip),%rdi
call   1080 <error_msg_and_exit@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsp,-0x30(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11af <receive_file+0x4f>
lea    0xe56(%rip),%rdi
call   1080 <error_msg_and_exit@plt>
lea    -0x38(%rbp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jns    11d3 <receive_file+0x73>
lea    0xe42(%rip),%rdi
call   1080 <error_msg_and_exit@plt>
mov    0x2dde(%rip),%rax
mov    0x2e17(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe36(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1216 <receive_file+0xb6>
lea    0xe29(%rip),%rdi
call   1080 <error_msg_and_exit@plt>
mov    -0x38(%rbp),%r13
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %r12,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    1276 <receive_file+0x116>
test   %r13,%r13
jle    12a5 <receive_file+0x145>
nopl   (%rax)
mov    %r15d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %r12,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    1279 <receive_file+0x119>
sub    %rbx,%r13
mov    %rax,%rbx
test   %r13,%r13
jg     1240 <receive_file+0xe0>
jmp    1279 <receive_file+0x119>
mov    %rbx,%rax
test   %rax,%rax
jns    128a <receive_file+0x12a>
lea    0xd8b(%rip),%rdi
call   1080 <error_msg_and_exit@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rax
test   %rax,%rax
jns    128a <receive_file+0x12a>
jmp    127e <receive_file+0x11e>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@.str = [16 x i8] c"Error on accept\00"
@.str.1 = [36 x i8] c"Error on receiving size of the file\00"
@.str.2 = [6 x i8] c"file1\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [20 x i8] c"Failed to open file\00"
define void @receive_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %2
%12 = load i32, i32* @BUFSIZ
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
store i64 0, i64* %10
%16 = load i32, i32* %2
%17 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%18 = call i32 @accept(i32 %16, %struct.sockaddr* %17, i32* %5)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %1
%24 = load i32, i32* %3
%25 = bitcast i64* %9 to i8*
%26 = call i64 @recv(i32 %24, i8* %25, i32 8, i32 0)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = load i32, i32* @O_WRONLY
%32 = load i32, i32* @O_CREAT
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IRUSR
%35 = load i32, i32* @S_IWUSR
%36 = or i32 %34, %35
%37 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %30
%41 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %30
%43 = load i64, i64* %9
store i64 %43, i64* %10
br label %44
44: ; preds = %54, %42
%45 = load i32, i32* %3
%46 = load i32, i32* @BUFSIZ
%47 = call i64 @recv(i32 %45, i8* %15, i32 %46, i32 0)
store i64 %47, i64* %11
%48 = icmp sgt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i64, i64* %10
%51 = icmp sgt i64 %50, 0
br label %52
52: ; preds = %49, %44
%53 = phi i1 [ false, %44 ], [ %51, %49 ]
br i1 %53, label %54, label %61
54: ; preds = %52
%55 = load i32, i32* %8
%56 = load i64, i64* %11
%57 = call i32 @write(i32 %55, i8* %15, i64 %56)
%58 = load i64, i64* %11
%59 = load i64, i64* %10
%60 = sub nsw i64 %59, %58
store i64 %60, i64* %10
br label %44
61: ; preds = %52
%62 = load i64, i64* %11
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
%67 = load i32, i32* %8
%68 = call i32 @close(i32 %67)
%69 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@.str = [16 x i8] c"Error on accept\00"
@.str.1 = [36 x i8] c"Error on receiving size of the file\00"
@.str.2 = [6 x i8] c"file1\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [20 x i8] c"Failed to open file\00"
define void @receive_file(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i64
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @BUFSIZ
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = bitcast i64* %4 to i8*
%12 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %12, i32* %3)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %1
%18 = call i64 @recv(i32 %13, i8* %11, i32 8, i32 0)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %27, %26
%29 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %22
%34 = load i64, i64* %4
%35 = load i32, i32* @BUFSIZ
%36 = call i64 @recv(i32 %13, i8* %10, i32 %35, i32 0)
%37 = icmp sgt i64 %36, 0
%38 = icmp sgt i64 %34, 0
%39 = select i1 %37, i1 %38, i1 false
br i1 %39, label %40, label %50
40: ; preds = %33, %40
%41 = phi i64 [ %46, %40 ], [ %36, %33 ]
%42 = phi i64 [ %44, %40 ], [ %34, %33 ]
%43 = call i32 @write(i32 %29, i8* %10, i64 %41)
%44 = sub nsw i64 %42, %41
%45 = load i32, i32* @BUFSIZ
%46 = call i64 @recv(i32 %13, i8* %10, i32 %45, i32 0)
%47 = icmp sgt i64 %46, 0
%48 = icmp sgt i64 %44, 0
%49 = select i1 %47, i1 %48, i1 false
br i1 %49, label %40, label %50
50: ; preds = %40, %33
%51 = phi i64 [ %36, %33 ], [ %46, %40 ]
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @error_msg_and_exit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
%56 = call i32 @close(i32 %29)
ret void
}