#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 epoll_event {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ backup_file(char *);
int epoll_wait(int, struct epoll_event *, int, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ printf(char *, size_t);
int /*<<< orphan*/ puts(char *);
int rxsize(int, size_t *);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ write_file(int, size_t, char *);
void file_transfer_loop(int sock, char *path, int epfd) {
  struct epoll_event events[1];
  for (;;) {
    int nfds;
    nfds = epoll_wait(epfd, events, 1, -1);
    if (nfds == -1)
      err(1, "epoll_wait");
    size_t sz;
    sz = 0;
    int ret;
    ret = rxsize(sock, &sz);
    if (ret != 0) {
      warn("error reading file size");
    } else if (sz == 0) {
      puts("zero file size - client disconnect");
      return;
    } else {
      printf("start receiving file %zu bytes\n", sz);
      backup_file(path);
      write_file(sock, sz, path);
    }
  }
}


x86 assembly (clang, -O0):
<file_transfer_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   1080 <epoll_wait@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c6 <file_transfer_loop+0x46>
mov    $0x1,%edi
lea    0xe3f(%rip),%rsi
call   1070 <err@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <rxsize@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11f8 <file_transfer_loop+0x78>
lea    0xe1d(%rip),%rdi
call   1030 <warn@plt>
jmp    1243 <file_transfer_loop+0xc3>
cmpq   $0x0,-0x28(%rbp)
jne    1215 <file_transfer_loop+0x95>
lea    0xe19(%rip),%rdi
call   1040 <puts@plt>
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x28(%rbp),%rsi
lea    0xe26(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <backup_file@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1060 <write_file@plt>
jmp    1243 <file_transfer_loop+0xc3>
jmp    1192 <file_transfer_loop+0x12>

x86 assembly (clang, -O2):
<file_transfer_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r13
lea    0xe65(%rip),%r12
jmp    11b8 <file_transfer_loop+0x38>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <warn@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   1080 <epoll_wait@plt>
cmp    $0xffffffff,%eax
jne    11e2 <file_transfer_loop+0x62>
mov    $0x1,%edi
lea    0xe23(%rip),%rsi
call   1070 <err@plt>
movq   $0x0,0x8(%rsp)
mov    %ebp,%edi
mov    %r13,%rsi
call   1090 <rxsize@plt>
test   %eax,%eax
jne    11b0 <file_transfer_loop+0x30>
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
je     1228 <file_transfer_loop+0xa8>
lea    0xe3c(%rip),%rdi
call   1050 <printf@plt>
mov    %r14,%rdi
call   10a0 <backup_file@plt>
mov    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <write_file@plt>
jmp    11b8 <file_transfer_loop+0x38>
lea    0xdf4(%rip),%rdi
call   1040 <puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.epoll_event = type { i32 }
@.str = [11 x i8] c"epoll_wait\00"
@.str.1 = [24 x i8] c"error reading file size\00"
@.str.2 = [35 x i8] c"zero file size - client disconnect\00"
@.str.3 = [32 x i8] c"start receiving file %zu bytes\0A\00"
define void @file_transfer_loop(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [1 x %struct.epoll_event]
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %11
11: ; preds = %41, %3
%12 = load i32, i32* %6
%13 = getelementptr inbounds [1 x %struct.epoll_event], [1 x %struct.epoll_event]* %7, i64 0, i64 0
%14 = call i32 @epoll_wait(i32 %12, %struct.epoll_event* %13, i32 1, i32 -1)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
store i64 0, i64* %9
%20 = load i32, i32* %4
%21 = call i32 @rxsize(i32 %20, i64* %9)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @warn(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %41
26: ; preds = %19
%27 = load i64, i64* %9
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
ret void
31: ; preds = %26
%32 = load i64, i64* %9
%33 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %32)
%34 = load i8*, i8** %5
%35 = call i32 @backup_file(i8* %34)
%36 = load i32, i32* %4
%37 = load i64, i64* %9
%38 = load i8*, i8** %5
%39 = call i32 @write_file(i32 %36, i64 %37, i8* %38)
br label %40
40: ; preds = %31
br label %41
41: ; preds = %40, %24
br label %11
}

LLVM IR (-O2):
%struct.epoll_event = type { i32 }
@.str = [11 x i8] c"epoll_wait\00"
@.str.1 = [24 x i8] c"error reading file size\00"
@.str.2 = [35 x i8] c"zero file size - client disconnect\00"
@.str.3 = [32 x i8] c"start receiving file %zu bytes\0A\00"
define void @file_transfer_loop(i32 %0, i8* %1, i32 %2) {
%4 = alloca [1 x %struct.epoll_event]
%5 = alloca i64
%6 = bitcast [1 x %struct.epoll_event]* %4 to i8*
%7 = getelementptr inbounds [1 x %struct.epoll_event], [1 x %struct.epoll_event]* %4, i64 0, i64 0
%8 = bitcast i64* %5 to i8*
br label %9
9: ; preds = %29, %3
%10 = call i32 @epoll_wait(i32 %2, %struct.epoll_event* %7, i32 1, i32 -1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
store i64 0, i64* %5
%15 = call i32 @rxsize(i32 %0, i64* %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @warn(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %29
19: ; preds = %14
%20 = load i64, i64* %5
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
ret void
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %20)
%26 = call i32 @backup_file(i8* %1)
%27 = load i64, i64* %5
%28 = call i32 @write_file(i32 %0, i64 %27, i8* %1)
br label %29
29: ; preds = %17, %24
br label %9
}