#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*/ client_addr;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ return_stats(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
long strtol(char *, char **, int);
void service_responder(char *port_s) {
  int listen_fd;
  if (port_s == NULL) {
    fprintf(stderr, "Missing SERVICE_PORT, cannot start.\n");
    do_exit(1);
  }
  char *en = 0;
  long lport = strtol(port_s, &en, 10);
  int port = 0;
  if (*en) {
    fprintf(stderr, "Port[%s] is not numeric.\n", port_s);
    do_exit(1);
  }
  port = lport;
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_fd < 0) {
    perror("Cannot create listen socket.");
  }
  struct sockaddr_in addr;
  struct sockaddr_in client_addr;
  memset(&addr, 0, sizeof(addr));
  memset(&client_addr, 0, sizeof(client_addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(port);
  bind(listen_fd, (struct sockaddr *)&addr, sizeof(addr));
  listen(listen_fd, 10);
  for (;;) {
    socklen_t len = sizeof(client_addr);
    fprintf(stdout, "Starting accept\n");
    int client_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &len);
    fprintf(stdout, "Received connection on port %d\n",
            ntohs(client_addr.sin_port));
    return_stats(client_fd);
    close(client_fd);
  }
}


x86 assembly (clang, -O0):
<service_responder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1218 <service_responder+0x38>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1030 <do_exit@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     126f <service_responder+0x8f>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1030 <do_exit@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12a8 <service_responder+0xc8>
lea    0xd9c(%rip),%rdi
call   10e0 <perror@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    -0xc(%rbp),%edi
mov    $0xa,%esi
call   10b0 <listen@plt>
movl   $0xc,-0x44(%rbp)
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
lea    0xd37(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x48(%rbp)
mov    0x2c7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%edi
call   10c0 <ntohs@plt>
mov    -0x4c(%rbp),%edi
mov    %eax,%edx
lea    0xd0e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x48(%rbp),%edi
call   1050 <return_stats@plt>
mov    -0x48(%rbp),%edi
call   1080 <close@plt>
jmp    130e <service_responder+0x12e>

x86 assembly (clang, -O2):
<service_responder>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1217 <service_responder+0x37>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1030 <do_exit@plt>
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %rax,%r15
mov    0x10(%rsp),%rax
cmpb   $0x0,(%rax)
je     1263 <service_responder+0x83>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1030 <do_exit@plt>
mov    0x2d7e(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1355 <service_responder+0x175>
lea    0x18(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
lea    0x28(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x1c(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   10b0 <listen@plt>
mov    0x2ccd(%rip),%r15
lea    0xc(%rsp),%r12
lea    0xd6e(%rip),%r13
nop
movl   $0xc,0xc(%rsp)
mov    (%r15),%edi
lea    0xd4a(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebx,%edi
lea    0x28(%rsp),%rsi
mov    %r12,%rdx
call   10f0 <accept@plt>
mov    %eax,%ebp
mov    (%r15),%r14d
mov    0x28(%rsp),%edi
call   10c0 <ntohs@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebp,%edi
call   1050 <return_stats@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1300 <service_responder+0x120>
lea    0xce3(%rip),%rdi
call   10e0 <perror@plt>
jmp    1287 <service_responder+0xa7>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [37 x i8] c"Missing SERVICE_PORT, cannot start.\0A\00"
@.str.1 = [26 x i8] c"Port[%s] is not numeric.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [29 x i8] c"Cannot create listen socket.\00"
@INADDR_ANY = global i32 0
@stdout = global i32 0
@.str.3 = [17 x i8] c"Starting accept\0A\00"
@.str.4 = [32 x i8] c"Received connection on port %d\0A\00"
define void @service_responder(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %2
%11 = load i8*, i8** %2
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @do_exit(i32 1)
br label %17
17: ; preds = %13, %1
store i8* null, i8** %4
%18 = load i8*, i8** %2
%19 = call i64 @strtol(i8* %18, i8** %4, i32 10)
store i64 %19, i64* %5
store i32 0, i32* %6
%20 = load i8*, i8** %4
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %2
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @do_exit(i32 1)
br label %28
28: ; preds = %23, %17
%29 = load i64, i64* %5
%30 = trunc i64 %29 to i32
store i32 %30, i32* %6
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %28
%39 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%40 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %41, i32* %42
%43 = load i32, i32* @INADDR_ANY
%44 = call i32 @htonl(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%46 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i32 0, i32 0
store i32 %44, i32* %46
%47 = load i32, i32* %6
%48 = call i32 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %48, i32* %49
%50 = load i32, i32* %3
%51 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%52 = call i32 @bind(i32 %50, %struct.sockaddr* %51, i32 12)
%53 = load i32, i32* %3
%54 = call i32 @listen(i32 %53, i32 10)
br label %55
55: ; preds = %55, %38
store i32 12, i32* %9
%56 = load i32, i32* @stdout
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* %3
%59 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%60 = call i32 @accept(i32 %58, %struct.sockaddr* %59, i32* %9)
store i32 %60, i32* %10
%61 = load i32, i32* @stdout
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i32 @ntohs(i32 %63)
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %10
%67 = call i32 @return_stats(i32 %66)
%68 = load i32, i32* %10
%69 = call i32 @close(i32 %68)
br label %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [37 x i8] c"Missing SERVICE_PORT, cannot start.\0A\00"
@.str.1 = [26 x i8] c"Port[%s] is not numeric.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [29 x i8] c"Cannot create listen socket.\00"
@INADDR_ANY = global i32 0
@stdout = global i32 0
@.str.3 = [17 x i8] c"Starting accept\0A\00"
@.str.4 = [32 x i8] c"Received connection on port %d\0A\00"
define void @service_responder(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @do_exit(i32 1)
br label %11
11: ; preds = %7, %1
%12 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%13 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%14 = load i8*, i8** %2
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%20 = tail call i32 @do_exit(i32 1)
br label %21
21: ; preds = %17, %11
%22 = trunc i64 %13 to i32
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = tail call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = bitcast %struct.sockaddr_in* %3 to i8*
%31 = bitcast %struct.sockaddr_in* %4 to i8*
%32 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%33 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 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* %3, i64 0, i32 1, i32 0
store i32 %37, i32* %38
%39 = call i32 @htons(i32 %22)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i32 @bind(i32 %25, %struct.sockaddr* %41, i32 12)
%43 = call i32 @listen(i32 %25, i32 10)
%44 = bitcast i32* %5 to i8*
%45 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %47
47: ; preds = %47, %29
store i32 12, i32* %5
%48 = load i32, i32* @stdout
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @accept(i32 %25, %struct.sockaddr* %45, i32* %5)
%51 = load i32, i32* @stdout
%52 = load i32, i32* %46
%53 = call i32 @ntohs(i32 %52)
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%55 = call i32 @return_stats(i32 %50)
%56 = call i32 @close(i32 %50)
br label %47
}