#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 /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int daemon_create_server_socket(int port, int backlog) {
  int opt;
  int sockfd;
  struct sockaddr_in servaddr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("socket() at daemon_create_server_socket(): %s", strerror(errno));
    return -1;
  }
  opt = 1;
  opt = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(int));
  if (opt < 0) {
    close(sockfd);
    printf("setsockopt() at daemon_create_server_socket(): %s",
           strerror(errno));
    return -2;
  }
  bzero((char *)&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(port);
  opt = bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (opt < 0) {
    close(sockfd);
    printf("bind() at daemon_create_server_socket(): %s", strerror(errno));
    return -3;
  }
  if (listen(sockfd, backlog)) {
    close(sockfd);
    printf("listen() at daemon_create_server_socket(): %s", strerror(errno));
    return -4;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<daemon_create_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fd <daemon_create_server_socket+0x5d>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1337 <daemon_create_server_socket+0x197>
movl   $0x1,-0x10(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1266 <daemon_create_server_socket+0xc6>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdd9(%rip),%rdi
call   1050 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1337 <daemon_create_server_socket+0x197>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12eb <daemon_create_server_socket+0x14b>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd86(%rip),%rdi
call   1050 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1337 <daemon_create_server_socket+0x197>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     1331 <daemon_create_server_socket+0x191>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd6c(%rip),%rdi
call   1050 <printf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1337 <daemon_create_server_socket+0x197>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_create_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e28(%rip),%r12
mov    (%r12),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     129e <daemon_create_server_socket+0xfe>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     12c2 <daemon_create_server_socket+0x122>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     12ed <daemon_create_server_socket+0x14d>
mov    %ebx,%edi
mov    %r14d,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     1316 <daemon_create_server_socket+0x176>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdfd(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0xfffffffc,%ebx
jmp    1316 <daemon_create_server_socket+0x176>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd4d(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1316 <daemon_create_server_socket+0x176>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd50(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0xfffffffe,%ebx
jmp    1316 <daemon_create_server_socket+0x176>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd57(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0xfffffffd,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [46 x i8] c"socket() at daemon_create_server_socket(): %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [50 x i8] c"setsockopt() at daemon_create_server_socket(): %s\00"
@INADDR_ANY = global i32 0
@.str.2 = [44 x i8] c"bind() at daemon_create_server_socket(): %s\00"
@.str.3 = [46 x i8] c"listen() at daemon_create_server_socket(): %s\00"
define i32 @daemon_create_server_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %68
18: ; preds = %2
store i32 1, i32* %6
%19 = load i32, i32* %7
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = bitcast i32* %6 to i8*
%23 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, i8* %22, i32 4)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 -2, i32* %3
br label %68
32: ; preds = %18
%33 = bitcast %struct.sockaddr_in* %8 to i8*
%34 = call i32 @bzero(i8* %33, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i32, i32* @INADDR_ANY
%38 = call i32 @htonl(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i32, i32* %4
%42 = call i32 @htons(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* %7
%45 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%46 = call i32 @bind(i32 %44, %struct.sockaddr* %45, i32 12)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %32
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %53)
store i32 -3, i32* %3
br label %68
55: ; preds = %32
%56 = load i32, i32* %7
%57 = load i32, i32* %5
%58 = call i64 @listen(i32 %56, i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %55
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %64)
store i32 -4, i32* %3
br label %68
66: ; preds = %55
%67 = load i32, i32* %7
store i32 %67, i32* %3
br label %68
68: ; preds = %66, %60, %49, %26, %14
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [46 x i8] c"socket() at daemon_create_server_socket(): %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [50 x i8] c"setsockopt() at daemon_create_server_socket(): %s\00"
@INADDR_ANY = global i32 0
@.str.2 = [44 x i8] c"bind() at daemon_create_server_socket(): %s\00"
@.str.3 = [46 x i8] c"listen() at daemon_create_server_socket(): %s\00"
define i32 @daemon_create_server_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = load i32, i32* @AF_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 %15
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %50
15: ; preds = %2
store i32 1, i32* %3
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %9, i32 %16, i32 %17, i8* %5, i32 4)
store i32 %18, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = call i32 @close(i32 %9)
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %50
25: ; preds = %15
%26 = call i32 @bzero(i8* %6, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, 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* %4, i64 0, i32 1, i32 0
store i32 %30, i32* %31
%32 = call i32 @htons(i32 %0)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i32 @bind(i32 %9, %struct.sockaddr* %34, i32 12)
store i32 %35, i32* %3
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %25
%38 = call i32 @close(i32 %9)
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %50
42: ; preds = %25
%43 = call i64 @listen(i32 %9, i32 %1)
%44 = icmp eq i64 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %42
%46 = call i32 @close(i32 %9)
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %42, %45, %37, %20, %11
%51 = phi i32 [ -1, %11 ], [ -2, %20 ], [ -3, %37 ], [ -4, %45 ], [ %9, %42 ]
ret i32 %51
}