#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*/ uint8_t;
struct sockaddr {
  int dummy;
};
struct packet_t {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int deserialize(int /*<<< orphan*/ *, struct packet_t *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ is_lost(double);
int /*<<< orphan*/ *malloc(size_t);
int recvfrom(int, int /*<<< orphan*/ *, size_t, int /*<<< orphan*/,
             struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int recv_packet(struct packet_t *packet, int sock, struct sockaddr *addr,
                socklen_t *addrlen, double loss_rate) {
  if (packet == NULL) {
    fprintf(stderr, "[recv_packet]: packet was NULL\n");
    return -1;
  } else if (addr == NULL) {
    fprintf(stderr, "[recv_packet]: addr was NULL\n");
    return -1;
  } else if (addrlen == NULL) {
    fprintf(stderr, "[recv_packet]: addrlen was NULL\n");
    return -1;
  }
  size_t maxlen = 3 * sizeof(int) + 512;
  uint8_t *buf = malloc(maxlen * sizeof(uint8_t));
  while (is_lost(loss_rate)) {
    ssize_t recv_len = recvfrom(sock, buf, maxlen, 0, addr, addrlen);
    if (recv_len == -1) {
      return -1;
    }
  }
  ssize_t recv_len = recvfrom(sock, buf, maxlen, 0, addr, addrlen);
  if (recv_len == -1) {
    return -1;
  }
  if (deserialize(buf, packet) == -1) {
    fprintf(stderr, "[recv_packet]: couldn't deserialize packet\n");
    free(buf);
    return -1;
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<recv_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movsd  %xmm0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a8 <recv_packet+0x48>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <recv_packet+0x192>
cmpq   $0x0,-0x20(%rbp)
jne    11d4 <recv_packet+0x74>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <recv_packet+0x192>
cmpq   $0x0,-0x28(%rbp)
jne    1200 <recv_packet+0xa0>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <recv_packet+0x192>
jmp    1205 <recv_packet+0xa5>
jmp    120a <recv_packet+0xaa>
movq   $0x20c,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
shl    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1070 <is_lost@plt>
cmp    $0x0,%rax
je     126f <recv_packet+0x10f>
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
xor    %ecx,%ecx
call   1060 <recvfrom@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    126a <recv_packet+0x10a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <recv_packet+0x192>
jmp    1223 <recv_packet+0xc3>
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
xor    %ecx,%ecx
call   1060 <recvfrom@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    12a2 <recv_packet+0x142>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <recv_packet+0x192>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <deserialize@plt>
cmp    $0xffffffff,%eax
jne    12e2 <recv_packet+0x182>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <recv_packet+0x192>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movsd  %xmm0,(%rsp)
test   %rdi,%rdi
je     11d1 <recv_packet+0x71>
mov    %rdx,%rbx
test   %rdx,%rdx
je     11e3 <recv_packet+0x83>
mov    %rcx,%r12
test   %rcx,%rcx
je     11f5 <recv_packet+0x95>
mov    %esi,%r13d
mov    %rdi,%r14
mov    $0x830,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
nopl   0x0(%rax,%rax,1)
movsd  (%rsp),%xmm0
call   1070 <is_lost@plt>
mov    %rax,%rbp
mov    $0x20c,%edx
mov    %r13d,%edi
mov    %r15,%rsi
xor    %ecx,%ecx
mov    %rbx,%r8
mov    %r12,%r9
call   1060 <recvfrom@plt>
test   %rbp,%rbp
je     1220 <recv_packet+0xc0>
cmp    $0xffffffff,%eax
jne    11a0 <recv_packet+0x40>
jmp    120a <recv_packet+0xaa>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
jmp    1205 <recv_packet+0xa5>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
jmp    1205 <recv_packet+0xa5>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     120f <recv_packet+0xaf>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <deserialize@plt>
cmp    $0xffffffff,%eax
je     1246 <recv_packet+0xe6>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    120f <recv_packet+0xaf>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
call   1040 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    120f <recv_packet+0xaf>

LLVM IR (-O0):
%struct.packet_t = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"[recv_packet]: packet was NULL\0A\00"
@.str.1 = [30 x i8] c"[recv_packet]: addr was NULL\0A\00"
@.str.2 = [33 x i8] c"[recv_packet]: addrlen was NULL\0A\00"
@.str.3 = [44 x i8] c"[recv_packet]: couldn't deserialize packet\0A\00"
define i32 @recv_packet(%struct.packet_t* %0, i32 %1, %struct.sockaddr* %2, i32* %3, double %4) {
%6 = alloca i32
%7 = alloca %struct.packet_t*
%8 = alloca i32
%9 = alloca %struct.sockaddr*
%10 = alloca i32*
%11 = alloca double
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
store %struct.packet_t* %0, %struct.packet_t** %7
store i32 %1, i32* %8
store %struct.sockaddr* %2, %struct.sockaddr** %9
store i32* %3, i32** %10
store double %4, double* %11
%16 = load %struct.packet_t*, %struct.packet_t** %7
%17 = icmp eq %struct.packet_t* %16, null
br i1 %17, label %18, label %21
18: ; preds = %5
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %77
21: ; preds = %5
%22 = load %struct.sockaddr*, %struct.sockaddr** %9
%23 = icmp eq %struct.sockaddr* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %77
27: ; preds = %21
%28 = load i32*, i32** %10
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %77
33: ; preds = %27
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34
store i64 524, i64* %12
%36 = load i64, i64* %12
%37 = mul i64 %36, 4
%38 = call i32* @malloc(i64 %37)
store i32* %38, i32** %13
br label %39
39: ; preds = %53, %35
%40 = load double, double* %11
%41 = call i64 @is_lost(double %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %54
43: ; preds = %39
%44 = load i32, i32* %8
%45 = load i32*, i32** %13
%46 = load i64, i64* %12
%47 = load %struct.sockaddr*, %struct.sockaddr** %9
%48 = load i32*, i32** %10
%49 = call i32 @recvfrom(i32 %44, i32* %45, i64 %46, i32 0, %struct.sockaddr* %47, i32* %48)
store i32 %49, i32* %14
%50 = load i32, i32* %14
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %43
store i32 -1, i32* %6
br label %77
53: ; preds = %43
br label %39
54: ; preds = %39
%55 = load i32, i32* %8
%56 = load i32*, i32** %13
%57 = load i64, i64* %12
%58 = load %struct.sockaddr*, %struct.sockaddr** %9
%59 = load i32*, i32** %10
%60 = call i32 @recvfrom(i32 %55, i32* %56, i64 %57, i32 0, %struct.sockaddr* %58, i32* %59)
store i32 %60, i32* %15
%61 = load i32, i32* %15
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %64
63: ; preds = %54
store i32 -1, i32* %6
br label %77
64: ; preds = %54
%65 = load i32*, i32** %13
%66 = load %struct.packet_t*, %struct.packet_t** %7
%67 = call i32 @deserialize(i32* %65, %struct.packet_t* %66)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %74
69: ; preds = %64
%70 = load i32, i32* @stderr
%71 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%72 = load i32*, i32** %13
%73 = call i32 @free(i32* %72)
store i32 -1, i32* %6
br label %77
74: ; preds = %64
%75 = load i32*, i32** %13
%76 = call i32 @free(i32* %75)
store i32 0, i32* %6
br label %77
77: ; preds = %74, %69, %63, %52, %30, %24, %18
%78 = load i32, i32* %6
ret i32 %78
}

LLVM IR (-O2):
%struct.packet_t = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"[recv_packet]: packet was NULL\0A\00"
@.str.1 = [30 x i8] c"[recv_packet]: addr was NULL\0A\00"
@.str.2 = [33 x i8] c"[recv_packet]: addrlen was NULL\0A\00"
@.str.3 = [44 x i8] c"[recv_packet]: couldn't deserialize packet\0A\00"
define i32 @recv_packet(%struct.packet_t* %0, i32 %1, %struct.sockaddr* %2, i32* %3, double %4) {
%6 = icmp eq %struct.packet_t* %0, null
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %38
10: ; preds = %5
%11 = icmp eq %struct.sockaddr* %2, null
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %38
15: ; preds = %10
%16 = icmp eq i32* %3, null
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %38
20: ; preds = %15
%21 = tail call i32* @malloc(i64 2096)
br label %22
22: ; preds = %27, %20
%23 = tail call i64 @is_lost(double %4)
%24 = icmp eq i64 %23, 0
%25 = tail call i32 @recvfrom(i32 %1, i32* %21, i64 524, i32 0, %struct.sockaddr* %2, i32* %3)
%26 = icmp eq i32 %25, -1
br i1 %24, label %28, label %27
27: ; preds = %22
br i1 %26, label %38, label %22
28: ; preds = %22
br i1 %26, label %38, label %29
29: ; preds = %28
%30 = tail call i32 @deserialize(i32* %21, %struct.packet_t* %0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @free(i32* %21)
br label %38
36: ; preds = %29
%37 = tail call i32 @free(i32* %21)
br label %38
38: ; preds = %27, %28, %36, %32, %17, %12, %7
%39 = phi i32 [ -1, %7 ], [ -1, %12 ], [ -1, %17 ], [ -1, %32 ], [ 0, %36 ], [ -1, %28 ], [ -1, %27 ]
ret i32 %39
}