#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 {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int readMessage(int sockfd, char *buf, int buflen, struct sockaddr *servaddr,
                socklen_t servlen) {
  int msg;
  if ((msg = recvfrom(sockfd, buf, buflen, 0, servaddr, &servlen)) < 0) {
    if (errno == EWOULDBLOCK) {
      fprintf(stderr, "socket timeout\n");
      return (2);
    } else {
      printf("Call to recvfrom failed\n");
      return (1);
    }
  }
  printf("Received message: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<readMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
lea    -0x24(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11c4 <readMessage+0x94>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
jne    11aa <readMessage+0x7a>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    11dd <readMessage+0xad>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11dd <readMessage+0xad>
mov    -0x10(%rbp),%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMessage>:
push   %rbp
push   %rbx
push   %rax
mov    %rcx,%rax
mov    %rsi,%rbx
mov    %r8d,0x4(%rsp)
xor    %ebp,%ebp
lea    0x4(%rsp),%r9
xor    %ecx,%ecx
mov    %rax,%r8
call   1060 <recvfrom@plt>
test   %eax,%eax
js     1176 <readMessage+0x36>
lea    0xea6(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b9 <readMessage+0x79>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
jne    11a8 <readMessage+0x68>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%ebp
jmp    11b9 <readMessage+0x79>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"socket timeout\0A\00"
@.str.1 = [25 x i8] c"Call to recvfrom failed\0A\00"
@.str.2 = [22 x i8] c"Received message: %s\0A\00"
define i32 @readMessage(i32 %0, i8* %1, i32 %2, %struct.sockaddr* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store %struct.sockaddr* %3, %struct.sockaddr** %10
store i32 %4, i32* %11
%13 = load i32, i32* %7
%14 = load i8*, i8** %8
%15 = load i32, i32* %9
%16 = load %struct.sockaddr*, %struct.sockaddr** %10
%17 = call i32 @recvfrom(i32 %13, i8* %14, i32 %15, i32 0, %struct.sockaddr* %16, i32* %11)
store i32 %17, i32* %12
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %5
%20 = load i64, i64* @errno
%21 = load i64, i64* @EWOULDBLOCK
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %6
br label %31
26: ; preds = %19
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %6
br label %31
28: ; preds = %5
%29 = load i8*, i8** %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store i32 0, i32* %6
br label %31
31: ; preds = %28, %26, %23
%32 = load i32, i32* %6
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"socket timeout\0A\00"
@.str.2 = [22 x i8] c"Received message: %s\0A\00"
@str = [24 x i8] c"Call to recvfrom failed\00"
define i32 @readMessage(i32 %0, i8* %1, i32 %2, %struct.sockaddr* %3, i32 %4) {
%6 = alloca i32
store i32 %4, i32* %6
%7 = call i32 @recvfrom(i32 %0, i8* %1, i32 %2, i32 0, %struct.sockaddr* %3, i32* %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %5
%10 = load i64, i64* @errno
%11 = load i64, i64* @EWOULDBLOCK
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %20
16: ; preds = %9
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %20
18: ; preds = %5
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %20
20: ; preds = %18, %16, %13
%21 = phi i32 [ 2, %13 ], [ 1, %16 ], [ 0, %18 ]
ret i32 %21
}