#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ recvbuf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sock;
  if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
    do {
      perror("socket");
      exit(EXIT_FAILURE);
    } while (0);
  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(5188);
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    do {
      perror("bind");
      exit(EXIT_FAILURE);
    } while (0);
  sendto(sock, "ABCD", 4, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
  char recvbuf[1];
  int n;
  int i;
  for (i = 0; i < 4; i++) {
    n = recvfrom(sock, recvbuf, sizeof(recvbuf), 0, NULL, NULL);
    if (n == -1) {
      if (errno == EINTR)
        continue;
      do {
        perror("recvfrom");
        exit(EXIT_FAILURE);
      } while (0);
    } else if (n > 0)
      printf("n=%d %c\n", n, recvbuf[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f3 <main+0x53>
jmp    11d9 <main+0x39>
lea    0xe20(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1444,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1267 <main+0xc7>
jmp    124d <main+0xad>
lea    0xdb3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%r8
lea    0xd97(%rip),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
jge    1333 <main+0x193>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1080 <recvfrom@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12fe <main+0x15e>
mov    0x2d01(%rip),%rax
mov    (%rax),%rax
mov    0x2cdf(%rip),%rcx
cmp    (%rcx),%rax
jne    12df <main+0x13f>
jmp    1325 <main+0x185>
jmp    12e4 <main+0x144>
lea    0xd26(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jle    131b <main+0x17b>
mov    -0x20(%rbp),%esi
lea    0xd08(%rip),%rdi
movsbl -0x19(%rbp),%edx
call   1040 <printf@plt>
jmp    1320 <main+0x180>
jmp    1325 <main+0x185>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    128e <main+0xee>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1392 <main+0x1f2>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x1444,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     139b <main+0x1fb>
lea    0xde2(%rip),%rsi
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     127a <main+0xda>
test   %eax,%eax
jle    1294 <main+0xf4>
movsbl 0x7(%rsp),%edx
lea    0xda9(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    1294 <main+0xf4>
mov    0x2d47(%rip),%rax
mov    (%rax),%rax
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%rax
jne    1389 <main+0x1e9>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     12cb <main+0x12b>
test   %eax,%eax
jle    12e5 <main+0x145>
movsbl 0x7(%rsp),%edx
lea    0xd58(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    12e5 <main+0x145>
mov    0x2cf6(%rip),%rax
mov    (%rax),%rax
mov    0x2cd4(%rip),%rcx
cmp    (%rcx),%rax
jne    1389 <main+0x1e9>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     131c <main+0x17c>
test   %eax,%eax
jle    1332 <main+0x192>
movsbl 0x7(%rsp),%edx
lea    0xd07(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    1332 <main+0x192>
mov    0x2ca5(%rip),%rax
mov    (%rax),%rax
mov    0x2c83(%rip),%rcx
cmp    (%rcx),%rax
jne    1389 <main+0x1e9>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1369 <main+0x1c9>
test   %eax,%eax
jle    137f <main+0x1df>
movsbl 0x7(%rsp),%edx
lea    0xcba(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    137f <main+0x1df>
mov    0x2c58(%rip),%rax
mov    (%rax),%rax
mov    0x2c36(%rip),%rcx
cmp    (%rcx),%rax
jne    1389 <main+0x1e9>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xc81(%rip),%rdi
jmp    13a2 <main+0x202>
lea    0xc67(%rip),%rdi
jmp    13a2 <main+0x202>
lea    0xc65(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c0a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [5 x i8] c"ABCD\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [9 x i8] c"recvfrom\00"
@.str.4 = [9 x i8] c"n=%d %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [1 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %0
br label %12
12: ; preds = %11
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; No predecessors!
br label %17
17: ; preds = %16, %0
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 5188)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %17
br label %32
32: ; preds = %31
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; No predecessors!
br label %37
37: ; preds = %36, %17
%38 = load i32, i32* %2
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @sendto(i32 %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4, i32 0, %struct.sockaddr* %39, i32 12)
store i32 0, i32* %6
br label %41
41: ; preds = %71, %37
%42 = load i32, i32* %6
%43 = icmp slt i32 %42, 4
br i1 %43, label %44, label %74
44: ; preds = %41
%45 = load i32, i32* %2
%46 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%47 = call i32 @recvfrom(i32 %45, i8* %46, i32 1, i32 0, i32* null, i32* null)
store i32 %47, i32* %5
%48 = load i32, i32* %5
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %61
50: ; preds = %44
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %71
55: ; preds = %50
br label %56
56: ; preds = %55
%57 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; No predecessors!
br label %70
61: ; preds = %44
%62 = load i32, i32* %5
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %61
%65 = load i32, i32* %5
%66 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%67 = load i8, i8* %66
%68 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %65, i8 signext %67)
br label %69
69: ; preds = %64, %61
br label %70
70: ; preds = %69, %60
br label %71
71: ; preds = %70, %54
%72 = load i32, i32* %6
%73 = add nsw i32 %72, 1
store i32 %73, i32* %6
br label %41
74: ; preds = %41
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [5 x i8] c"ABCD\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [9 x i8] c"recvfrom\00"
@.str.4 = [9 x i8] c"n=%d %c\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1 x i8]
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = bitcast %struct.sockaddr_in* %1 to i8*
%13 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 5188)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i64 @bind(i32 %5, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %11
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %11
%29 = call i32 @sendto(i32 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4, i32 0, %struct.sockaddr* %21, i32 12)
%30 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
%31 = call i32 @recvfrom(i32 %5, i8* %30, i32 1, i32 0, i32* null, i32* null)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %41
33: ; preds = %28
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %46, label %37
37: ; preds = %78, %66, %54, %33
%38 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %28
%42 = icmp sgt i32 %31, 0
br i1 %42, label %43, label %46
43: ; preds = %41
%44 = load i8, i8* %30
%45 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %31, i8 signext %44)
br label %46
46: ; preds = %43, %41, %33
%47 = call i32 @recvfrom(i32 %5, i8* %30, i32 1, i32 0, i32* null, i32* null)
%48 = icmp eq i32 %47, -1
br i1 %48, label %54, label %49
49: ; preds = %46
%50 = icmp sgt i32 %47, 0
br i1 %50, label %51, label %58
51: ; preds = %49
%52 = load i8, i8* %30
%53 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %47, i8 signext %52)
br label %58
54: ; preds = %46
%55 = load i64, i64* @errno
%56 = load i64, i64* @EINTR
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %37
58: ; preds = %54, %51, %49
%59 = call i32 @recvfrom(i32 %5, i8* %30, i32 1, i32 0, i32* null, i32* null)
%60 = icmp eq i32 %59, -1
br i1 %60, label %66, label %61
61: ; preds = %58
%62 = icmp sgt i32 %59, 0
br i1 %62, label %63, label %70
63: ; preds = %61
%64 = load i8, i8* %30
%65 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %59, i8 signext %64)
br label %70
66: ; preds = %58
%67 = load i64, i64* @errno
%68 = load i64, i64* @EINTR
%69 = icmp eq i64 %67, %68
br i1 %69, label %70, label %37
70: ; preds = %66, %63, %61
%71 = call i32 @recvfrom(i32 %5, i8* %30, i32 1, i32 0, i32* null, i32* null)
%72 = icmp eq i32 %71, -1
br i1 %72, label %78, label %73
73: ; preds = %70
%74 = icmp sgt i32 %71, 0
br i1 %74, label %75, label %82
75: ; preds = %73
%76 = load i8, i8* %30
%77 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %71, i8 signext %76)
br label %82
78: ; preds = %70
%79 = load i64, i64* @errno
%80 = load i64, i64* @EINTR
%81 = icmp eq i64 %79, %80
br i1 %81, label %82, label %37
82: ; preds = %78, %75, %73
ret i32 0
}