#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 /*<<< orphan*/ uint16_t;

/* Variables and functions */
scalar_t__ bind_socket(int /*<<< orphan*/, int *);
int /*<<< orphan*/ close(int);
scalar_t__ create_and_send_session_key(char *);
scalar_t__ get_client(int, int *);
scalar_t__ parse_args(int, char **, int /*<<< orphan*/ *, int *, char const **);
scalar_t__ raw_receive_file(char const *, int, int);
scalar_t__ receive_and_validate_session_key(char *, int);
int do_receive_file(int num_args, char **args) {
  uint16_t port;
  int size;
  const char *path;
  if (parse_args(num_args, args, &port, &size, &path) != 0) {
    return 1;
  }
  int listen_socket = -1;
  int client_socket = -1;
  if (bind_socket(port, &listen_socket) != 0) {
    goto fail1;
  }
  char secret_key[(32) + 1];
  if (create_and_send_session_key(secret_key) != 0) {
    goto fail1;
  }
  if (get_client(listen_socket, &client_socket) != 0) {
    goto fail1;
  }
  close(listen_socket);
  listen_socket = -1;
  if (receive_and_validate_session_key(secret_key, client_socket) != 0) {
    goto fail1;
  }
  if (raw_receive_file(path, size, client_socket) != 0) {
    goto fail1;
  }
  return 0;
fail1:
  if (client_socket >= 0) {
    close(client_socket);
  }
  if (listen_socket >= 0) {
    close(listen_socket);
  }
  return 1;
}


x86 assembly (clang, -O0):
<do_receive_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1060 <parse_args@plt>
cmp    $0x0,%rax
je     11ad <do_receive_file+0x3d>
movl   $0x1,-0x4(%rbp)
jmp    1288 <do_receive_file+0x118>
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1030 <bind_socket@plt>
cmp    $0x0,%rax
je     11d6 <do_receive_file+0x66>
jmp    125d <do_receive_file+0xed>
lea    -0x50(%rbp),%rdi
call   1090 <create_and_send_session_key@plt>
cmp    $0x0,%rax
je     11ee <do_receive_file+0x7e>
jmp    125d <do_receive_file+0xed>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1040 <get_client@plt>
cmp    $0x0,%rax
je     1209 <do_receive_file+0x99>
jmp    125d <do_receive_file+0xed>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1080 <receive_and_validate_session_key@plt>
cmp    $0x0,%rax
je     1233 <do_receive_file+0xc3>
jmp    125d <do_receive_file+0xed>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1070 <raw_receive_file@plt>
cmp    $0x0,%rax
je     1251 <do_receive_file+0xe1>
jmp    125d <do_receive_file+0xed>
movl   $0x0,-0x4(%rbp)
jmp    1288 <do_receive_file+0x118>
cmpl   $0x0,-0x28(%rbp)
jl     126f <do_receive_file+0xff>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x24(%rbp)
jl     1281 <do_receive_file+0x111>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_receive_file>:
push   %rbx
sub    $0x50,%rsp
lea    0x14(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x18(%rsp),%r8
call   1060 <parse_args@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    1236 <do_receive_file+0xc6>
movl   $0xffffffff,0x8(%rsp)
ff
movl   $0xffffffff,0xc(%rsp)
ff
mov    0x14(%rsp),%edi
lea    0x8(%rsp),%rsi
call   1030 <bind_socket@plt>
test   %rax,%rax
jne    1217 <do_receive_file+0xa7>
lea    0x20(%rsp),%rdi
call   1090 <create_and_send_session_key@plt>
test   %rax,%rax
jne    1217 <do_receive_file+0xa7>
mov    0x8(%rsp),%edi
lea    0xc(%rsp),%rsi
call   1040 <get_client@plt>
test   %rax,%rax
jne    1217 <do_receive_file+0xa7>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
movl   $0xffffffff,0x8(%rsp)
ff
mov    0xc(%rsp),%esi
lea    0x20(%rsp),%rdi
call   1080 <receive_and_validate_session_key@plt>
test   %rax,%rax
jne    1217 <do_receive_file+0xa7>
mov    0x18(%rsp),%rdi
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
call   1070 <raw_receive_file@plt>
test   %rax,%rax
je     123e <do_receive_file+0xce>
mov    0xc(%rsp),%edi
test   %edi,%edi
js     1224 <do_receive_file+0xb4>
call   1050 <close@plt>
mov    0x8(%rsp),%edi
mov    $0x1,%ebx
test   %edi,%edi
js     1236 <do_receive_file+0xc6>
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
ret
xor    %ebx,%ebx
jmp    1236 <do_receive_file+0xc6>

LLVM IR (-O0):
define i32 @do_receive_file(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [33 x i8]
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i64 @parse_args(i32 %12, i8** %13, i32* %6, i32* %7, i8** %8)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 1, i32* %3
br label %61
17: ; preds = %2
store i32 -1, i32* %9
store i32 -1, i32* %10
%18 = load i32, i32* %6
%19 = call i64 @bind_socket(i32 %18, i32* %9)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
br label %48
22: ; preds = %17
%23 = getelementptr inbounds [33 x i8], [33 x i8]* %11, i64 0, i64 0
%24 = call i64 @create_and_send_session_key(i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
br label %48
27: ; preds = %22
%28 = load i32, i32* %9
%29 = call i64 @get_client(i32 %28, i32* %10)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
br label %48
32: ; preds = %27
%33 = load i32, i32* %9
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %9
%35 = getelementptr inbounds [33 x i8], [33 x i8]* %11, i64 0, i64 0
%36 = load i32, i32* %10
%37 = call i64 @receive_and_validate_session_key(i8* %35, i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %32
br label %48
40: ; preds = %32
%41 = load i8*, i8** %8
%42 = load i32, i32* %7
%43 = load i32, i32* %10
%44 = call i64 @raw_receive_file(i8* %41, i32 %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
br label %48
47: ; preds = %40
store i32 0, i32* %3
br label %61
48: ; preds = %46, %39, %31, %26, %21
%49 = load i32, i32* %10
%50 = icmp sge i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* %10
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51, %48
%55 = load i32, i32* %9
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i32, i32* %9
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %54
store i32 1, i32* %3
br label %61
61: ; preds = %60, %47, %16
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
define i32 @do_receive_file(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [33 x i8]
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = bitcast i8** %5 to i8*
%12 = call i64 @parse_args(i32 %0, i8** %1, i32* %3, i32* %4, i8** %5)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %52
14: ; preds = %2
%15 = bitcast i32* %6 to i8*
store i32 -1, i32* %6
%16 = bitcast i32* %7 to i8*
store i32 -1, i32* %7
%17 = load i32, i32* %3
%18 = call i64 @bind_socket(i32 %17, i32* %6)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %40
20: ; preds = %14
%21 = getelementptr inbounds [33 x i8], [33 x i8]* %8, i64 0, i64 0
%22 = call i64 @create_and_send_session_key(i8* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %20
%25 = load i32, i32* %6
%26 = call i64 @get_client(i32 %25, i32* %7)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %24
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %6
%31 = load i32, i32* %7
%32 = call i64 @receive_and_validate_session_key(i8* %21, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %28
%35 = load i8*, i8** %5
%36 = load i32, i32* %4
%37 = load i32, i32* %7
%38 = call i64 @raw_receive_file(i8* %35, i32 %36, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %50, label %40
40: ; preds = %34, %28, %24, %20, %14
%41 = load i32, i32* %7
%42 = icmp sgt i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @close(i32 %41)
br label %45
45: ; preds = %43, %40
%46 = load i32, i32* %6
%47 = icmp sgt i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @close(i32 %46)
br label %50
50: ; preds = %45, %48, %34
%51 = phi i32 [ 0, %34 ], [ 1, %48 ], [ 1, %45 ]
br label %52
52: ; preds = %2, %50
%53 = phi i32 [ %51, %50 ], [ 1, %2 ]
ret i32 %53
}