#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ make_internet_address(char *, int, struct sockaddr_in *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_dgram_server_socket(int portnum) {
  struct sockaddr_in saddr;
  char hostname[256];
  int sock_id;
  sock_id = socket(PF_INET, SOCK_DGRAM, 0);
  if (sock_id == -1)
    return -1;
  gethostname(hostname, 256);
  make_internet_address(hostname, portnum, &saddr);
  if (bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0)
    return -2;
  return sock_id;
}


x86 assembly (clang, -O0):
<make_dgram_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    1186 <make_dgram_server_socket+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <make_dgram_server_socket+0x9d>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
call   1050 <gethostname@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%esi
lea    -0x10(%rbp),%rdx
call   1030 <make_internet_address@plt>
mov    -0x114(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
je     11d4 <make_dgram_server_socket+0x94>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11dd <make_dgram_server_socket+0x9d>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_dgram_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %edi,%ebp
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11af <make_dgram_server_socket+0x6f>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   1050 <gethostname@plt>
lea    0x8(%rsp),%r15
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
call   1030 <make_internet_address@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <bind@plt>
test   %rax,%rax
mov    $0xfffffffe,%eax
cmove  %ebx,%eax
jmp    11b4 <make_dgram_server_socket+0x74>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @make_dgram_server_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [256 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %26
13: ; preds = %1
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%15 = call i32 @gethostname(i8* %14, i32 256)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%17 = load i32, i32* %3
%18 = call i32 @make_internet_address(i8* %16, i32 %17, %struct.sockaddr_in* %4)
%19 = load i32, i32* %6
%20 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 4)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %13
store i32 -2, i32* %2
br label %26
24: ; preds = %13
%25 = load i32, i32* %6
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %23, %12
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @make_dgram_server_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [256 x i8]
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = call i32 @gethostname(i8* %5, i32 256)
%12 = call i32 @make_internet_address(i8* %5, i32 %0, %struct.sockaddr_in* %2)
%13 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%14 = call i64 @bind(i32 %8, %struct.sockaddr* %13, i32 4)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i32 %8, i32 -2
br label %17
17: ; preds = %10, %1
%18 = phi i32 [ -1, %1 ], [ %16, %10 ]
ret i32 %18
}