#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_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
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*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, 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_DGRAM, IPPROTO_UDP);
  if (sock == -1) {
    perror("socket error");
    return 1;
  } else {
    printf("socket ok\n");
  }
  int broadcast = 1;
  setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast);
  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);
    for (int i = 0; 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    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11eb <main+0x3b>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    13e7 <main+0x237>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x404(%rbp)
cmpl   $0xffffffff,-0x404(%rbp)
jne    1236 <main+0x86>
lea    0xdf1(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e7 <main+0x237>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x408(%rbp)
mov    -0x404(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
lea    -0x408(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,-0x418(%rbp)
mov    0x2d3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x410(%rbp)
movl   $0xc,-0x42c(%rbp)
mov    -0x404(%rbp),%edi
lea    -0x418(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12fd <main+0x14d>
lea    0xd4d(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x404(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e7 <main+0x237>
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x404(%rbp),%edi
lea    -0x400(%rbp),%rsi
lea    -0x428(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x42c(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0xffffffff,-0x430(%rbp)
jne    1356 <main+0x1a6>
lea    0xcf6(%rip),%rdi
call   10b0 <perror@plt>
jmp    13c5 <main+0x215>
mov    -0x430(%rbp),%esi
lea    0xcea(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x434(%rbp)
mov    -0x434(%rbp),%eax
cmp    -0x430(%rbp),%eax
jge    13b7 <main+0x207>
movslq -0x434(%rbp),%rax
movzbl -0x400(%rbp,%rax,1),%esi
ff
lea    0xcc0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    1374 <main+0x1c4>
lea    0xc82(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x404(%rbp),%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
mov    -0x404(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
cmp    $0x2,%edi
jne    131b <main+0x15b>
mov    %rsi,%rbx
mov    0x2e00(%rip),%r14
mov    (%r14),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     1339 <main+0x179>
mov    %eax,%r15d
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,0xc(%rsp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r15d,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    0x8(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
movl   $0xc,0x8(%rsp)
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     134c <main+0x18c>
lea    0xdca(%rip),%rdi
call   1040 <puts@plt>
lea    0x30(%rsp),%rsi
lea    0x20(%rsp),%r8
lea    0x8(%rsp),%r9
mov    %r15d,%edi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   10a0 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     135f <main+0x19f>
mov    %eax,%ebx
lea    0xd73(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
test   %ebx,%ebx
jle    12f8 <main+0x138>
mov    %ebx,%ebp
lea    0xd6c(%rip),%r14
xor    %ebx,%ebx
xchg   %ax,%ax
movzbl 0x30(%rsp,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12e0 <main+0x120>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%edi
mov    $0x2,%esi
call   1060 <shutdown@plt>
xor    %ebx,%ebx
mov    %r15d,%edi
call   1090 <close@plt>
jmp    1329 <main+0x169>
lea    0xcde(%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    0xcd6(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebx
jmp    1329 <main+0x169>
lea    0xcd0(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebx
jmp    1311 <main+0x151>
lea    0xcc8(%rip),%rdi
call   10c0 <perror@plt>
jmp    1302 <main+0x142>

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

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