#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
int /*<<< orphan*/ diep(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
void init_sock(struct sockaddr_in *addr, int *sock, char *address_s) {
  char *port_s = strchr(address_s, ':');
  int port;
  if (port_s == NULL) {
    fprintf(stderr, "Invalid address: %s\n", address_s);
    exit(1);
  }
  *port_s = '\0';
  port = atoi(port_s + 1);
  addr->sin_family = AF_INET;
  addr->sin_port = htons(port);
  addr->sin_addr.s_addr =
      (strcmp(address_s, "0.0.0.0")) ? inet_addr(address_s) : htonl(INADDR_ANY);
  *sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (*sock < 0)
    diep("socket");
}


x86 assembly (clang, -O0):
<init_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f4 <init_sock+0x54>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe1b(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1257 <init_sock+0xb7>
mov    -0x18(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
jmp    1268 <init_sock+0xc8>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    12ab <init_sock+0x10b>
lea    0xd77(%rip),%rdi
call   1060 <diep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_sock>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     125d <init_sock+0xbd>
movb   $0x0,(%rax)
add    $0x1,%rax
mov    %rax,%rdi
call   10a0 <atoi@plt>
mov    0x2e0b(%rip),%r12
mov    (%r12),%ecx
mov    %ecx,0x8(%r15)
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%r15)
lea    0xe1e(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     120e <init_sock+0x6e>
mov    %rbx,%rdi
call   1070 <inet_addr@plt>
jmp    121c <init_sock+0x7c>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,(%r15)
mov    (%r12),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     1246 <init_sock+0xa6>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdd0(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <diep@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"Invalid address: %s\0A\00"
@AF_INET = global i32 0
@.str.1 = [8 x i8] c"0.0.0.0\00"
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
define void @init_sock(%struct.sockaddr_in* %0, i32* %1, i8* %2) {
%4 = alloca %struct.sockaddr_in*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
%10 = call i8* @strchr(i8* %9, i8 signext 58)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %6
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i8*, i8** %7
store i8 0, i8* %19
%20 = load i8*, i8** %7
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %8
%23 = load i32, i32* @AF_INET
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 2
store i32 %23, i32* %25
%26 = load i32, i32* %8
%27 = call i32 @htons(i32 %26)
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 1
store i32 %27, i32* %29
%30 = load i8*, i8** %6
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %18
%34 = load i8*, i8** %6
%35 = call i32 @inet_addr(i8* %34)
br label %39
36: ; preds = %18
%37 = load i32, i32* @INADDR_ANY
%38 = call i32 @htonl(i32 %37)
br label %39
39: ; preds = %36, %33
%40 = phi i32 [ %35, %33 ], [ %38, %36 ]
%41 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %41, i32 0, i32 0
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
store i32 %40, i32* %43
%44 = load i32, i32* @AF_INET
%45 = load i32, i32* @SOCK_DGRAM
%46 = call i32 @socket(i32 %44, i32 %45, i32 0)
%47 = load i32*, i32** %5
store i32 %46, i32* %47
%48 = load i32*, i32** %5
%49 = load i32, i32* %48
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %39
%52 = call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %51, %39
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"Invalid address: %s\0A\00"
@AF_INET = global i32 0
@.str.1 = [8 x i8] c"0.0.0.0\00"
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
define void @init_sock(%struct.sockaddr_in* nocapture writeonly %0, i32* nocapture writeonly %1, i8* %2) {
%4 = tail call i8* @strchr(i8* dereferenceable(1) %2, i8 signext 58)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %2)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
store i8 0, i8* %4
%11 = getelementptr inbounds i8, i8* %4, i64 1
%12 = tail call i32 @atoi(i8* %11)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %12)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1
store i32 %15, i32* %16
%17 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %10
%20 = tail call i32 @inet_addr(i8* %2)
br label %24
21: ; preds = %10
%22 = load i32, i32* @INADDR_ANY
%23 = tail call i32 @htonl(i32 %22)
br label %24
24: ; preds = %21, %19
%25 = phi i32 [ %20, %19 ], [ %23, %21 ]
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = tail call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %1
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = tail call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %24
ret void
}