#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*/ 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_DGRAM, 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);
    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    $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    13a3 <main+0x203>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2ded(%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    13a3 <main+0x203>
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    0x2d8b(%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    13a3 <main+0x203>
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    1381 <main+0x1e1>
mov    -0x428(%rbp),%esi
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x428(%rbp),%eax
jge    1373 <main+0x1d3>
movslq -0x42c(%rbp),%rax
movzbl -0x400(%rbp,%rax,1),%esi
ff
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    1330 <main+0x190>
lea    0xcc6(%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    12eb <main+0x13b>
mov    %rsi,%rbx
mov    0x2e18(%rip),%r14
mov    (%r14),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1309 <main+0x159>
mov    %eax,%r15d
lea    0xe54(%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    0x2dc3(%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     131c <main+0x16c>
lea    0xe07(%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     132f <main+0x17f>
mov    %eax,%ebx
lea    0xdb0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebx,%ebx
jle    12c8 <main+0x118>
mov    %ebx,%ebp
lea    0xda9(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x30(%rsp,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12b0 <main+0x100>
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    12f9 <main+0x149>
lea    0xd0e(%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    0xd06(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
jmp    12f9 <main+0x149>
lea    0xd00(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
jmp    12e1 <main+0x131>
lea    0xcf8(%rip),%rdi
call   10b0 <perror@plt>
jmp    12d2 <main+0x122>

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"
@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 %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 %83
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%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 %83
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 %83
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 %78
59: ; preds = %50
%60 = load i32, i32* %11
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %60)
store i32 0, i32* %12
br label %62
62: ; preds = %73, %59
%63 = load i32, i32* %12
%64 = load i32, i32* %11
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %76
66: ; preds = %62
%67 = load i32, i32* %12
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %68
%70 = load i8, i8* %69
%71 = zext i8 %70 to i32
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %66
%74 = load i32, i32* %12
%75 = add nsw i32 %74, 1
store i32 %75, i32* %12
br label %62
76: ; preds = %62
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
br label %78
78: ; preds = %76, %57
%79 = load i32, i32* %7
%80 = call i32 @shutdown(i32 %79, i32 2)
%81 = load i32, i32* %7
%82 = call i32 @close(i32 %81)
store i32 0, i32* %3
br label %83
83: ; preds = %78, %44, %24, %15
%84 = load i32, i32* %3
ret i32 %84
}

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"
@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 %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 %67
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_DGRAM
%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 %65
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 %62
38: ; preds = %19
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, 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 %60
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %41)
%47 = icmp sgt i32 %41, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = zext i32 %41 to i64
br label %52
50: ; preds = %52, %45
%51 = call i32 @putchar(i32 10)
br label %60
52: ; preds = %48, %52
%53 = phi i64 [ 0, %48 ], [ %58, %52 ]
%54 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %53
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %56)
%58 = add nuw nsw i64 %53, 1
%59 = icmp eq i64 %58, %49
br i1 %59, label %50, label %52
60: ; preds = %50, %43
%61 = call i32 @shutdown(i32 %15, i32 2)
br label %62
62: ; preds = %60, %36
%63 = phi i32 [ 1, %36 ], [ 0, %60 ]
%64 = call i32 @close(i32 %15)
br label %65
65: ; preds = %62, %17
%66 = phi i32 [ 1, %17 ], [ %63, %62 ]
br label %67
67: ; preds = %65, %8
%68 = phi i32 [ 0, %8 ], [ %66, %65 ]
ret i32 %68
}