#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_TCP;
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(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() {
  unsigned char buf[1000];
  int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  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(1100)};
  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    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x3f4(%rbp)
cmpl   $0xffffffff,-0x3f4(%rbp)
jne    11dd <main+0x5d>
lea    0xe34(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1350 <main+0x1d0>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x44c,%edi
call   1040 <htons@plt>
mov    %eax,-0x400(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3fc(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3f8(%rbp)
movl   $0xc,-0x414(%rbp)
mov    -0x3f4(%rbp),%edi
lea    -0x400(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1266 <main+0xe6>
lea    0xdce(%rip),%rdi
call   1090 <perror@plt>
mov    -0x3f4(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1350 <main+0x1d0>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x3f4(%rbp),%edi
lea    -0x3f0(%rbp),%rsi
lea    -0x410(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x414(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0xffffffff,-0x418(%rbp)
jne    12bf <main+0x13f>
lea    0xd77(%rip),%rdi
call   1090 <perror@plt>
jmp    132e <main+0x1ae>
mov    -0x418(%rbp),%esi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
cmp    -0x418(%rbp),%eax
jge    1320 <main+0x1a0>
movslq -0x41c(%rbp),%rax
movzbl -0x3f0(%rbp,%rax,1),%esi
ff
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    12dd <main+0x15d>
lea    0xd03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x3f4(%rbp),%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
mov    -0x3f4(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    0x2e34(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c9 <main+0x129>
mov    %eax,%r14d
lea    0xe5a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x44c,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12dc <main+0x13c>
lea    0xe13(%rip),%rdi
call   1040 <puts@plt>
lea    0x30(%rsp),%rsi
lea    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %r14d,%edi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     12ef <main+0x14f>
mov    %eax,%ebp
lea    0xdbc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebp,%ebp
jle    1298 <main+0xf8>
mov    %ebp,%ebx
lea    0xdb5(%rip),%r15
xor    %ebp,%ebp
nop
movzbl 0x30(%rsp,%rbp,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1280 <main+0xe0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14d,%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
xor    %ebp,%ebp
mov    %r14d,%edi
call   1080 <close@plt>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd30(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebp
jmp    12b9 <main+0x119>
lea    0xd2a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebp
jmp    12b1 <main+0x111>
lea    0xd22(%rip),%rdi
call   10b0 <perror@plt>
jmp    12a2 <main+0x102>

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

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