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

/* Variables and functions */
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ close(int);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ log_msg(char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
scalar_t__ send_to_client(int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
void spawn(int *client) {
  int sock_fd;
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);
  signal(SIGPIPE, SIG_IGN);
  sock_fd = (int)*client;
  if (getsockname(sock_fd, (struct sockaddr *)&addr, &addrlen)) {
    close(sock_fd);
    pthread_exit(NULL);
  }
  log_msg("Connection from %s on port %hd", inet_ntoa(addr.sin_addr),
          ntohs(addr.sin_port));
  while (1) {
    if (send_to_client(sock_fd)) {
      log_msg("Closing connection to %s", inet_ntoa(addr.sin_addr));
      close(sock_fd);
      pthread_exit(NULL);
    }
    usleep(250000);
  }
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x8,-0x1c(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10a0 <getsockname@plt>
cmp    $0x0,%rax
je     11ee <spawn+0x5e>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <pthread_exit@plt>
mov    -0x18(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1080 <log_msg@plt>
mov    -0xc(%rbp),%edi
call   1040 <send_to_client@plt>
cmp    $0x0,%rax
je     124f <spawn+0xbf>
mov    -0x18(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1080 <log_msg@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <pthread_exit@plt>
mov    $0x3d090,%edi
call   10b0 <usleep@plt>
jmp    1214 <spawn+0x84>

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movl   $0x8,0xc(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    (%rbx),%ebx
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <getsockname@plt>
test   %rax,%rax
je     11de <spawn+0x4e>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1070 <pthread_exit@plt>
mov    (%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%ebp
mov    0x4(%rsp),%edi
call   1090 <ntohs@plt>
lea    0xe08(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <log_msg@plt>
lea    0xe15(%rip),%r14
jmp    121a <spawn+0x8a>
nopl   0x0(%rax)
mov    $0x3d090,%edi
call   10b0 <usleep@plt>
mov    %ebx,%edi
call   1040 <send_to_client@plt>
test   %rax,%rax
je     1210 <spawn+0x80>
mov    (%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <log_msg@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1070 <pthread_exit@plt>
jmp    1210 <spawn+0x80>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [31 x i8] c"Connection from %s on port %hd\00"
@.str.1 = [25 x i8] c"Closing connection to %s\00"
define void @spawn(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32* %0, i32** %2
store i32 8, i32* %5
%6 = load i32, i32* @SIGPIPE
%7 = load i32, i32* @SIG_IGN
%8 = call i32 @signal(i32 %6, i32 %7)
%9 = load i32*, i32** %2
%10 = load i32, i32* %9
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%13 = call i64 @getsockname(i32 %11, %struct.sockaddr* %12, i32* %5)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* %3
%17 = call i32 @close(i32 %16)
%18 = call i32 @pthread_exit(i32* null)
br label %19
19: ; preds = %15, %1
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @inet_ntoa(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 @ntohs(i32 %24)
%26 = call i32 (i8*, i32, ...) @log_msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %25)
br label %27
27: ; preds = %19, %39
%28 = load i32, i32* %3
%29 = call i64 @send_to_client(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @inet_ntoa(i32 %33)
%35 = call i32 (i8*, i32, ...) @log_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
%38 = call i32 @pthread_exit(i32* null)
br label %39
39: ; preds = %31, %27
%40 = call i32 @usleep(i32 250000)
br label %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [31 x i8] c"Connection from %s on port %hd\00"
@.str.1 = [25 x i8] c"Closing connection to %s\00"
define void @spawn(i32* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 8, i32* %3
%6 = load i32, i32* @SIGPIPE
%7 = load i32, i32* @SIG_IGN
%8 = tail call i32 @signal(i32 %6, i32 %7)
%9 = load i32, i32* %0
%10 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%11 = call i64 @getsockname(i32 %9, %struct.sockaddr* %10, i32* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = call i32 @close(i32 %9)
%15 = call i32 @pthread_exit(i32* null)
br label %16
16: ; preds = %13, %1
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @inet_ntoa(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 @ntohs(i32 %21)
%23 = call i32 (i8*, i32, ...) @log_msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %22)
br label %24
24: ; preds = %33, %16
%25 = call i64 @send_to_client(i32 %9)
%26 = icmp eq i64 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = load i32, i32* %17
%29 = call i32 @inet_ntoa(i32 %28)
%30 = call i32 (i8*, i32, ...) @log_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 @close(i32 %9)
%32 = call i32 @pthread_exit(i32* null)
br label %33
33: ; preds = %27, %24
%34 = call i32 @usleep(i32 250000)
br label %24
}