#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(1100)};
  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");
  }
  int msgsock = accept(sock, NULL, NULL);
  if (msgsock == -1) {
    perror("accept error");
    close(sock);
    return 1;
  } else {
    printf("accept ok\n");
  }
  int numwrite = send(msgsock, "hello\n", 6, 0);
  if (numwrite == -1) {
    perror("send error");
    close(msgsock);
    close(sock);
    return 1;
  } else {
    printf("send %d bytes ok\n", numwrite);
  }
  if (shutdown(msgsock, 2) == -1) {
    perror("shutdown error");
    close(msgsock);
    close(sock);
    return 1;
  } else {
    printf("shutdown 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    140a <main+0x26a>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x44c,%edi
call   1040 <htons@plt>
mov    %eax,-0x1b0(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1ac(%rbp)
mov    0x2db7(%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    127c <main+0xdc>
lea    0xdb8(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1a4(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    140a <main+0x26a>
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1a4(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c6 <main+0x126>
lea    0xd82(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1a4(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    140a <main+0x26a>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
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    131c <main+0x17c>
lea    0xd44(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1a4(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    140a <main+0x26a>
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1b4(%rbp),%edi
lea    0xd25(%rip),%rsi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x1b8(%rbp)
cmpl   $0xffffffff,-0x1b8(%rbp)
jne    1384 <main+0x1e4>
lea    0xd06(%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    140a <main+0x26a>
mov    -0x1b8(%rbp),%esi
lea    0xcdd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1b4(%rbp),%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
cmp    $0xffffffff,%eax
jne    13df <main+0x23f>
lea    0xcc8(%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    140a <main+0x26a>
lea    0xca9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1b4(%rbp),%edi
call   1070 <close@plt>
mov    -0x1a4(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e29(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12e0 <main+0x130>
mov    %eax,%ebx
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x44c,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12f4 <main+0x144>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     12fd <main+0x14d>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     1306 <main+0x156>
mov    %eax,%ebp
lea    0xe0e(%rip),%rdi
call   1030 <puts@plt>
lea    0xdb3(%rip),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
je     131a <main+0x16a>
lea    0xda7(%rip),%rdi
xor    %r14d,%r14d
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
cmp    $0xffffffff,%eax
je     1323 <main+0x173>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd19(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r14d
jmp    12d4 <main+0x124>
lea    0xd12(%rip),%rdi
jmp    130d <main+0x15d>
lea    0xd14(%rip),%rdi
jmp    130d <main+0x15d>
lea    0xd18(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r14d
jmp    12cd <main+0x11d>
lea    0xd18(%rip),%rdi
jmp    132a <main+0x17a>
lea    0xd2c(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0x1,%r14d
jmp    12cd <main+0x11d>

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

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.8 = [7 x i8] c"hello\0A\00"
@.str.9 = [11 x i8] c"send error\00"
@.str.10 = [18 x i8] c"send %d bytes ok\0A\00"
@.str.11 = [15 x i8] c"shutdown error\00"
@str = [10 x i8] c"socket ok\00"
@str.13 = [8 x i8] c"bind ok\00"
@str.14 = [10 x i8] c"listen ok\00"
@str.15 = [10 x i8] c"accept ok\00"
@str.16 = [12 x i8] c"shutdown 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 %55
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 1100)
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.13, 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.14, 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 = %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
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.15, i64 0, i64 0))
%37 = call i32 @send(i32 %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i32 6, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%41 = call i32 @close(i32 %31)
br label %52
42: ; preds = %35
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i32 %37)
%44 = call i32 @shutdown(i32 %31, i32 2)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
%48 = call i32 @close(i32 %31)
br label %52
49: ; preds = %42
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.16, i64 0, i64 0))
%51 = call i32 @close(i32 %31)
br label %52
52: ; preds = %33, %49, %46, %39, %27, %21
%53 = phi i32 [ 1, %21 ], [ 1, %27 ], [ 1, %33 ], [ 1, %39 ], [ 1, %46 ], [ 0, %49 ]
%54 = call i32 @close(i32 %5)
br label %55
55: ; preds = %52, %7
%56 = phi i32 [ 1, %7 ], [ %53, %52 ]
ret i32 %56
}