#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*/ s;
typedef int /*<<< orphan*/ d;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
scalar_t__ parse_address(char *, struct sockaddr_in *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 3) {
    printf("usage: udp_relay <source> <dest1> [ <dest2> ... ]\n");
    exit(1);
  }
  struct sockaddr_in s;
  struct sockaddr_in d[10];
  int dcount = 0, i;
  if (parse_address(argv[1], &s)) {
    printf("error: could not resolve source address: %s\n", argv[1]);
    return 1;
  }
  for (i = 2; i < argc && dcount < 10; i++) {
    if (parse_address(argv[i], &d[dcount])) {
      printf("error: could not resolve destination address: %s\n", argv[i]);
      return 1;
    }
    dcount++;
  }
  int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (sock < 0) {
    perror("socket()");
    return 1;
  }
  if (bind(sock, (struct sockaddr *)&s, sizeof(s))) {
    perror("bind()");
    return 1;
  }
  char buffer[65536];
  while (1) {
    int n = recv(sock, buffer, sizeof(buffer), 0);
    if (n < 0)
      continue;
    for (i = 0; i < dcount; i++) {
      sendto(sock, buffer, n, 0, (struct sockaddr *)&d[i], sizeof(d[i]));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10060,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11bb <main+0x3b>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <parse_address@plt>
cmp    $0x0,%rax
je     11ff <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139e <main+0x21e>
movl   $0x2,-0x48(%rbp)
mov    -0x48(%rbp),%ecx
xor    %eax,%eax
cmp    -0x8(%rbp),%ecx
mov    %al,-0x10055(%rbp)
jge    1227 <main+0xa7>
cmpl   $0xa,-0x44(%rbp)
setl   %al
mov    %al,-0x10055(%rbp)
mov    -0x10055(%rbp),%al
test   $0x1,%al
jne    123a <main+0xba>
jmp    12a1 <main+0x121>
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
movslq -0x44(%rbp),%rax
lea    -0x40(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
call   1090 <parse_address@plt>
cmp    $0x0,%rax
je     128a <main+0x10a>
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139e <main+0x21e>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1206 <main+0x86>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
mov    0x2d2e(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    12e6 <main+0x166>
lea    0xdbd(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139e <main+0x21e>
mov    -0x4c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
je     1319 <main+0x199>
lea    0xd93(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139e <main+0x21e>
jmp    131e <main+0x19e>
mov    -0x4c(%rbp),%edi
lea    -0x10050(%rbp),%rsi
mov    $0x10000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x10054(%rbp)
cmpl   $0x0,-0x10054(%rbp)
jge    134c <main+0x1cc>
jmp    131e <main+0x19e>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    1399 <main+0x219>
mov    -0x4c(%rbp),%edi
lea    -0x10050(%rbp),%rsi
mov    -0x10054(%rbp),%edx
movslq -0x48(%rbp),%rax
lea    -0x40(%rbp),%r8
shl    $0x2,%rax
add    %rax,%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1050 <sendto@plt>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1353 <main+0x1d3>
jmp    131e <main+0x19e>
mov    -0x4(%rbp),%eax
add    $0x10060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10048,%rsp
cmp    $0x2,%edi
jle    12f5 <main+0x165>
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   10a0 <parse_address@plt>
test   %rax,%rax
je     11d2 <main+0x42>
mov    0x8(%r15),%rsi
lea    0xe33(%rip),%rdi
jmp    12c0 <main+0x130>
add    $0xfffffffd,%ebp
cmp    $0x9,%ebp
mov    $0x9,%eax
cmovb  %ebp,%eax
lea    0x1(%rax),%r14d
lea    0x4(,%rax,4),%rbx
xor    %ebp,%ebp
xchg   %ax,%ax
mov    0x10(%r15,%rbp,2),%rdi
lea    (%rsp,%rbp,1),%rsi
add    $0x10,%rsi
call   10a0 <parse_address@plt>
test   %rax,%rax
jne    12b4 <main+0x124>
add    $0x4,%rbp
cmp    %rbp,%rbx
jne    11f0 <main+0x60>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12de <main+0x14e>
mov    %eax,%r15d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    12e7 <main+0x157>
mov    %r14d,%r13d
lea    0x40(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x10000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1270 <main+0xe0>
mov    %eax,%ebp
mov    %r13,%r12
lea    0x10(%rsp),%rbx
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    $0x4,%r9d
call   1060 <sendto@plt>
add    $0x4,%rbx
add    $0xffffffffffffffff,%r12
jne    1290 <main+0x100>
jmp    1270 <main+0xe0>
mov    0x10(%r15,%rbp,2),%rsi
lea    0xd6d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x10048,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7a(%rip),%rdi
jmp    12ee <main+0x15e>
lea    0xd7a(%rip),%rdi
call   1080 <perror@plt>
jmp    12c7 <main+0x137>
lea    0xd73(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [51 x i8] c"usage: udp_relay <source> <dest1> [ <dest2> ... ]\0A\00"
@.str.1 = [45 x i8] c"error: could not resolve source address: %s\0A\00"
@.str.2 = [50 x i8] c"error: could not resolve destination address: %s\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str.3 = [9 x i8] c"socket()\00"
@.str.4 = [7 x i8] c"bind()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca [10 x %struct.sockaddr_in]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [65536 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
store i32 0, i32* %8
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @parse_address(i8* %21, %struct.sockaddr_in* %6)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 1, i32* %3
br label %105
29: ; preds = %18
store i32 2, i32* %9
br label %30
30: ; preds = %60, %29
%31 = load i32, i32* %9
%32 = load i32, i32* %4
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 10
br label %37
37: ; preds = %34, %30
%38 = phi i1 [ false, %30 ], [ %36, %34 ]
br i1 %38, label %39, label %63
39: ; preds = %37
%40 = load i8**, i8*** %5
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %40, i64 %42
%44 = load i8*, i8** %43
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [10 x %struct.sockaddr_in], [10 x %struct.sockaddr_in]* %7, i64 0, i64 %46
%48 = call i64 @parse_address(i8* %44, %struct.sockaddr_in* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %39
%51 = load i8**, i8*** %5
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8*, i8** %51, i64 %53
%55 = load i8*, i8** %54
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %55)
store i32 1, i32* %3
br label %105
57: ; preds = %39
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %60
60: ; preds = %57
%61 = load i32, i32* %9
%62 = add nsw i32 %61, 1
store i32 %62, i32* %9
br label %30
63: ; preds = %37
%64 = load i32, i32* @PF_INET
%65 = load i32, i32* @SOCK_DGRAM
%66 = load i32, i32* @IPPROTO_IP
%67 = call i32 @socket(i32 %64, i32 %65, i32 %66)
store i32 %67, i32* %10
%68 = load i32, i32* %10
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %63
%71 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %105
72: ; preds = %63
%73 = load i32, i32* %10
%74 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%75 = call i64 @bind(i32 %73, %struct.sockaddr* %74, i32 4)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %72
%78 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %105
79: ; preds = %72
br label %80
80: ; preds = %79, %86, %104
%81 = load i32, i32* %10
%82 = getelementptr inbounds [65536 x i8], [65536 x i8]* %11, i64 0, i64 0
%83 = call i32 @recv(i32 %81, i8* %82, i32 65536, i32 0)
store i32 %83, i32* %12
%84 = load i32, i32* %12
%85 = icmp slt i32 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %80
br label %80
87: ; preds = %80
store i32 0, i32* %9
br label %88
88: ; preds = %101, %87
%89 = load i32, i32* %9
%90 = load i32, i32* %8
%91 = icmp slt i32 %89, %90
br i1 %91, label %92, label %104
92: ; preds = %88
%93 = load i32, i32* %10
%94 = getelementptr inbounds [65536 x i8], [65536 x i8]* %11, i64 0, i64 0
%95 = load i32, i32* %12
%96 = load i32, i32* %9
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds [10 x %struct.sockaddr_in], [10 x %struct.sockaddr_in]* %7, i64 0, i64 %97
%99 = bitcast %struct.sockaddr_in* %98 to %struct.sockaddr*
%100 = call i32 @sendto(i32 %93, i8* %94, i32 %95, i32 0, %struct.sockaddr* %99, i32 4)
br label %101
101: ; preds = %92
%102 = load i32, i32* %9
%103 = add nsw i32 %102, 1
store i32 %103, i32* %9
br label %88
104: ; preds = %88
br label %80
105: ; preds = %77, %70, %50, %24
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [45 x i8] c"error: could not resolve source address: %s\0A\00"
@.str.2 = [50 x i8] c"error: could not resolve destination address: %s\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str.3 = [9 x i8] c"socket()\00"
@.str.4 = [7 x i8] c"bind()\00"
@str = [50 x i8] c"usage: udp_relay <source> <dest1> [ <dest2> ... ]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [10 x %struct.sockaddr_in]
%5 = alloca [65536 x i8]
%6 = icmp slt i32 %0, 3
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = bitcast [10 x %struct.sockaddr_in]* %4 to i8*
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @parse_address(i8* %14, %struct.sockaddr_in* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = add nsw i32 %0, -3
%19 = call i32 @llvm.umin.i32(i32 %18, i32 9)
%20 = add nuw nsw i32 %19, 1
%21 = zext i32 %20 to i64
br label %25
22: ; preds = %10
%23 = load i8*, i8** %13
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %70
25: ; preds = %17, %38
%26 = phi i64 [ 2, %17 ], [ %40, %38 ]
%27 = phi i64 [ 0, %17 ], [ %39, %38 ]
%28 = getelementptr inbounds i8*, i8** %1, i64 %26
%29 = load i8*, i8** %28
%30 = getelementptr inbounds [10 x %struct.sockaddr_in], [10 x %struct.sockaddr_in]* %4, i64 0, i64 %27
%31 = call i64 @parse_address(i8* %29, %struct.sockaddr_in* %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %25
%34 = and i64 %26, 4294967295
%35 = getelementptr inbounds i8*, i8** %1, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %70
38: ; preds = %25
%39 = add nuw nsw i64 %27, 1
%40 = add nuw nsw i64 %26, 1
%41 = icmp eq i64 %39, %21
br i1 %41, label %42, label %25
42: ; preds = %38
%43 = load i32, i32* @PF_INET
%44 = load i32, i32* @SOCK_DGRAM
%45 = load i32, i32* @IPPROTO_IP
%46 = call i32 @socket(i32 %43, i32 %44, i32 %45)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %70
50: ; preds = %42
%51 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%52 = call i64 @bind(i32 %46, %struct.sockaddr* %51, i32 4)
%53 = icmp eq i64 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %70
56: ; preds = %50
%57 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
%58 = zext i32 %20 to i64
br label %59
59: ; preds = %62, %56
%60 = call i32 @recv(i32 %46, i8* %57, i32 65536, i32 0)
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %63, %59
br label %59
63: ; preds = %59, %63
%64 = phi i64 [ %68, %63 ], [ 0, %59 ]
%65 = getelementptr inbounds [10 x %struct.sockaddr_in], [10 x %struct.sockaddr_in]* %4, i64 0, i64 %64
%66 = bitcast %struct.sockaddr_in* %65 to %struct.sockaddr*
%67 = call i32 @sendto(i32 %46, i8* %57, i32 %60, i32 0, %struct.sockaddr* %66, i32 4)
%68 = add nuw nsw i64 %64, 1
%69 = icmp eq i64 %68, %58
br i1 %69, label %62, label %63
70: ; preds = %48, %54, %33, %22
ret i32 1
}