#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
scalar_t__ EINTR;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
void *start_server(void *arg) {
  int server_fd = (int)(unsigned long)arg;
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);
  char *buf;
  int fd;
  int r;
  buf = malloc(8192);
  for (;;) {
    fd = accept(server_fd, (struct sockaddr *)&addr, &addrlen);
    if (fd == -1) {
      perror("accept");
      break;
    }
    do {
      r = send(fd, buf, 8192, 0);
    } while (r < 0 && errno == EINTR);
    if (r < 0)
      perror("send");
    if (r != 8192)
      fprintf(stderr, "can only send %d bytes\n", r);
    sleep(1);
    close(fd);
  }
  free(buf);
  close(server_fd);
  pthread_exit(0);
}


x86 assembly (clang, -O0):
<start_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    $0x2000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e6 <start_server+0x56>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
jmp    1287 <start_server+0xf7>
jmp    11eb <start_server+0x5b>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x30(%rbp)
mov    %al,-0x31(%rbp)
jge    122a <start_server+0x9a>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11eb <start_server+0x5b>
cmpl   $0x0,-0x30(%rbp)
jge    124b <start_server+0xbb>
lea    0xdc1(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x2000,-0x30(%rbp)
je     1270 <start_server+0xe0>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%edx
lea    0xda1(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    11b8 <start_server+0x28>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1070 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movl   $0x4,0xc(%rsp)
mov    $0x2000,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
jne    11ff <start_server+0x6f>
lea    0xe2c(%rip),%rdi
call   1090 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1070 <pthread_exit@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    0x2dd0(%rip),%r13
mov    0x2dc1(%rip),%r15
jmp    1270 <start_server+0xe0>
lea    0xdef(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    %r12d,%edx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%edi
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11cd <start_server+0x3d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1291 <start_server+0x101>
mov    (%r15),%rax
cmp    %rax,0x0(%r13)
je     1270 <start_server+0xe0>
test   %r12d,%r12d
js     1211 <start_server+0x81>
cmp    $0x2000,%r12d
jne    121d <start_server+0x8d>
jmp    1235 <start_server+0xa5>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [5 x i8] c"send\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"can only send %d bytes\0A\00"
define i8* @start_server(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = ptrtoint i8* %10 to i64
%12 = trunc i64 %11 to i32
store i32 %12, i32* %4
store i32 4, i32* %6
%13 = call i8* @malloc(i32 8192)
store i8* %13, i8** %7
br label %14
14: ; preds = %48, %1
%15 = load i32, i32* %4
%16 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%17 = call i32 @accept(i32 %15, %struct.sockaddr* %16, i32* %6)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %52
22: ; preds = %14
br label %23
23: ; preds = %34, %22
%24 = load i32, i32* %8
%25 = load i8*, i8** %7
%26 = call i32 @send(i32 %24, i8* %25, i32 8192, i32 0)
store i32 %26, i32* %9
br label %27
27: ; preds = %23
%28 = load i32, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br label %34
34: ; preds = %30, %27
%35 = phi i1 [ false, %27 ], [ %33, %30 ]
br i1 %35, label %23, label %36
36: ; preds = %34
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = load i32, i32* %9
%43 = icmp ne i32 %42, 8192
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = load i32, i32* %9
%47 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %44, %41
%49 = call i32 @sleep(i32 1)
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
br label %14
52: ; preds = %20
%53 = load i8*, i8** %7
%54 = call i32 @free(i8* %53)
%55 = load i32, i32* %4
%56 = call i32 @close(i32 %55)
%57 = call i32 @pthread_exit(i32 0)
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [5 x i8] c"send\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"can only send %d bytes\0A\00"
define noalias i8* @start_server(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = ptrtoint i8* %0 to i64
%5 = trunc i64 %4 to i32
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%8 = tail call dereferenceable_or_null(8192) i8* @malloc(i32 8192)
%9 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%10 = call i32 @accept(i32 %5, %struct.sockaddr* %9, i32* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %14, label %12
12: ; preds = %1, %34
%13 = phi i32 [ %37, %34 ], [ %10, %1 ]
br label %19
14: ; preds = %34, %1
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @free(i8* %8)
%17 = call i32 @close(i32 %5)
%18 = call i32 @pthread_exit(i32 0)
ret i8* undef
19: ; preds = %12, %19
%20 = call i32 @send(i32 %13, i8* %8, i32 8192, i32 0)
%21 = icmp slt i32 %20, 0
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
%25 = select i1 %21, i1 %24, i1 false
br i1 %25, label %19, label %26
26: ; preds = %19
br i1 %21, label %27, label %29
27: ; preds = %26
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %31
29: ; preds = %26
%30 = icmp eq i32 %20, 8192
br i1 %30, label %34, label %31
31: ; preds = %27, %29
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %34
34: ; preds = %31, %29
%35 = call i32 @sleep(i32 1)
%36 = call i32 @close(i32 %13)
%37 = call i32 @accept(i32 %5, %struct.sockaddr* %9, i32* %3)
%38 = icmp eq i32 %37, -1
br i1 %38, label %14, label %12
}