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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ cmd_parser(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void watch_socket() {
  int sockfd, new_fd;
  int numbytes;
  char buf[500];
  int yes;
  struct sockaddr_in my_addr;
  struct sockaddr_in their_addr;
  int sin_size;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    perror("Server-setsockopt() error lol!");
    exit(1);
  }
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(3678);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  bzero(&(my_addr.sin_zero), sizeof(my_addr.sin_zero));
  if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("bind");
    exit(1);
  }
  if (listen(sockfd, 10) == -1) {
    perror("listen");
    exit(1);
  }
  while (1) {
    sin_size = sizeof(struct sockaddr_in);
    if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) ==
        -1) {
      perror("accept");
      continue;
    }
    memset(buf, 0, sizeof(buf));
    if ((numbytes = recv(new_fd, buf, 500, 0)) == -1) {
      perror("recv");
      continue;
    }
    cmd_parser(buf);
    close(new_fd);
  }
}


x86 assembly (clang, -O0):
<watch_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    1216 <watch_socket+0x46>
lea    0xdf9(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
lea    -0x204(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    125c <watch_socket+0x8c>
lea    0xdba(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20c(%rbp)
mov    $0xe5e,%edi
call   1050 <htons@plt>
mov    %eax,-0x210(%rbp)
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x214(%rbp)
lea    -0x218(%rbp),%rdi
mov    $0x4,%esi
call   10c0 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x218(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12ce <watch_socket+0xfe>
lea    0xd67(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12fa <watch_socket+0x12a>
lea    0xd40(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12ff <watch_socket+0x12f>
movl   $0x10,-0x22c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x228(%rbp),%rsi
lea    -0x22c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    133c <watch_socket+0x16c>
lea    0xd00(%rip),%rdi
call   10b0 <perror@plt>
jmp    12ff <watch_socket+0x12f>
lea    -0x200(%rbp),%rdi
xor    %esi,%esi
mov    $0x1f4,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x200(%rbp),%rsi
mov    $0x1f4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1382 <watch_socket+0x1b2>
lea    0xcc1(%rip),%rdi
call   10b0 <perror@plt>
jmp    12ff <watch_socket+0x12f>
lea    -0x200(%rbp),%rdi
call   1090 <cmd_parser@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    12ff <watch_socket+0x12f>

x86 assembly (clang, -O2):
<watch_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    0x2df8(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1322 <watch_socket+0x152>
mov    %eax,%ebp
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    0x1c(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     132b <watch_socket+0x15b>
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    $0xe5e,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
call   10c0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     1334 <watch_socket+0x164>
mov    %ebp,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     133d <watch_socket+0x16d>
lea    0x20(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xd8f(%rip),%r12
lea    0x30(%rsp),%rbx
jmp    12b8 <watch_socket+0xe8>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   10b0 <perror@plt>
movl   $0x10,0x4(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     12b0 <watch_socket+0xe0>
mov    %eax,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1f4,%edx
call   1060 <memset@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x1f4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    1310 <watch_socket+0x140>
lea    0xd37(%rip),%rdi
call   10b0 <perror@plt>
jmp    12b8 <watch_socket+0xe8>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <cmd_parser@plt>
mov    %r13d,%edi
call   1070 <close@plt>
jmp    12b8 <watch_socket+0xe8>
lea    0xcd7(%rip),%rdi
jmp    1344 <watch_socket+0x174>
lea    0xcd5(%rip),%rdi
jmp    1344 <watch_socket+0x174>
lea    0xceb(%rip),%rdi
jmp    1344 <watch_socket+0x174>
lea    0xce7(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [31 x i8] c"Server-setsockopt() error lol!\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [5 x i8] c"recv\00"
define void @watch_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [500 x i8]
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %1
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %1
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %5, i32 4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 3
store i32 %26, i32* %27
%28 = call i32 @htons(i32 3678)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%34 = call i32 @bzero(i32* %33, i32 4)
%35 = load i32, i32* %1
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i32 @bind(i32 %35, %struct.sockaddr* %36, i32 4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %25
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %25
%43 = load i32, i32* %1
%44 = call i32 @listen(i32 %43, i32 10)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %42
br label %50
50: ; preds = %49, %55, %64, %66
store i32 16, i32* %8
%51 = load i32, i32* %1
%52 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%53 = call i32 @accept(i32 %51, %struct.sockaddr* %52, i32* %8)
store i32 %53, i32* %2
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %50
57: ; preds = %50
%58 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%59 = call i32 @memset(i8* %58, i32 0, i32 500)
%60 = load i32, i32* %2
%61 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%62 = call i32 @recv(i32 %60, i8* %61, i32 500, i32 0)
store i32 %62, i32* %3
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %57
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %50
66: ; preds = %57
%67 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%68 = call i32 @cmd_parser(i8* %67)
%69 = load i32, i32* %2
%70 = call i32 @close(i32 %69)
br label %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [31 x i8] c"Server-setsockopt() error lol!\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [5 x i8] c"recv\00"
define void @watch_socket() {
%1 = alloca [500 x i8]
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%7 = bitcast i32* %2 to i8*
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %13, i32 %19, i32 %20, i32* %2, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %27, i32* %28
%29 = call i32 @htons(i32 3678)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%34 = call i32 @bzero(i32* %33, i32 4)
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i32 @bind(i32 %13, %struct.sockaddr* %35, i32 4)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %26
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %26
%42 = call i32 @listen(i32 %13, i32 10)
%43 = icmp eq i32 %42, -1
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %49
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %54, %44
store i32 16, i32* %5
%50 = call i32 @accept(i32 %13, %struct.sockaddr* %45, i32* %5)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %59, %61
br label %49
55: ; preds = %49
%56 = call i32 @memset(i8* %6, i32 0, i32 500)
%57 = call i32 @recv(i32 %50, i8* %6, i32 500, i32 0)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %54
61: ; preds = %55
%62 = call i32 @cmd_parser(i8* %6)
%63 = call i32 @close(i32 %50)
br label %54
}