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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int recvfrom(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
             int *);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    puts("usage: program <port>");
    return 0;
  }
  unsigned char buf[1000];
  int sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
  if (sock == -1) {
    perror("socket error");
    return 1;
  } else {
    printf("socket ok\n");
  }
  struct sockaddr_in server = {.sin_family = AF_INET,
                               .sin_addr.s_addr = INADDR_ANY,
                               .sin_port = htons(atoi(argv[1]))};
  struct sockaddr_in other;
  int slen = sizeof(other);
  if (bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
    perror("bind error");
    close(sock);
    return 1;
  } else {
    printf("bind ok\n");
  }
  int numread =
      recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&other, &slen);
  if (numread == -1) {
    perror("recv error");
  } else {
    printf("recv %d bytes\n", numread - 28);
    printf("destinaton port %hhu %hhu\n", buf[22], buf[23]);
    for (int i = 28; i < numread; i++) {
      printf("0x%02X, ", buf[i]);
    }
    printf("\n");
  }
  shutdown(sock, 2);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11db <main+0x3b>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c2 <main+0x222>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x404(%rbp)
cmpl   $0xffffffff,-0x404(%rbp)
jne    1226 <main+0x86>
lea    0xe01(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c2 <main+0x222>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x410(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40c(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x408(%rbp)
movl   $0xc,-0x424(%rbp)
mov    -0x404(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12b9 <main+0x119>
lea    0xd91(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x404(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c2 <main+0x222>
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x404(%rbp),%edi
lea    -0x400(%rbp),%rsi
lea    -0x420(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x424(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0xffffffff,-0x428(%rbp)
jne    1312 <main+0x172>
lea    0xd3a(%rip),%rdi
call   10a0 <perror@plt>
jmp    13a0 <main+0x200>
mov    -0x428(%rbp),%esi
sub    $0x1c,%esi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movzbl -0x3ea(%rbp),%esi
movzbl -0x3e9(%rbp),%edx
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1c,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x428(%rbp),%eax
jge    1392 <main+0x1f2>
movslq -0x42c(%rbp),%rax
movzbl -0x400(%rbp,%rax,1),%esi
ff
lea    0xd00(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    134f <main+0x1af>
lea    0xca7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x404(%rbp),%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
mov    -0x404(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
cmp    $0x2,%edi
jne    12fb <main+0x14b>
mov    %rsi,%rbx
mov    0x2e18(%rip),%r14
mov    (%r14),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1319 <main+0x169>
mov    %eax,%r15d
lea    0xe6f(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     132c <main+0x17c>
lea    0xe22(%rip),%rdi
call   1040 <puts@plt>
lea    0x30(%rsp),%rsi
lea    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %r15d,%edi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     133f <main+0x18f>
mov    %eax,%ebx
lea    -0x1c(%rbx),%esi
lea    0xdad(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movzbl 0x46(%rsp),%esi
movzbl 0x47(%rsp),%edx
lea    0xda4(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x1d,%ebx
jl     12d8 <main+0x128>
mov    %ebx,%ebp
mov    $0x1c,%ebx
lea    0xda5(%rip),%r14
xchg   %ax,%ax
movzbl 0x30(%rsp,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12c0 <main+0x110>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
xor    %ebx,%ebx
mov    %r15d,%edi
call   1080 <close@plt>
jmp    1309 <main+0x159>
lea    0xcfe(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
jmp    1309 <main+0x159>
lea    0xcf0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
jmp    12f1 <main+0x141>
lea    0xce8(%rip),%rdi
call   10b0 <perror@plt>
jmp    12e2 <main+0x132>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"usage: program <port>\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [11 x i8] c"socket ok\0A\00"
@INADDR_ANY = global i32 0
@.str.3 = [11 x i8] c"bind error\00"
@.str.4 = [9 x i8] c"bind ok\0A\00"
@.str.5 = [11 x i8] c"recv error\00"
@.str.6 = [15 x i8] c"recv %d bytes\0A\00"
@.str.7 = [27 x i8] c"destinaton port %hhu %hhu\0A\00"
@.str.8 = [9 x i8] c"0x%02X, \00"
@.str.9 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1000 x i8]
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%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 ne i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %91
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_RAW
%20 = load i32, i32* @IPPROTO_UDP
%21 = call i32 @socket(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %91
26: ; preds = %17
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
%34 = call i32 @htons(i32 %33)
store i32 %34, i32* %29
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = load i32, i32* @INADDR_ANY
store i32 %37, i32* %36
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%39 = load i32, i32* @AF_INET
store i32 %39, i32* %38
store i32 12, i32* %10
%40 = load i32, i32* %7
%41 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %28
%45 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
store i32 1, i32* %3
br label %91
48: ; preds = %28
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48
%51 = load i32, i32* %7
%52 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%53 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%54 = call i32 @recvfrom(i32 %51, i8* %52, i32 1000, i32 0, %struct.sockaddr* %53, i32* %10)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %86
59: ; preds = %50
%60 = load i32, i32* %11
%61 = sub nsw i32 %60, 28
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %61)
%63 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 22
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 23
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %65, i32 %68)
store i32 28, i32* %12
br label %70
70: ; preds = %81, %59
%71 = load i32, i32* %12
%72 = load i32, i32* %11
%73 = icmp slt i32 %71, %72
br i1 %73, label %74, label %84
74: ; preds = %70
%75 = load i32, i32* %12
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %76
%78 = load i8, i8* %77
%79 = zext i8 %78 to i32
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i32 %79)
br label %81
81: ; preds = %74
%82 = load i32, i32* %12
%83 = add nsw i32 %82, 1
store i32 %83, i32* %12
br label %70
84: ; preds = %70
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
br label %86
86: ; preds = %84, %57
%87 = load i32, i32* %7
%88 = call i32 @shutdown(i32 %87, i32 2)
%89 = load i32, i32* %7
%90 = call i32 @close(i32 %89)
store i32 0, i32* %3
br label %91
91: ; preds = %86, %44, %24, %15
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"usage: program <port>\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@INADDR_ANY = global i32 0
@.str.3 = [11 x i8] c"bind error\00"
@.str.5 = [11 x i8] c"recv error\00"
@.str.6 = [15 x i8] c"recv %d bytes\0A\00"
@.str.7 = [27 x i8] c"destinaton port %hhu %hhu\0A\00"
@.str.8 = [9 x i8] c"0x%02X, \00"
@str = [10 x i8] c"socket ok\00"
@str.10 = [8 x i8] c"bind ok\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1000 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %75
10: ; preds = %2
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_RAW
%14 = load i32, i32* @IPPROTO_UDP
%15 = tail call i32 @socket(i32 %12, i32 %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %73
19: ; preds = %10
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%21 = bitcast %struct.sockaddr_in* %4 to i8*
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = tail call i32 @atoi(i8* %24)
%26 = tail call i32 @htons(i32 %25)
store i32 %26, i32* %22
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%28 = load i32, i32* @INADDR_ANY
store i32 %28, i32* %27
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
%30 = load i32, i32* @AF_INET
store i32 %30, i32* %29
%31 = bitcast %struct.sockaddr_in* %5 to i8*
%32 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%34 = call i32 @bind(i32 %15, %struct.sockaddr* %33, i32 12)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %19
%37 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %70
38: ; preds = %19
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.10, i64 0, i64 0))
%40 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%41 = call i32 @recvfrom(i32 %15, i8* %11, i32 1000, i32 0, %struct.sockaddr* %40, i32* %6)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %68
45: ; preds = %38
%46 = add nsw i32 %41, -28
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %46)
%48 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 22
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 23
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %50, i32 %53)
%55 = icmp sgt i32 %41, 28
br i1 %55, label %56, label %58
56: ; preds = %45
%57 = zext i32 %41 to i64
br label %60
58: ; preds = %60, %45
%59 = call i32 @putchar(i32 10)
br label %68
60: ; preds = %56, %60
%61 = phi i64 [ 28, %56 ], [ %66, %60 ]
%62 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %61
%63 = load i8, i8* %62
%64 = zext i8 %63 to i32
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i32 %64)
%66 = add nuw nsw i64 %61, 1
%67 = icmp eq i64 %66, %57
br i1 %67, label %58, label %60
68: ; preds = %58, %43
%69 = call i32 @shutdown(i32 %15, i32 2)
br label %70
70: ; preds = %68, %36
%71 = phi i32 [ 1, %36 ], [ 0, %68 ]
%72 = call i32 @close(i32 %15)
br label %73
73: ; preds = %70, %17
%74 = phi i32 [ 1, %17 ], [ %71, %70 ]
br label %75
75: ; preds = %73, %8
%76 = phi i32 [ 0, %8 ], [ %74, %73 ]
ret i32 %76
}