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

/* Variables and functions */
int /*<<< orphan*/ MSG_PEEK;
int NAME_MAX;
int PATH_MAX;
int PIPE_BUF;
size_t atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
scalar_t__ isdigit(char);
int /*<<< orphan*/ memset(char *, char, size_t);
int /*<<< orphan*/ printf(char *);
scalar_t__ recv(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int receive_file(int data_socket_id) {
  ssize_t message_size, written_size;
  size_t file_name_buf_size = PATH_MAX + NAME_MAX + 1;
  size_t buffer_size = PIPE_BUF + 1;
  char file_name_buf[file_name_buf_size], file_buffer[buffer_size];
  FILE *file_id;
  int counter;
  if ((message_size = recv(data_socket_id, file_name_buf, file_name_buf_size,
                           MSG_PEEK)) < 0) {
    printf("recv of file name length failed\n");
    return -1;
  }
  counter = 0;
  while (isdigit(file_name_buf[counter]))
    counter++;
  strncpy(file_buffer, file_name_buf, counter);
  file_name_buf_size = atoi(file_buffer);
  memset(file_name_buf, '\0', file_name_buf_size);
  if ((message_size = recv(data_socket_id, file_name_buf, counter, 0)) < 0) {
    printf("Removal of length from queue failed\n");
    return -1;
  }
  memset(file_name_buf, '\0', file_name_buf_size);
  if ((message_size =
           recv(data_socket_id, file_name_buf, file_name_buf_size, 0)) < 0) {
    printf("recv of file name failed\n");
    return -1;
  }
  file_id = fopen(file_name_buf, "w+");
  do {
    memset(file_buffer, '\0', buffer_size);
    if ((message_size = recv(data_socket_id, file_buffer, buffer_size, 0)) <
        0) {
      printf("recv of file data failed\n");
      fclose(file_id);
      return -1;
    }
    if ((written_size = fwrite(file_buffer, 1, message_size, file_id)) <
        message_size) {
      printf("write to file failed\n");
      fclose(file_id);
      return -1;
    }
  } while (message_size > 0);
  fclose(file_id);
  return 0;
}


x86 assembly (clang, -O0):
<receive_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %edx,%eax
lea    0x1(%rax,%rcx,1),%eax
cltq
mov    %rax,-0x20(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x60(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    0x2dbe(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1256 <receive_file+0xc6>
lea    0xdc2(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    141d <receive_file+0x28d>
movl   $0x0,-0x4c(%rbp)
mov    -0x60(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <isdigit@plt>
cmp    $0x0,%rax
je     1286 <receive_file+0xf6>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    125d <receive_file+0xcd>
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdi
mov    -0x4c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x58(%rbp),%rdi
call   10a0 <atoi@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x60(%rbp),%rsi
mov    -0x8(%rbp),%edi
movslq -0x4c(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    12f1 <receive_file+0x161>
lea    0xd48(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    141d <receive_file+0x28d>
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x60(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    133f <receive_file+0x1af>
lea    0xd1f(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    141d <receive_file+0x28d>
mov    -0x60(%rbp),%rdi
lea    0xd16(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x58(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    13aa <receive_file+0x21a>
lea    0xcda(%rip),%rdi
call   1070 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    141d <receive_file+0x28d>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    %rax,-0x18(%rbp)
cmp    -0x10(%rbp),%rax
jge    13f6 <receive_file+0x266>
lea    0xca8(%rip),%rdi
call   1070 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    141d <receive_file+0x28d>
jmp    13fb <receive_file+0x26b>
cmpq   $0x0,-0x10(%rbp)
jg     1353 <receive_file+0x1c3>
mov    -0x48(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
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    $0x18,%rsp
mov    %edi,%ebx
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%ecx
lea    0x1(%rax,%rcx,1),%eax
movslq %eax,%rdx
mov    0x2e0d(%rip),%rax
movslq (%rax),%r15
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %rsp,%r12
lea    0x10(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
mov    %r14,%rsi
call   1030 <recv@plt>
test   %rax,%rax
js     12fe <receive_file+0x16e>
mov    %ebx,-0x2c(%rbp)
add    $0x1,%r15
mov    $0xffffffffffffffff,%rbx
movsbl 0x1(%r14,%rbx,1),%edi
call   1050 <isdigit@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1210 <receive_file+0x80>
mov    %ebx,%eax
mov    %rax,-0x38(%rbp)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <strncpy@plt>
mov    %r12,%rdi
call   10a0 <atoi@plt>
mov    %rax,%r13
mov    %r14,%rdi
xor    %esi,%esi
mov    %rax,%rdx
call   1080 <memset@plt>
mov    -0x2c(%rbp),%ebx
mov    %ebx,%edi
mov    %r14,%rsi
mov    -0x38(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     1307 <receive_file+0x177>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1080 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     1310 <receive_file+0x180>
lea    0xd69(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%r14
mov    -0x2c(%rbp),%r13d
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1080 <memset@plt>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     1330 <receive_file+0x1a0>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
cmp    %rbx,%rax
jl     1339 <receive_file+0x1a9>
test   %rbx,%rbx
jne    12b0 <receive_file+0x120>
mov    %r14,%rdi
call   1070 <fclose@plt>
xor    %eax,%eax
jmp    1321 <receive_file+0x191>
lea    0xd69(%rip),%rdi
jmp    1317 <receive_file+0x187>
lea    0xd3c(%rip),%rdi
jmp    1317 <receive_file+0x187>
lea    0xd1a(%rip),%rdi
call   1060 <puts@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce1(%rip),%rdi
jmp    1340 <receive_file+0x1b0>
lea    0xcc3(%rip),%rdi
call   1060 <puts@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
jmp    131c <receive_file+0x18c>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@NAME_MAX = global i32 0
@PIPE_BUF = global i32 0
@MSG_PEEK = global i32 0
@.str = [33 x i8] c"recv of file name length failed\0A\00"
@.str.1 = [37 x i8] c"Removal of length from queue failed\0A\00"
@.str.2 = [26 x i8] c"recv of file name failed\0A\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [26 x i8] c"recv of file data failed\0A\00"
@.str.5 = [22 x i8] c"write to file failed\0A\00"
define i32 @receive_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %3
%14 = load i32, i32* @PATH_MAX
%15 = load i32, i32* @NAME_MAX
%16 = add nsw i32 %14, %15
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
store i64 %18, i64* %6
%19 = load i32, i32* @PIPE_BUF
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
store i64 %21, i64* %7
%22 = load i64, i64* %6
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %8
%24 = alloca i8, i64 %22
store i64 %22, i64* %9
%25 = load i64, i64* %7
%26 = alloca i8, i64 %25
store i64 %25, i64* %10
%27 = load i32, i32* %3
%28 = load i64, i64* %6
%29 = load i32, i32* @MSG_PEEK
%30 = call i64 @recv(i32 %27, i8* %24, i64 %28, i32 %29)
store i64 %30, i64* %4
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %1
%33 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %13
br label %97
34: ; preds = %1
store i32 0, i32* %12
br label %35
35: ; preds = %42, %34
%36 = load i32, i32* %12
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %24, i64 %37
%39 = load i8, i8* %38
%40 = call i64 @isdigit(i8 signext %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i32, i32* %12
%44 = add nsw i32 %43, 1
store i32 %44, i32* %12
br label %35
45: ; preds = %35
%46 = load i32, i32* %12
%47 = call i32 @strncpy(i8* %26, i8* %24, i32 %46)
%48 = call i64 @atoi(i8* %26)
store i64 %48, i64* %6
%49 = load i64, i64* %6
%50 = call i32 @memset(i8* %24, i8 signext 0, i64 %49)
%51 = load i32, i32* %3
%52 = load i32, i32* %12
%53 = sext i32 %52 to i64
%54 = call i64 @recv(i32 %51, i8* %24, i64 %53, i32 0)
store i64 %54, i64* %4
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %45
%57 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %13
br label %97
58: ; preds = %45
%59 = load i64, i64* %6
%60 = call i32 @memset(i8* %24, i8 signext 0, i64 %59)
%61 = load i32, i32* %3
%62 = load i64, i64* %6
%63 = call i64 @recv(i32 %61, i8* %24, i64 %62, i32 0)
store i64 %63, i64* %4
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %58
%66 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %13
br label %97
67: ; preds = %58
%68 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %68, i32** %11
br label %69
69: ; preds = %91, %67
%70 = load i64, i64* %7
%71 = call i32 @memset(i8* %26, i8 signext 0, i64 %70)
%72 = load i32, i32* %3
%73 = load i64, i64* %7
%74 = call i64 @recv(i32 %72, i8* %26, i64 %73, i32 0)
store i64 %74, i64* %4
%75 = icmp slt i64 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %69
%77 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%78 = load i32*, i32** %11
%79 = call i32 @fclose(i32* %78)
store i32 -1, i32* %2
store i32 1, i32* %13
br label %97
80: ; preds = %69
%81 = load i64, i64* %4
%82 = load i32*, i32** %11
%83 = call i64 @fwrite(i8* %26, i32 1, i64 %81, i32* %82)
store i64 %83, i64* %5
%84 = load i64, i64* %4
%85 = icmp slt i64 %83, %84
br i1 %85, label %86, label %90
86: ; preds = %80
%87 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%88 = load i32*, i32** %11
%89 = call i32 @fclose(i32* %88)
store i32 -1, i32* %2
store i32 1, i32* %13
br label %97
90: ; preds = %80
br label %91
91: ; preds = %90
%92 = load i64, i64* %4
%93 = icmp sgt i64 %92, 0
br i1 %93, label %69, label %94
94: ; preds = %91
%95 = load i32*, i32** %11
%96 = call i32 @fclose(i32* %95)
store i32 0, i32* %2
store i32 1, i32* %13
br label %97
97: ; preds = %94, %86, %76, %65, %56, %32
%98 = load i8*, i8** %8
%99 = load i32, i32* %2
ret i32 %99
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@NAME_MAX = global i32 0
@PIPE_BUF = global i32 0
@MSG_PEEK = global i32 0
@.str.3 = [3 x i8] c"w+\00"
@str = [21 x i8] c"write to file failed\00"
@str.6 = [25 x i8] c"recv of file data failed\00"
@str.7 = [25 x i8] c"recv of file name failed\00"
@str.8 = [36 x i8] c"Removal of length from queue failed\00"
@str.9 = [32 x i8] c"recv of file name length failed\00"
define i32 @receive_file(i32 %0) {
%2 = load i32, i32* @PATH_MAX
%3 = load i32, i32* @NAME_MAX
%4 = add i32 %2, 1
%5 = add i32 %4, %3
%6 = sext i32 %5 to i64
%7 = load i32, i32* @PIPE_BUF
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = alloca i8, i64 %6
%11 = alloca i8, i64 %9
%12 = load i32, i32* @MSG_PEEK
%13 = call i64 @recv(i32 %0, i8* %10, i64 %6, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.9, i64 0, i64 0))
br label %59
17: ; preds = %1, %17
%18 = phi i64 [ %23, %17 ], [ 0, %1 ]
%19 = getelementptr inbounds i8, i8* %10, i64 %18
%20 = load i8, i8* %19
%21 = call i64 @isdigit(i8 signext %20)
%22 = icmp eq i64 %21, 0
%23 = add nuw i64 %18, 1
br i1 %22, label %24, label %17
24: ; preds = %17
%25 = trunc i64 %18 to i32
%26 = and i64 %18, 4294967295
%27 = call i32 @strncpy(i8* %11, i8* %10, i32 %25)
%28 = call i64 @atoi(i8* %11)
%29 = call i32 @memset(i8* %10, i8 signext 0, i64 %28)
%30 = call i64 @recv(i32 %0, i8* %10, i64 %26, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
br label %59
34: ; preds = %24
%35 = call i32 @memset(i8* %10, i8 signext 0, i64 %28)
%36 = call i64 @recv(i32 %0, i8* %10, i64 %28, i32 0)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
br label %59
40: ; preds = %34
%41 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %55, %40
%43 = call i32 @memset(i8* %11, i8 signext 0, i64 %9)
%44 = call i64 @recv(i32 %0, i8* %11, i64 %9, i32 0)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
%48 = call i32 @fclose(i32* %41)
br label %59
49: ; preds = %42
%50 = call i64 @fwrite(i8* %11, i32 1, i64 %44, i32* %41)
%51 = icmp slt i64 %50, %44
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%54 = call i32 @fclose(i32* %41)
br label %59
55: ; preds = %49
%56 = icmp eq i64 %44, 0
br i1 %56, label %57, label %42
57: ; preds = %55
%58 = call i32 @fclose(i32* %41)
br label %59
59: ; preds = %57, %52, %46, %38, %32, %15
%60 = phi i32 [ -1, %15 ], [ -1, %32 ], [ -1, %38 ], [ -1, %46 ], [ -1, %52 ], [ 0, %57 ]
ret i32 %60
}