#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 s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ me;
typedef int /*<<< orphan*/ it;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int 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) {
    int s = socket(PF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in me, it;
    int melen = sizeof(me), itlen = sizeof(it);
    memset((char *)&me, 0, sizeof(me));
    me.sin_family = AF_INET;
    me.sin_addr.s_addr = inet_addr(argv[1]);
    me.sin_port = htons(atoi(argv[2]));
    int bound = bind(s, (struct sockaddr *)&me, melen);
    if (bound < 0) {
      perror("bind fails: ");
      return -1;
    }
    printf("bound to //%08x:%d/\n", me.sin_addr.s_addr,
           (int)ntohs(me.sin_port));
    char buf[128];
    while (1) {
      memset((char *)&it, 0, sizeof(it));
      memset(buf, 0, sizeof(buf));
      int received =
          recvfrom(s, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&it, &itlen);
      if (received < 0) {
        perror("receive fails: ");
        break;
      } else {
        buf[received] = 0;
        printf("received from //%08x/ '%s'\n", it.sin_addr.s_addr, buf);
      }
      int sent = sendto(s, buf, received, 0, (struct sockaddr *)&it, itlen);
      if (sent < 0) {
        perror("send fails: ");
        break;
      }
    }
  } else
    printf("ses <local ip> <local port>\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    1374 <main+0x1c4>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
movl   $0xc,-0x34(%rbp)
movl   $0xc,-0x38(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   10a0 <bind@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1274 <main+0xc4>
lea    0xd9d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1389 <main+0x1d9>
mov    -0x20(%rbp),%eax
mov    %eax,-0xcc(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0xcc(%rbp),%esi
mov    %eax,%edx
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0xc0(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x7f,%edx
xor    %ecx,%ecx
lea    -0x38(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0xc4(%rbp)
cmpl   $0x0,-0xc4(%rbp)
jge    1300 <main+0x150>
lea    0xd2c(%rip),%rdi
call   10b0 <perror@plt>
jmp    136f <main+0x1bf>
movslq -0xc4(%rbp),%rax
movb   $0x0,-0xc0(%rbp,%rax,1)
mov    -0x30(%rbp),%esi
lea    -0xc0(%rbp),%rdx
lea    0xd12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    -0xc4(%rbp),%edx
lea    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r9d
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,-0xc8(%rbp)
cmpl   $0x0,-0xc8(%rbp)
jge    136a <main+0x1ba>
lea    0xcee(%rip),%rdi
call   10b0 <perror@plt>
jmp    136f <main+0x1bf>
jmp    129b <main+0xeb>
jmp    1382 <main+0x1d2>
lea    0xce0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
cmp    $0x3,%edi
je     11f6 <main+0x36>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsi,%rbx
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r15d
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rbx),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    0x10(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %eax,%eax
jns    1289 <main+0xc9>
lea    0xd86(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
jmp    11e4 <main+0x24>
mov    0x10(%rsp),%ebx
mov    0x14(%rsp),%edi
call   10a0 <ntohs@plt>
lea    0xd70(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x20(%rsp),%rbx
lea    0x30(%rsp),%rbp
lea    0xc(%rsp),%r12
lea    0xd74(%rip),%r13
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x7f,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
mov    %r12,%r9
call   1090 <recvfrom@plt>
test   %eax,%eax
js     1342 <main+0x182>
mov    %eax,%r14d
mov    %eax,%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    0x20(%rsp),%esi
mov    %r13,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%r9d
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1070 <sendto@plt>
test   %eax,%eax
jns    12c0 <main+0x100>
lea    0xd16(%rip),%rdi
call   10c0 <perror@plt>
jmp    11e2 <main+0x22>
lea    0xcd9(%rip),%rdi
call   10c0 <perror@plt>
jmp    11e2 <main+0x22>

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
@AF_INET = global i32 0
@.str = [13 x i8] c"bind fails: \00"
@.str.1 = [21 x i8] c"bound to //%08x:%d/\0A\00"
@.str.2 = [16 x i8] c"receive fails: \00"
@.str.3 = [28 x i8] c"received from //%08x/ '%s'\0A\00"
@.str.4 = [13 x i8] c"send fails: \00"
@.str.5 = [29 x i8] c"ses <local ip> <local port>\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [128 x i8]
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 3
br i1 %16, label %17, label %89
17: ; preds = %2
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
store i32 12, i32* %9
store i32 12, i32* %10
%21 = bitcast %struct.sockaddr_in* %7 to i8*
%22 = call i32 @memset(i8* %21, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @inet_addr(i8* %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = load i32, i32* %9
%40 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 %39)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %17
%44 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %92
45: ; preds = %17
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i64 @ntohs(i32 %50)
%52 = trunc i64 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %48, i32 %52)
br label %54
54: ; preds = %45, %87
%55 = bitcast %struct.sockaddr_in* %8 to i8*
%56 = call i32 @memset(i8* %55, i32 0, i32 12)
%57 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%58 = call i32 @memset(i8* %57, i32 0, i32 128)
%59 = load i32, i32* %6
%60 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%61 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%62 = call i32 @recvfrom(i32 %59, i8* %60, i32 127, i32 0, %struct.sockaddr* %61, i32* %10)
store i32 %62, i32* %13
%63 = load i32, i32* %13
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %54
%66 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %88
67: ; preds = %54
%68 = load i32, i32* %13
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 %69
store i8 0, i8* %70
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%72 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %73, i8* %74)
br label %76
76: ; preds = %67
%77 = load i32, i32* %6
%78 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%79 = load i32, i32* %13
%80 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%81 = load i32, i32* %10
%82 = call i32 @sendto(i32 %77, i8* %78, i32 %79, i32 0, %struct.sockaddr* %80, i32 %81)
store i32 %82, i32* %14
%83 = load i32, i32* %14
%84 = icmp slt i32 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %76
%86 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %88
87: ; preds = %76
br label %54
88: ; preds = %85, %65
br label %91
89: ; preds = %2
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %91
91: ; preds = %89, %88
store i32 0, i32* %3
br label %92
92: ; preds = %91, %43
%93 = load i32, i32* %3
ret i32 %93
}

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
@AF_INET = global i32 0
@.str = [13 x i8] c"bind fails: \00"
@.str.1 = [21 x i8] c"bound to //%08x:%d/\0A\00"
@.str.2 = [16 x i8] c"receive fails: \00"
@.str.3 = [28 x i8] c"received from //%08x/ '%s'\0A\00"
@.str.4 = [13 x i8] c"send fails: \00"
@str = [28 x i8] c"ses <local ip> <local port>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = icmp eq i32 %0, 3
br i1 %7, label %8, label %57
8: ; preds = %2
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = bitcast %struct.sockaddr_in* %3 to i8*
%13 = bitcast %struct.sockaddr_in* %4 to i8*
%14 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%15 = call i32 @memset(i8* %12, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @inet_addr(i8* %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @bind(i32 %11, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %8
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %59
32: ; preds = %8
%33 = load i32, i32* %21
%34 = load i32, i32* %26
%35 = call i64 @ntohs(i32 %34)
%36 = trunc i64 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %36)
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%39 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %41
41: ; preds = %46, %32
%42 = call i32 @memset(i8* %13, i32 0, i32 12)
%43 = call i32 @memset(i8* %38, i32 0, i32 128)
%44 = call i32 @recvfrom(i32 %11, i8* %38, i32 127, i32 0, %struct.sockaddr* %39, i32* %5)
%45 = icmp slt i32 %44, 0
br i1 %45, label %54, label %46
46: ; preds = %41
%47 = zext i32 %44 to i64
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %47
store i8 0, i8* %48
%49 = load i32, i32* %40
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %49, i8* %38)
%51 = load i32, i32* %5
%52 = call i32 @sendto(i32 %11, i8* %38, i32 %44, i32 0, %struct.sockaddr* %39, i32 %51)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %41
54: ; preds = %46, %41
%55 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %41 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %46 ]
%56 = call i32 @perror(i8* %55)
br label %59
57: ; preds = %2
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %59
59: ; preds = %54, %57, %30
%60 = phi i32 [ -1, %30 ], [ 0, %54 ], [ 0, %57 ]
ret i32 %60
}