#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 */
typedef int /*<<< orphan*/ uint16_t;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int create_socket(struct in_addr addr, uint16_t port, int backlog) {
  int s;
  int r;
  struct sockaddr_in sockaddr;
  const int flags = 1;
  s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (s == -1) {
    syslog(LOG_ERR, "error creating socket: %m");
    exit(EXIT_FAILURE);
  }
  r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const void *)&flags,
                 sizeof flags);
  if (r != 0) {
    syslog(LOG_ERR, "error setting re-use addr option: %m");
    exit(EXIT_FAILURE);
  }
  r = fcntl(s, F_SETFL, O_NONBLOCK);
  if (r == -1) {
    syslog(LOG_ERR, "error setting non-blocking: %m");
    exit(EXIT_FAILURE);
  }
  memset(&sockaddr, 0, sizeof sockaddr);
  sockaddr.sin_family = AF_INET;
  sockaddr.sin_port = htons(port);
  sockaddr.sin_addr = addr;
  r = bind(s, (struct sockaddr *)&sockaddr, sizeof sockaddr);
  if (r != 0) {
    syslog(LOG_ERR, "error binding socket: %m");
    exit(EXIT_FAILURE);
  }
  r = listen(s, backlog);
  if (r != 0) {
    syslog(LOG_ERR, "error listening on socket: %m");
    exit(EXIT_FAILURE);
  }
  return s;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f8 <create_socket+0x68>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     124c <create_socket+0xbc>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1296 <create_socket+0x106>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1304 <create_socket+0x174>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1080 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     133f <create_socket+0x1af>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
lea    0xd4b(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %esi,%r12d
mov    %edi,%r15d
movl   $0x1,0xc(%rsp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1288 <create_socket+0xf8>
mov    %eax,%ebp
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    129a <create_socket+0x10a>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12ac <create_socket+0x11c>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %r12d,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %r15d,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
jne    12be <create_socket+0x12e>
mov    %ebp,%edi
mov    %r14d,%esi
call   1080 <listen@plt>
test   %eax,%eax
jne    12d0 <create_socket+0x140>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
jmp    12e0 <create_socket+0x150>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
jmp    12e0 <create_socket+0x150>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
jmp    12e0 <create_socket+0x150>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
jmp    12e0 <create_socket+0x150>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
call   1070 <syslog@plt>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@LOG_ERR = global i32 0
@.str = [26 x i8] c"error creating socket: %m\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [37 x i8] c"error setting re-use addr option: %m\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [31 x i8] c"error setting non-blocking: %m\00"
@AF_INET = global i32 0
@.str.3 = [25 x i8] c"error binding socket: %m\00"
@.str.4 = [30 x i8] c"error listening on socket: %m\00"
define i32 @create_socket(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.in_addr
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i32 0, i32 0
store i32 %0, i32* %11
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 1, i32* %10
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_TCP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %3
%19 = load i32, i32* @LOG_ERR
%20 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %3
%24 = load i32, i32* %7
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = bitcast i32* %10 to i8*
%28 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i8* %27, i32 4)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %23
%32 = load i32, i32* @LOG_ERR
%33 = call i32 @syslog(i32 %32, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %23
%37 = load i32, i32* %7
%38 = load i32, i32* @F_SETFL
%39 = load i32, i32* @O_NONBLOCK
%40 = call i32 @fcntl(i32 %37, i32 %38, i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %36
%44 = load i32, i32* @LOG_ERR
%45 = call i32 @syslog(i32 %44, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %36
%49 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%50 = load i32, i32* @AF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %50, i32* %51
%52 = load i32, i32* %5
%53 = call i32 @htons(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %53, i32* %54
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%56 = bitcast %struct.in_addr* %55 to i8*
%57 = bitcast %struct.in_addr* %4 to i8*
%58 = load i32, i32* %7
%59 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%60 = call i32 @bind(i32 %58, %struct.sockaddr* %59, i32 12)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %48
%64 = load i32, i32* @LOG_ERR
%65 = call i32 @syslog(i32 %64, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %48
%69 = load i32, i32* %7
%70 = load i32, i32* %6
%71 = call i32 @listen(i32 %69, i32 %70)
store i32 %71, i32* %8
%72 = load i32, i32* %8
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %68
%75 = load i32, i32* @LOG_ERR
%76 = call i32 @syslog(i32 %75, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
%78 = call i32 @exit(i32 %77)
unreachable
79: ; preds = %68
%80 = load i32, i32* %7
ret i32 %80
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@LOG_ERR = global i32 0
@.str = [26 x i8] c"error creating socket: %m\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [37 x i8] c"error setting re-use addr option: %m\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [31 x i8] c"error setting non-blocking: %m\00"
@AF_INET = global i32 0
@.str.3 = [25 x i8] c"error binding socket: %m\00"
@.str.4 = [30 x i8] c"error listening on socket: %m\00"
define i32 @create_socket(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = load i32, i32* @IPPROTO_TCP
%11 = tail call i32 @socket(i32 %8, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i32, i32* @LOG_ERR
%15 = tail call i32 @syslog(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %3
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %11, i32 %19, i32 %20, i8* %7, i32 4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load i32, i32* @LOG_ERR
%25 = call i32 @syslog(i32 %24, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* @O_NONBLOCK
%31 = call i32 @fcntl(i32 %11, i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i32, i32* @LOG_ERR
%35 = call i32 @syslog(i32 %34, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %28
%39 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %40, i32* %41
%42 = call i32 @htons(i32 %1)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %42, i32* %43
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %0, i32* %44
%45 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%46 = call i32 @bind(i32 %11, %struct.sockaddr* %45, i32 12)
%47 = icmp eq i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %38
%49 = load i32, i32* @LOG_ERR
%50 = call i32 @syslog(i32 %49, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %38
%54 = call i32 @listen(i32 %11, i32 %2)
%55 = icmp eq i32 %54, 0
br i1 %55, label %61, label %56
56: ; preds = %53
%57 = load i32, i32* @LOG_ERR
%58 = call i32 @syslog(i32 %57, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %53
ret i32 %11
}