#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*/ fsin;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ errexit(char *, ...);
int /*<<< orphan*/ errno;
int passiveUDP(char *);
scalar_t__ recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                    unsigned int *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char *service = "echo";
  struct sockaddr_in fsin;
  int sock;
  unsigned int alen;
  char buf[4096];
  int nrec;
  switch (argc) {
  case 1:
    break;
  case 2:
    service = argv[1];
    break;
  default:
    errexit("usage: UDPechod [port]\n");
  }
  sock = passiveUDP(service);
  while (1) {
    alen = sizeof(fsin);
    if (nrec = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin,
                        &alen) < 0)
      errexit("recvfrom: %s\n", strerror(errno));
    (void)sendto(sock, buf, nrec, 0, (struct sockaddr *)&fsin, sizeof(fsin));
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1038(%rbp)
sub    $0x1,%eax
je     119f <main+0x4f>
jmp    118b <main+0x3b>
mov    -0x1038(%rbp),%eax
sub    $0x2,%eax
je     11a4 <main+0x54>
jmp    11b5 <main+0x65>
jmp    11c3 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11c3 <main+0x73>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <errexit@plt>
mov    -0x18(%rbp),%rdi
call   1060 <passiveUDP@plt>
mov    %eax,-0x24(%rbp)
movl   $0x4,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x1030(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x1000,%edx
xor    %ecx,%ecx
lea    -0x28(%rbp),%r9
call   1050 <recvfrom@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1034(%rbp)
test   $0x1,%al
jne    1216 <main+0xc6>
jmp    1234 <main+0xe4>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <errexit@plt>
mov    -0x24(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1034(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1030 <sendto@plt>
jmp    11cf <main+0x7f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
lea    0xe98(%rip),%rbx
cmp    $0x1,%edi
je     1186 <main+0x36>
cmp    $0x2,%edi
jne    1178 <main+0x28>
mov    0x8(%rsi),%rbx
jmp    1186 <main+0x36>
lea    0xe86(%rip),%rdi
xor    %eax,%eax
call   1040 <errexit@plt>
mov    %rbx,%rdi
call   1060 <passiveUDP@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%r12
lea    0xe77(%rip),%r13
jmp    11cb <main+0x7b>
nopl   0x0(%rax,%rax,1)
shr    $0x3f,%rbp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0x4,%r9d
call   1030 <sendto@plt>
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1050 <recvfrom@plt>
mov    %rax,%rbp
test   %rax,%rax
jns    11b0 <main+0x60>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <errexit@plt>
jmp    11b0 <main+0x60>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"echo\00"
@.str.1 = [24 x i8] c"usage: UDPechod [port]\0A\00"
@.str.2 = [14 x i8] c"recvfrom: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %6
%12 = load i32, i32* %4
switch i32 %12, label %18 [
i32 1, label %13
i32 2, label %14
]
13: ; preds = %2
br label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
br label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %14, %13
%21 = load i8*, i8** %6
%22 = call i32 @passiveUDP(i8* %21)
store i32 %22, i32* %8
br label %23
23: ; preds = %20, %34
store i32 4, i32* %9
%24 = load i32, i32* %8
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%26 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%27 = call i64 @recvfrom(i32 %24, i8* %25, i32 4096, i32 0, %struct.sockaddr* %26, i32* %9)
%28 = icmp slt i64 %27, 0
%29 = zext i1 %28 to i32
store i32 %29, i32* %11
br i1 %28, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %30, %23
%35 = load i32, i32* %8
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%37 = load i32, i32* %11
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = call i32 @sendto(i32 %35, i8* %36, i32 %37, i32 0, %struct.sockaddr* %38, i32 4)
br label %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"echo\00"
@.str.1 = [24 x i8] c"usage: UDPechod [port]\0A\00"
@.str.2 = [14 x i8] c"recvfrom: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
switch i32 %0, label %12 [
i32 1, label %14
i32 2, label %9
]
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
br label %14
12: ; preds = %2
%13 = tail call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %9, %2
%15 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %12 ], [ %11, %9 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ]
%16 = tail call i32 @passiveUDP(i8* %15)
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %18
18: ; preds = %27, %14
store i32 4, i32* %4
%19 = call i64 @recvfrom(i32 %16, i8* %8, i32 4096, i32 0, %struct.sockaddr* %17, i32* %4)
%20 = icmp slt i64 %19, 0
%21 = lshr i64 %19, 63
%22 = trunc i64 %21 to i32
br i1 %20, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 (i8*, ...) @errexit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %18
%28 = call i32 @sendto(i32 %16, i8* %8, i32 %22, i32 0, %struct.sockaddr* %17, i32 4)
br label %18
}