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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_socket_create(char *ip, int port, int is_server) {
  int sock = -1;
  int ret = -1;
  struct sockaddr_in addr;
  if (is_server && (!ip || port < 1024)) {
    printf("Fail to create udp server: Invalid ip or port\n");
    return -1;
  }
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    perror("socket failed");
    return -1;
  }
  if (is_server) {
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(ip);
    addr.sin_port = htons(port);
    ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
    if (ret < 0) {
      perror("Fail to bind");
      return -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<udp_socket_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11ca <udp_socket_create+0x5a>
cmpq   $0x0,-0x10(%rbp)
je     11b2 <udp_socket_create+0x42>
cmpl   $0x400,-0x14(%rbp)
jge    11ca <udp_socket_create+0x5a>
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <udp_socket_create+0x116>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1208 <udp_socket_create+0x98>
lea    0xe38(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <udp_socket_create+0x116>
cmpl   $0x0,-0x18(%rbp)
je     1280 <udp_socket_create+0x110>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    127b <udp_socket_create+0x10b>
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <udp_socket_create+0x116>
jmp    1280 <udp_socket_create+0x110>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_socket_create>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
test   %edx,%edx
je     1196 <udp_socket_create+0x26>
test   %r15,%r15
je     1208 <udp_socket_create+0x98>
cmp    $0x3ff,%r14d
jle    1208 <udp_socket_create+0x98>
mov    0x2e4b(%rip),%r12
mov    (%r12),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1228 <udp_socket_create+0xb8>
mov    %eax,%ebx
test   %ebp,%ebp
je     1219 <udp_socket_create+0xa9>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jns    1219 <udp_socket_create+0xa9>
lea    0xe08(%rip),%rdi
jmp    122f <udp_socket_create+0xbf>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd1(%rip),%rdi
call   1080 <perror@plt>
jmp    1214 <udp_socket_create+0xa4>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [47 x i8] c"Fail to create udp server: Invalid ip or port\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [14 x i8] c"socket failed\00"
@.str.2 = [13 x i8] c"Fail to bind\00"
define i32 @udp_socket_create(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 -1, i32* %8
store i32 -1, i32* %9
%11 = load i32, i32* %7
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 1024
br i1 %18, label %19, label %21
19: ; preds = %16, %13
%20 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
21: ; preds = %16, %3
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_DGRAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
29: ; preds = %21
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %51
32: ; preds = %29
%33 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i8*, i8** %5
%37 = call i32 @inet_addr(i8* %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %6
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %41, i32* %42
%43 = load i32, i32* %8
%44 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%45 = call i32 @bind(i32 %43, %struct.sockaddr* %44, i32 12)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %32
%49 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
50: ; preds = %32
br label %51
51: ; preds = %50, %29
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %53
53: ; preds = %51, %48, %27, %19
%54 = load i32, i32* %4
ret i32 %54
}

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_DGRAM = global i32 0
@.str.1 = [14 x i8] c"socket failed\00"
@.str.2 = [13 x i8] c"Fail to bind\00"
@str = [46 x i8] c"Fail to create udp server: Invalid ip or port\00"
define i32 @udp_socket_create(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = icmp eq i32 %2, 0
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = icmp eq i8* %0, null
%9 = icmp slt i32 %1, 1024
%10 = or i1 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %34
13: ; preds = %7, %3
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %34
20: ; preds = %13
br i1 %6, label %34, label %21
21: ; preds = %20
%22 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @inet_addr(i8* %0)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = call i32 @htons(i32 %1)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i32 @bind(i32 %16, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %20, %21, %32, %18, %11
%35 = phi i32 [ -1, %11 ], [ -1, %18 ], [ -1, %32 ], [ %16, %21 ], [ %16, %20 ]
ret i32 %35
}