#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 socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ message;
typedef int /*<<< orphan*/ clnt_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ error_handling(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int, int);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int serv_sock;
  int clnt_sock;
  struct sockaddr_in serv_addr;
  struct sockaddr_in clnt_addr;
  socklen_t clnt_addr_size;
  char message[] = "Hello Master HaKu!";
  if (argc != 2) {
    printf("Usage: %s <port>\n", argv[0]);
    exit(1);
  }
  serv_sock = socket(AF_INET, SOCK_STREAM, 0);
  if (serv_sock == -1)
    error_handling("sock() error");
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(atoi(argv[1]));
  if (bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
    error_handling("bind() error");
  if (listen(serv_sock, 5) == -1)
    error_handling("listen() error");
  clnt_addr_size = sizeof(clnt_addr);
  do {
    clnt_sock =
        accept(serv_sock, (struct sockaddr *)&clnt_addr, &clnt_addr_size);
    if (clnt_sock == -1)
      error_handling("accept() error");
    write(clnt_sock, message, sizeof(message));
    int z;
    int error = 0;
    socklen_t len = 4;
    sleep(1);
    z = getsockopt(clnt_sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len);
    fprintf(stderr, "getsock opt return %d, error = %d, errno = %d, len = %d\n",
            z, error, errno, len);
  } while (1);
  close(clnt_sock);
  close(serv_sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xde3(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xde0(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xddd(%rip),%ax
mov    %ax,-0x40(%rbp)
mov    0xdd5(%rip),%al
mov    %al,-0x3e(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1267 <main+0x67>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdbb(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1299 <main+0x99>
lea    0xd91(%rip),%rdi
call   10b0 <error_handling@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1303 <main+0x103>
lea    0xd34(%rip),%rdi
call   10b0 <error_handling@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1325 <main+0x125>
lea    0xd1f(%rip),%rdi
call   10b0 <error_handling@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1355 <main+0x155>
lea    0xcfe(%rip),%rdi
call   10b0 <error_handling@plt>
mov    -0x18(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x13,%edx
call   1030 <write@plt>
movl   $0x0,-0x58(%rbp)
movl   $0x4,-0x5c(%rbp)
mov    $0x1,%edi
call   1110 <sleep@plt>
mov    -0x18(%rbp),%edi
mov    0x2c60(%rip),%rax
mov    (%rax),%esi
mov    0x2c1f(%rip),%rax
mov    (%rax),%edx
lea    -0x58(%rbp),%rcx
lea    -0x5c(%rbp),%r8
call   1090 <getsockopt@plt>
mov    %eax,-0x54(%rbp)
mov    0x2c1e(%rip),%rax
mov    (%rax),%edi
mov    -0x54(%rbp),%edx
mov    -0x58(%rbp),%ecx
mov    0x2bff(%rip),%rax
mov    (%rax),%r8d
mov    -0x5c(%rbp),%r9d
lea    0xc96(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%al
test   $0x1,%al
jne    132c <main+0x12c>
jmp    13db <main+0x1db>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
movaps 0xdf8(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movl   $0x21754b,0x2f(%rsp)
cmp    $0x2,%edi
jne    138d <main+0x19d>
mov    0x2dbb(%rip),%r15
mov    (%r15),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    124b <main+0x5b>
lea    0xddf(%rip),%rdi
call   10a0 <error_handling@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%r14),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12ac <main+0xbc>
lea    0xd8b(%rip),%rdi
call   10a0 <error_handling@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c9 <main+0xd9>
lea    0xd7b(%rip),%rdi
call   10a0 <error_handling@plt>
movl   $0xc,0xc(%rsp)
mov    0x2cf0(%rip),%r13
mov    0x2cd9(%rip),%r12
lea    0xd77(%rip),%r14
jmp    135d <main+0x16d>
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
lea    0x20(%rsp),%rsi
mov    $0x13,%edx
call   1030 <write@plt>
movl   $0x0,0x8(%rsp)
movl   $0x4,0x4(%rsp)
mov    $0x1,%edi
call   1100 <sleep@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
mov    0x2c84(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
lea    0x8(%rsp),%rcx
lea    0x4(%rsp),%r8
call   1080 <getsockopt@plt>
mov    0x0(%r13),%edi
mov    0x8(%rsp),%ecx
mov    (%r12),%r8d
mov    0x4(%rsp),%r9d
mov    %r14,%rsi
mov    %eax,%edx
call   1090 <fprintf@plt>
mov    %ebp,%ebx
mov    %ebx,%ebp
mov    %ebx,%edi
lea    0x38(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10d0 <accept@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    12f0 <main+0x100>
lea    0xccb(%rip),%rdi
call   10a0 <error_handling@plt>
jmp    12f0 <main+0x100>
mov    (%r14),%rsi
lea    0xc7c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.message = [19 x i8] c"Hello Master HaKu!\00"
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"sock() error\00"
@INADDR_ANY = global i32 0
@.str.2 = [13 x i8] c"bind() error\00"
@.str.3 = [15 x i8] c"listen() error\00"
@.str.4 = [15 x i8] c"accept() error\00"
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@stderr = global i32 0
@.str.5 = [57 x i8] c"getsock opt return %d, error = %d, errno = %d, len = %d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca [19 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = bitcast [19 x i8]* %11 to i8*
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @error_handling(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @atoi(i8* %42)
%44 = call i32 @htons(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%48 = call i32 @bind(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %32
%51 = call i32 @error_handling(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %32
%53 = load i32, i32* %6
%54 = call i32 @listen(i32 %53, i32 5)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @error_handling(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
store i32 12, i32* %10
br label %59
59: ; preds = %83, %58
%60 = load i32, i32* %6
%61 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%62 = call i32 @accept(i32 %60, %struct.sockaddr* %61, i32* %10)
store i32 %62, i32* %7
%63 = load i32, i32* %7
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 @error_handling(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %67
67: ; preds = %65, %59
%68 = load i32, i32* %7
%69 = getelementptr inbounds [19 x i8], [19 x i8]* %11, i64 0, i64 0
%70 = call i32 @write(i32 %68, i8* %69, i32 19)
store i32 0, i32* %13
store i32 4, i32* %14
%71 = call i32 @sleep(i32 1)
%72 = load i32, i32* %7
%73 = load i32, i32* @SOL_SOCKET
%74 = load i32, i32* @SO_ERROR
%75 = bitcast i32* %13 to i8*
%76 = call i32 @getsockopt(i32 %72, i32 %73, i32 %74, i8* %75, i32* %14)
store i32 %76, i32* %12
%77 = load i32, i32* @stderr
%78 = load i32, i32* %12
%79 = load i32, i32* %13
%80 = load i32, i32* @errno
%81 = load i32, i32* %14
%82 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i32 %78, i32 %79, i32 %80, i32 %81)
br label %83
83: ; preds = %67
br i1 true, label %59, label %84
84: ; preds = %83
%85 = load i32, i32* %7
%86 = call i32 @close(i32 %85)
%87 = load i32, i32* %6
%88 = call i32 @close(i32 %87)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.message = [19 x i8] c"Hello Master HaKu!\00"
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"sock() error\00"
@INADDR_ANY = global i32 0
@.str.2 = [13 x i8] c"bind() error\00"
@.str.3 = [15 x i8] c"listen() error\00"
@.str.4 = [15 x i8] c"accept() error\00"
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@stderr = global i32 0
@.str.5 = [57 x i8] c"getsock opt return %d, error = %d, errno = %d, len = %d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [19 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = bitcast %struct.sockaddr_in* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = getelementptr inbounds [19 x i8], [19 x i8]* %6, i64 0, i64 0
%13 = icmp eq i32 %0, 2
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i8*, i8** %1
%16 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @error_handling(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = call i32 @htonl(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %30, i32* %31
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %35, i32* %36
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i32 @bind(i32 %21, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %25
%41 = call i32 @error_handling(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %25
%43 = call i32 @listen(i32 %21, i32 5)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @error_handling(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
store i32 12, i32* %5
%48 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%49 = bitcast i32* %7 to i8*
%50 = bitcast i32* %8 to i8*
br label %51
51: ; preds = %56, %47
%52 = call i32 @accept(i32 %21, %struct.sockaddr* %48, i32* %5)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @error_handling(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
%57 = call i32 @write(i32 %52, i8* %12, i32 19)
store i32 0, i32* %7
store i32 4, i32* %8
%58 = call i32 @sleep(i32 1)
%59 = load i32, i32* @SOL_SOCKET
%60 = load i32, i32* @SO_ERROR
%61 = call i32 @getsockopt(i32 %52, i32 %59, i32 %60, i8* %49, i32* %8)
%62 = load i32, i32* @stderr
%63 = load i32, i32* %7
%64 = load i32, i32* @errno
%65 = load i32, i32* %8
%66 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i32 %61, i32 %63, i32 %64, i32 %65)
br label %51
}