#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ remote;

/* Variables and functions */
int /*<<< orphan*/ error(char *);
char *malloc(int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ usleep(int);
int recv_message(int sock, int timeout, char **message, int *len) {
  char *msg = malloc(1024);
  *message = msg;
  *len = 0;
  if (!msg)
    error("malloc");
  int useconds = 0;
  struct sockaddr_in remote;
  int remote_len = sizeof(remote);
  while (useconds < timeout) {
    char status;
    int n = recvfrom(sock, (char *)msg, 1024, 0, (struct sockaddr *)&remote,
                     &remote_len);
    if (n > 0) {
      *len = n;
      return 1;
    }
    usleep(5000);
    useconds += 5;
  }
  return 0;
}


x86 assembly (clang, -O0):
<recv_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x400,%edi
call   1030 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
jne    1190 <recv_message+0x50>
lea    0xe75(%rip),%rdi
call   1050 <error@plt>
movl   $0x0,-0x2c(%rbp)
movl   $0x4,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ff <recv_message+0xbf>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1040 <recvfrom@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    11e7 <recv_message+0xa7>
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1206 <recv_message+0xc6>
mov    $0x1388,%edi
call   1060 <usleep@plt>
mov    -0x2c(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x2c(%rbp)
jmp    119e <recv_message+0x5e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%ebp
mov    %edi,%r15d
mov    $0x400,%edi
call   1030 <malloc@plt>
mov    %rax,%rbx
mov    %rax,(%r12)
movl   $0x0,(%r14)
test   %rax,%rax
jne    1182 <recv_message+0x42>
lea    0xe83(%rip),%rdi
call   1050 <error@plt>
movl   $0x4,0x4(%rsp)
test   %ebp,%ebp
jle    11cf <recv_message+0x8f>
mov    %r14,0x8(%rsp)
xor    %r14d,%r14d
lea    0x10(%rsp),%r12
lea    0x4(%rsp),%r13
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   1040 <recvfrom@plt>
test   %eax,%eax
jg     11d3 <recv_message+0x93>
mov    $0x1388,%edi
call   1060 <usleep@plt>
add    $0x5,%r14d
cmp    %ebp,%r14d
jl     11a0 <recv_message+0x60>
xor    %eax,%eax
jmp    11df <recv_message+0x9f>
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"malloc\00"
define i32 @recv_message(i32 %0, i32 %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i8
%15 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
%16 = call i8* @malloc(i32 1024)
store i8* %16, i8** %10
%17 = load i8*, i8** %10
%18 = load i8**, i8*** %8
store i8* %17, i8** %18
%19 = load i32*, i32** %9
store i32 0, i32* %19
%20 = load i8*, i8** %10
%21 = icmp ne i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %4
%23 = call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %4
store i32 0, i32* %11
store i32 4, i32* %13
br label %25
25: ; preds = %39, %24
%26 = load i32, i32* %11
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = load i32, i32* %6
%31 = load i8*, i8** %10
%32 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%33 = call i32 @recvfrom(i32 %30, i8* %31, i32 1024, i32 0, %struct.sockaddr* %32, i32* %13)
store i32 %33, i32* %15
%34 = load i32, i32* %15
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* %15
%38 = load i32*, i32** %9
store i32 %37, i32* %38
store i32 1, i32* %5
br label %44
39: ; preds = %29
%40 = call i32 @usleep(i32 5000)
%41 = load i32, i32* %11
%42 = add nsw i32 %41, 5
store i32 %42, i32* %11
br label %25
43: ; preds = %25
store i32 0, i32* %5
br label %44
44: ; preds = %43, %36
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"malloc\00"
define i32 @recv_message(i32 %0, i32 %1, i8** nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
store i8* %7, i8** %2
store i32 0, i32* %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %4
%12 = bitcast %struct.sockaddr_in* %5 to i8*
%13 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%14 = icmp sgt i32 %1, 0
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %17
17: ; preds = %15, %22
%18 = phi i32 [ 0, %15 ], [ %24, %22 ]
%19 = call i32 @recvfrom(i32 %0, i8* %7, i32 1024, i32 0, %struct.sockaddr* %16, i32* %6)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 %19, i32* %3
br label %26
22: ; preds = %17
%23 = call i32 @usleep(i32 5000)
%24 = add nuw nsw i32 %18, 5
%25 = icmp slt i32 %24, %1
br i1 %25, label %17, label %26
26: ; preds = %22, %11, %21
%27 = phi i32 [ 1, %21 ], [ 0, %11 ], [ 0, %22 ]
ret i32 %27
}