#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 {
  void *sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ servAddr;
typedef int /*<<< orphan*/ msg;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
void *htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ recvfrom(int, char *, int, int /*<<< orphan*/,
                            struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int recvSock, sendSock;
  struct sockaddr_in cliAddr;
  struct sockaddr_in servAddr;
  char msg[256];
  recvSock = socket(AF_INET, SOCK_DGRAM, 0);
  if (recvSock < 0) {
    perror("Can't open socket");
    return 1;
  }
  sendSock = socket(AF_INET, SOCK_DGRAM, 0);
  if (recvSock < 0) {
    perror("Can't open socket");
    return 1;
  }
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(8330);
  if (bind(recvSock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
    perror("Can't bind port");
    return 1;
  }
  for (;;) {
    socklen_t cliAddrLen;
    recvfrom(recvSock, msg, sizeof(msg), 0, (struct sockaddr *)&cliAddr,
             &cliAddrLen);
    cliAddr.sin_port = htons(4321);
    msg[0] = '!';
    msg[1] = 0;
    sendto(sendSock, msg, 1, 0, (struct sockaddr *)&cliAddr, cliAddrLen);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c7 <main+0x57>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1205 <main+0x95>
lea    0xe0c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x30(%rbp)
mov    $0x208a,%edi
call   1030 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1263 <main+0xf3>
lea    0xdc0(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
jmp    1268 <main+0xf8>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x144(%rbp),%r9
call   1060 <recvfrom@plt>
mov    $0x10e1,%edi
call   1030 <htons@plt>
mov    %rax,-0x28(%rbp)
movb   $0x21,-0x140(%rbp)
movb   $0x0,-0x13f(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    -0x144(%rbp),%r9d
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
jmp    1268 <main+0xf8>
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x130,%rsp
mov    0x2e62(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e49(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     124a <main+0xda>
mov    %eax,%r14d
mov    (%rbx),%edi
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%r15d
mov    (%rbx),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x18(%rsp)
mov    $0x208a,%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x10,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1253 <main+0xe3>
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%rbp
lea    0xc(%rsp),%r12
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r12,%r9
call   1060 <recvfrom@plt>
mov    $0x10e1,%edi
call   1030 <htons@plt>
mov    %rax,0x20(%rsp)
movw   $0x21,0x30(%rsp)
mov    0xc(%rsp),%r9d
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1050 <sendto@plt>
jmp    1200 <main+0x90>
lea    0xdaf(%rip),%rdi
jmp    125a <main+0xea>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"Can't open socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [16 x i8] c"Can't bind port\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca [256 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %57
19: ; preds = %2
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_DGRAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %7
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %57
27: ; preds = %19
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = call i32 @htonl(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = call i8* @htons(i32 8330)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i8* %34, i8** %35
%36 = load i32, i32* %6
%37 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%38 = call i64 @bind(i32 %36, %struct.sockaddr* %37, i32 16)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %27
%41 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %57
42: ; preds = %27
br label %43
43: ; preds = %43, %42
%44 = load i32, i32* %6
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%46 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%47 = call i32 @recvfrom(i32 %44, i8* %45, i32 256, i32 0, %struct.sockaddr* %46, i32* %11)
%48 = call i8* @htons(i32 4321)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i8* %48, i8** %49
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
store i8 33, i8* %50
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 1
store i8 0, i8* %51
%52 = load i32, i32* %7
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%54 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%55 = load i32, i32* %11
%56 = call i32 @sendto(i32 %52, i8* %53, i32 1, i32 0, %struct.sockaddr* %54, i32 %55)
br label %43
57: ; preds = %40, %25, %17
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [18 x i8] c"Can't open socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [16 x i8] c"Can't bind port\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %42
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = tail call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %23, i32* %24
%25 = tail call i8* @htons(i32 8330)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i8* %25, i8** %26
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @bind(i32 %12, %struct.sockaddr* %27, i32 16)
%29 = icmp slt i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %16
%31 = bitcast i32* %6 to i8*
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 1
br label %37
35: ; preds = %16
%36 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %42
37: ; preds = %30, %37
%38 = call i32 @recvfrom(i32 %12, i8* %9, i32 256, i32 0, %struct.sockaddr* %32, i32* %6)
%39 = call i8* @htons(i32 4321)
store i8* %39, i8** %33
store i8 33, i8* %9
store i8 0, i8* %34
%40 = load i32, i32* %6
%41 = call i32 @sendto(i32 %19, i8* %9, i32 1, i32 0, %struct.sockaddr* %32, i32 %40)
br label %37
42: ; preds = %35, %14
ret i32 1
}