#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 int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int write(int, char *, int);
void writefile(int sockfd, int fd) {
  ssize_t n;
  char buff[4096] = {0};
  while ((n = recv(sockfd, buff, 4096, 0)) > 0) {
    if (n == -1) {
      perror("Receive File Error");
      exit(1);
    }
    if (write(fd, buff, n) != n) {
      perror("Write File Error");
      exit(1);
    }
    memset(buff, 0, 4096);
  }
}


x86 assembly (clang, -O0):
<writefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    11ff <writefile+0xaf>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11b6 <writefile+0x66>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <write@plt>
cmp    -0xc(%rbp),%eax
je     11e7 <writefile+0x97>
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
jmp    1174 <writefile+0x24>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writefile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
mov    %rsp,%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    11c8 <writefile+0x78>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
jne    11d6 <writefile+0x86>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1190 <writefile+0x40>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Receive File Error\00"
@.str.1 = [17 x i8] c"Write File Error\00"
define void @writefile(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4096 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = bitcast [4096 x i8]* %6 to i8*
br label %8
8: ; preds = %29, %2
%9 = load i32, i32* %3
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%11 = call i32 @recv(i32 %9, i8* %10, i32 4096, i32 0)
store i32 %11, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %32
13: ; preds = %8
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = load i32, i32* %4
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%22 = load i32, i32* %5
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
%24 = load i32, i32* %5
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 4096)
br label %8
32: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"Write File Error\00"
define void @writefile(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @recv(i32 %0, i8* %4, i32 4096, i32 0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %2, %14
%8 = phi i32 [ %16, %14 ], [ %5, %2 ]
%9 = call i32 @write(i32 %1, i8* %4, i32 %8)
%10 = icmp eq i32 %9, %8
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %7
%15 = call i32 @memset(i8* %4, i32 0, i32 4096)
%16 = call i32 @recv(i32 %0, i8* %4, i32 4096, i32 0)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %7, label %18
18: ; preds = %14, %2
ret void
}