#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;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int send(int, char *, int, int /*<<< orphan*/);
int shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int buf[100];
  int sock = socket(AF_INET, SOCK_STREAM, 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(502)};
  if (bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
    perror("bind error");
    close(sock);
    return 1;
  } else {
    printf("bind ok\n");
  }
  if (listen(sock, 10) == -1) {
    perror("listen error");
    close(sock);
    return 1;
  } else {
    printf("listen ok\n");
  }
  while (1) {
    int msgsock = accept(sock, NULL, NULL);
    if (msgsock == -1) {
      perror("accept error");
      close(sock);
      return 1;
    } else {
      printf("listen ok\n");
    }
    while (send(msgsock, "hello\n", 6, 0) != -1)
      ;
    if (shutdown(msgsock, 2) == -1) {
      perror("shutdown error");
      close(msgsock);
      close(sock);
      return 1;
    } else {
      printf("listen ok\n");
    }
    close(msgsock);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x1a4(%rbp)
cmpl   $0xffffffff,-0x1a4(%rbp)
jne    11fd <main+0x5d>
lea    0xe14(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
lea    0xe09(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1f6,%edi
call   1040 <htons@plt>
mov    %eax,-0x1b0(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1ac(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1a8(%rbp)
mov    -0x1a4(%rbp),%edi
lea    -0x1b0(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    127a <main+0xda>
lea    0xdba(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1a4(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
lea    0xda2(%rip),%rdi
call   1060 <printf@plt>
mov    -0x1a4(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c2 <main+0x122>
lea    0xd86(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1a4(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
lea    0xd70(%rip),%rdi
call   1060 <printf@plt>
jmp    12d3 <main+0x133>
mov    -0x1a4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x1b4(%rbp)
cmpl   $0xffffffff,-0x1b4(%rbp)
jne    131b <main+0x17b>
lea    0xd45(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1a4(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
lea    0xd17(%rip),%rdi
call   1060 <printf@plt>
jmp    132c <main+0x18c>
mov    -0x1b4(%rbp),%edi
lea    0xd18(%rip),%rsi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
je     1353 <main+0x1b3>
jmp    132c <main+0x18c>
mov    -0x1b4(%rbp),%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
cmp    $0xffffffff,%eax
jne    139a <main+0x1fa>
lea    0xce5(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1b4(%rbp),%edi
call   1070 <close@plt>
mov    -0x1a4(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
lea    0xc98(%rip),%rdi
call   1060 <printf@plt>
mov    -0x1b4(%rbp),%edi
call   1070 <close@plt>
jmp    12d3 <main+0x133>
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e37(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x40>
lea    0xe2a(%rip),%rdi
call   10a0 <perror@plt>
jmp    12fd <main+0x15d>
mov    %eax,%r14d
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1f6,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1233 <main+0x93>
lea    0xddf(%rip),%rdi
jmp    12f0 <main+0x150>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    125d <main+0xbd>
lea    0xdc0(%rip),%rdi
jmp    12f0 <main+0x150>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
cmp    $0xffffffff,%eax
je     12e9 <main+0x149>
mov    %eax,%ebp
lea    0xdd7(%rip),%r15
lea    0xda8(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <puts@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    12a0 <main+0x100>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
cmp    $0xffffffff,%eax
je     130d <main+0x16d>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1290 <main+0xf0>
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd25(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    12f5 <main+0x155>

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_STREAM = 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 = [13 x i8] c"listen error\00"
@.str.5 = [11 x i8] c"listen ok\0A\00"
@.str.6 = [13 x i8] c"accept error\00"
@.str.7 = [7 x i8] c"hello\0A\00"
@.str.8 = [15 x i8] c"shutdown error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i32]
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %77
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%18 = call i32 @htons(i32 502)
store i32 %18, i32* %17
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
%21 = load i32, i32* @INADDR_ANY
store i32 %21, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%23 = load i32, i32* @AF_INET
store i32 %23, i32* %22
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
store i32 1, i32* %1
br label %77
32: ; preds = %16
%33 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32
%35 = load i32, i32* %3
%36 = call i32 @listen(i32 %35, i32 10)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%40 = load i32, i32* %3
%41 = call i32 @close(i32 %40)
store i32 1, i32* %1
br label %77
42: ; preds = %34
%43 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42
br label %45
45: ; preds = %44, %74
%46 = load i32, i32* %3
%47 = call i32 @accept(i32 %46, i32* null, i32* null)
store i32 %47, i32* %5
%48 = load i32, i32* %5
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%52 = load i32, i32* %3
%53 = call i32 @close(i32 %52)
store i32 1, i32* %1
br label %77
54: ; preds = %45
%55 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %54
br label %57
57: ; preds = %61, %56
%58 = load i32, i32* %5
%59 = call i32 @send(i32 %58, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 6, i32 0)
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %57
br label %57
62: ; preds = %57
%63 = load i32, i32* %5
%64 = call i32 @shutdown(i32 %63, i32 2)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %72
66: ; preds = %62
%67 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%68 = load i32, i32* %5
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %3
%71 = call i32 @close(i32 %70)
store i32 1, i32* %1
br label %77
72: ; preds = %62
%73 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %74
74: ; preds = %72
%75 = load i32, i32* %5
%76 = call i32 @close(i32 %75)
br label %45
77: ; preds = %66, %50, %38, %28, %12
%78 = load i32, i32* %1
ret i32 %78
}

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_STREAM = 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 = [13 x i8] c"listen error\00"
@.str.6 = [13 x i8] c"accept error\00"
@.str.7 = [7 x i8] c"hello\0A\00"
@.str.8 = [15 x i8] c"shutdown error\00"
@str = [10 x i8] c"socket ok\00"
@str.9 = [8 x i8] c"bind ok\00"
@str.12 = [10 x i8] c"listen ok\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = load i32, i32* @IPPROTO_TCP
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %54
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%11 = bitcast %struct.sockaddr_in* %1 to i8*
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%13 = tail call i32 @htons(i32 502)
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
%15 = load i32, i32* @INADDR_ANY
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
%17 = load i32, i32* @AF_INET
store i32 %17, i32* %16
%18 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%19 = call i32 @bind(i32 %5, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %9
%22 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %52
23: ; preds = %9
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, i64 0, i64 0))
%25 = call i32 @listen(i32 %5, i32 10)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %52
29: ; preds = %23
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.12, i64 0, i64 0))
%31 = call i32 @accept(i32 %5, i32* null, i32* null)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %47, %29
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %52
35: ; preds = %29, %47
%36 = phi i32 [ %50, %47 ], [ %31, %29 ]
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.12, i64 0, i64 0))
br label %38
38: ; preds = %38, %35
%39 = call i32 @send(i32 %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 6, i32 0)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %38
41: ; preds = %38
%42 = call i32 @shutdown(i32 %36, i32 2)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%46 = call i32 @close(i32 %36)
br label %52
47: ; preds = %41
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.12, i64 0, i64 0))
%49 = call i32 @close(i32 %36)
%50 = call i32 @accept(i32 %5, i32* null, i32* null)
%51 = icmp eq i32 %50, -1
br i1 %51, label %33, label %35
52: ; preds = %33, %44, %27, %21
%53 = call i32 @close(i32 %5)
br label %54
54: ; preds = %52, %7
ret i32 1
}