#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*/ my_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main() {
  int sockfd;
  struct sockaddr_in my_addr;
  struct sockaddr_in their_addr;
  int addr_len, numbytes;
  char buf[100];
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("Listener socket");
    exit(1);
  }
  memset(&my_addr, 0, sizeof(my_addr));
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(4950);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("Listener bind");
    exit(1);
  }
  addr_len = sizeof(struct sockaddr);
  do {
    strcpy(buf, "");
    if ((numbytes = recvfrom(sockfd, buf, 100 - 1, 0,
                             (struct sockaddr *)&their_addr, &addr_len)) ==
        -1) {
      perror("Listener recvfrom");
      exit(1);
    }
    if ((numbytes =
             sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&their_addr,
                    sizeof(struct sockaddr))) == -1) {
      perror("echo fail");
      exit(1);
    }
    printf("Got packet from %s\n", inet_ntoa(their_addr.sin_addr));
    printf("Packet is %d bytes long\n", numbytes);
    buf[numbytes] = '\0';
    printf("Packet contains \"%s\"\n", buf);
    printf("echoing\n\n");
  } while (strcmp(buf, "quit") != 0);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    122d <main+0x4d>
lea    0xde2(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1356,%edi
call   1060 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1292 <main+0xb2>
lea    0xd8d(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
movl   $0x4,-0x2c(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x63,%edx
xor    %ecx,%ecx
lea    -0x2c(%rbp),%r9
call   10c0 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    12ec <main+0x10c>
lea    0xd41(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1090 <sendto@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    1351 <main+0x171>
lea    0xcee(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x28(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xcd7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%esi
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movslq -0x30(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rsi
lea    0xcd2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xccf(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    1299 <main+0xb9>
mov    -0x8(%rbp),%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2de0(%rip),%r14
mov    (%r14),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
je     1396 <main+0x1a6>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x1356,%edi
call   1070 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
je     139f <main+0x1af>
movl   $0x4,0xc(%rsp)
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0xdf5(%rip),%r13
lea    0xde9(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
lea    0xdd2(%rip),%rsi
call   1030 <strcpy@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    0xc(%rsp),%r9
call   10d0 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1377 <main+0x187>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0x4,%r9d
call   10a0 <sendto@plt>
cmp    $0xffffffff,%eax
je     1380 <main+0x190>
mov    %eax,%r12d
mov    0x20(%rsp),%edi
call   1050 <inet_ntoa@plt>
lea    0xd2e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xd31(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
movslq %r12d,%rax
movb   $0x0,0x30(%rsp,%rax,1)
lea    0xd31(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10c0 <strcmp@plt>
test   %rax,%rax
jne    12a0 <main+0xb0>
mov    %ebp,%edi
call   10b0 <close@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xca0(%rip),%rdi
jmp    1387 <main+0x197>
lea    0xca9(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xc63(%rip),%rdi
jmp    1387 <main+0x197>
lea    0xc6a(%rip),%rdi
jmp    1387 <main+0x197>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [16 x i8] c"Listener socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"Listener bind\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [18 x i8] c"Listener recvfrom\00"
@.str.4 = [10 x i8] c"echo fail\00"
@.str.5 = [20 x i8] c"Got packet from %s\0A\00"
@.str.6 = [25 x i8] c"Packet is %d bytes long\0A\00"
@.str.7 = [22 x i8] c"Packet contains \22%s\22\0A\00"
@.str.8 = [10 x i8] c"echoing\0A\0A\00"
@.str.9 = [5 x i8] c"quit\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca [100 x i8]
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 4950)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %15
%29 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %15
store i32 4, i32* %5
br label %32
32: ; preds = %68, %31
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %2
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i32 @recvfrom(i32 %35, i8* %36, i32 99, i32 0, %struct.sockaddr* %37, i32* %5)
store i32 %38, i32* %6
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
%44 = load i32, i32* %2
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%47 = call i32 @strlen(i8* %46)
%48 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%49 = call i32 @sendto(i32 %44, i8* %45, i32 %47, i32 0, %struct.sockaddr* %48, i32 4)
store i32 %49, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %43
%52 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %43
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i8* @inet_ntoa(i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %58)
%60 = load i32, i32* %6
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %68
68: ; preds = %54
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%70 = call i64 @strcmp(i8* %69, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%71 = icmp ne i64 %70, 0
br i1 %71, label %32, label %72
72: ; preds = %68
%73 = load i32, i32* %2
%74 = call i32 @close(i32 %73)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [16 x i8] c"Listener socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"Listener bind\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [18 x i8] c"Listener recvfrom\00"
@.str.4 = [10 x i8] c"echo fail\00"
@.str.5 = [20 x i8] c"Got packet from %s\0A\00"
@.str.6 = [25 x i8] c"Packet is %d bytes long\0A\00"
@.str.7 = [22 x i8] c"Packet contains \22%s\22\0A\00"
@.str.9 = [5 x i8] c"quit\00"
@str = [9 x i8] c"echoing\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 4950)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i32 @bind(i32 %11, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %16
store i32 4, i32* %3
%31 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
br label %33
33: ; preds = %47, %30
%34 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @recvfrom(i32 %11, i8* %8, i32 99, i32 0, %struct.sockaddr* %31, i32* %3)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = call i32 @strlen(i8* %8)
%42 = call i32 @sendto(i32 %11, i8* %8, i32 %41, i32 0, %struct.sockaddr* %31, i32 4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %40
%48 = load i32, i32* %32
%49 = call i8* @inet_ntoa(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %42)
%52 = sext i32 %42 to i64
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %52
store i8 0, i8* %53
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %8)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%56 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %33
58: ; preds = %47
%59 = call i32 @close(i32 %11)
ret i32 0
}