#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_zero;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* 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(int /*<<< orphan*/, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sockfd;
  struct sockaddr_in my_addr;
  struct sockaddr_in their_addr;
  socklen_t addr_len;
  int numbytes;
  char buf[100];
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(4950);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
  if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
    perror("bind");
    exit(1);
  }
  addr_len = sizeof their_addr;
  if ((numbytes = recvfrom(sockfd, buf, 100 - 1, 0,
                           (struct sockaddr *)&their_addr, &addr_len)) == -1) {
    perror("recvfrom");
    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);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11ed <main+0x4d>
lea    0xe22(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x1356,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1251 <main+0xb1>
lea    0xdc5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x10,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x63,%edx
xor    %ecx,%ecx
lea    -0x2c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    1298 <main+0xf8>
lea    0xd83(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%esi
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movslq -0x30(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rsi
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x98,%rsp
mov    0x2e38(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     129a <main+0xfa>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x1c(%rsp)
mov    $0x1356,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x103>
movl   $0x10,0xc(%rsp)
lea    0x30(%rsp),%rsi
lea    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     12ac <main+0x10c>
mov    %eax,%ebp
mov    0x20(%rsp),%edi
call   1030 <inet_ntoa@plt>
lea    0xdc3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdc6(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movslq %ebp,%rax
movb   $0x0,0x30(%rsp,%rax,1)
lea    0xdc7(%rip),%rdi
lea    0x30(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd5f(%rip),%rdi
jmp    12b3 <main+0x113>
lea    0xd5d(%rip),%rdi
jmp    12b3 <main+0x113>
lea    0xd59(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [9 x i8] c"recvfrom\00"
@.str.3 = [20 x i8] c"got packet from %s\0A\00"
@.str.4 = [25 x i8] c"packet is %d bytes long\0A\00"
@.str.5 = [22 x i8] c"packet contains \22%s\22\0A\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 ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 3
store i32 %16, i32* %17
%18 = call i32 @htons(i32 4950)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 @memset(i32 %24, i8 signext 0, i32 4)
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 16)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %15
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %15
store i32 16, i32* %5
%34 = load i32, i32* %2
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i32 @recvfrom(i32 %34, i8* %35, i32 99, i32 0, %struct.sockaddr* %36, i32* %5)
store i32 %37, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i8* @inet_ntoa(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %6
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 %51
store i8 0, i8* %52
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %2
%56 = call i32 @close(i32 %55)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [9 x i8] c"recvfrom\00"
@.str.3 = [20 x i8] c"got packet from %s\0A\00"
@.str.4 = [25 x i8] c"packet is %d bytes long\0A\00"
@.str.5 = [22 x i8] c"packet contains \22%s\22\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 ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 4950)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @memset(i32 undef, i8 signext 0, i32 4)
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i32 @bind(i32 %11, %struct.sockaddr* %24, i32 16)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %16
store i32 16, i32* %3
%31 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%32 = call i32 @recvfrom(i32 %11, i8* %8, i32 99, i32 0, %struct.sockaddr* %31, i32* %3)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i8* @inet_ntoa(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%43 = sext i32 %32 to i64
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %43
store i8 0, i8* %44
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%46 = call i32 @close(i32 %11)
ret i32 0
}