#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ i;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, size_t *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ copyData(int, int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  struct sockaddr_in address;
  int sock, conn, i;
  size_t addrLength = sizeof(struct sockaddr_in);
  if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    die("socket");
  i = 1;
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
  address.sin_family = AF_INET;
  address.sin_port = htons(1234);
  memset(&address.sin_addr, 0, sizeof(address.sin_addr));
  if (bind(sock, (struct sockaddr *)&address, sizeof(address)))
    die("bind");
  if (listen(sock, 5))
    die("listen");
  while ((conn = accept(sock, (struct sockaddr *)&address, &addrLength)) >= 0) {
    printf("---- getting data\n");
    copyData(conn, 1);
    printf("---- done\n");
    close(conn);
  }
  if (conn < 0)
    die("accept");
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0xc,-0x28(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f8 <main+0x48>
lea    0xe0d(%rip),%rdi
call   10a0 <die@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1273 <main+0xc3>
lea    0xd99(%rip),%rdi
call   10a0 <die@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     1296 <main+0xe6>
lea    0xd7b(%rip),%rdi
call   10a0 <die@plt>
jmp    129b <main+0xeb>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jl     12e9 <main+0x139>
lea    0xd55(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   10c0 <copyData@plt>
lea    0xd4f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    129b <main+0xeb>
cmpl   $0x0,-0x18(%rbp)
jge    12ff <main+0x14f>
lea    0xd37(%rip),%rdi
call   10a0 <die@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0xc,0x20(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11f3 <main+0x43>
lea    0xe12(%rip),%rdi
call   10a0 <die@plt>
movl   $0x1,0xc(%rsp)
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x4d2,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
je     1270 <main+0xc0>
lea    0xd9c(%rip),%rdi
call   10a0 <die@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     128e <main+0xde>
lea    0xd83(%rip),%rdi
call   10a0 <die@plt>
lea    0x10(%rsp),%rsi
lea    0x20(%rsp),%rdx
mov    %r14d,%edi
call   10b0 <accept@plt>
test   %eax,%eax
js     12f7 <main+0x147>
mov    %eax,%ebp
lea    0xd6d(%rip),%r15
lea    0xd78(%rip),%r12
lea    0x10(%rsp),%r13
lea    0x20(%rsp),%rbx
xchg   %ax,%ax
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   10c0 <copyData@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12c0 <main+0x110>
lea    0xd15(%rip),%rdi
call   10a0 <die@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_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
@AF_INET = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [19 x i8] c"---- getting data\0A\00"
@.str.4 = [11 x i8] c"---- done\0A\00"
@.str.5 = [7 x i8] c"accept\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
store i64 12, i64* %6
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
store i32 1, i32* %5
%14 = load i32, i32* %3
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %5, i32 4)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 1234)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%23 = call i32 @memset(i32* %22, i32 0, i32 4)
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %13
%29 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %13
%31 = load i32, i32* %3
%32 = call i64 @listen(i32 %31, i32 5)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
br label %37
37: ; preds = %42, %36
%38 = load i32, i32* %3
%39 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%40 = call i32 @accept(i32 %38, %struct.sockaddr* %39, i64* %6)
store i32 %40, i32* %4
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %37
%43 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* %4
%45 = call i32 @copyData(i32 %44, i32 1)
%46 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
br label %37
49: ; preds = %37
%50 = load i32, i32* %4
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
%55 = load i32, i32* %3
%56 = call i32 @close(i32 %55)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_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
@AF_INET = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.5 = [7 x i8] c"accept\00"
@str = [18 x i8] c"---- getting data\00"
@str.6 = [10 x i8] c"---- done\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i64
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i64* %3 to i8*
store i64 12, i64* %3
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
store i32 1, i32* %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %9, i32 %14, i32 %15, i32* %2, i32 4)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 1234)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%22 = call i32 @memset(i32* %21, i32 0, i32 4)
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i64 @bind(i32 %9, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %13
%27 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %13
%29 = call i64 @listen(i32 %9, i32 5)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = call i32 @accept(i32 %9, %struct.sockaddr* %23, i64* %3)
%35 = icmp sgt i32 %34, -1
br i1 %35, label %36, label %44
36: ; preds = %33, %36
%37 = phi i32 [ %42, %36 ], [ %34, %33 ]
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%39 = call i32 @copyData(i32 %37, i32 1)
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.6, i64 0, i64 0))
%41 = call i32 @close(i32 %37)
%42 = call i32 @accept(i32 %9, %struct.sockaddr* %23, i64* %3)
%43 = icmp sgt i32 %42, -1
br i1 %43, label %36, label %44
44: ; preds = %36, %33
%45 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @close(i32 %9)
ret i32 0
}