#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;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(unsigned int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udpsock_bind(char *host, unsigned int port) {
  struct sockaddr_in addr;
  int sfd;
  sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sfd < 0) {
    perror("socket() ");
    return -1;
  }
  memset((char *)&addr, 0, sizeof addr);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = inet_addr(host);
  if (bind(sfd, (struct sockaddr *)&addr, sizeof addr) < 0) {
    perror("bind() ");
    close(sfd);
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<udpsock_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c4 <udpsock_bind+0x54>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <udpsock_bind+0xc8>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1232 <udpsock_bind+0xc2>
lea    0xdf1(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <udpsock_bind+0xc8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udpsock_bind>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e60(%rip),%r12
mov    (%r12),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11fc <udpsock_bind+0x8c>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1060 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     120a <udpsock_bind+0x9a>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
jmp    121d <udpsock_bind+0xad>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    11ed <udpsock_bind+0x7d>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [10 x i8] c"socket() \00"
@.str.1 = [8 x i8] c"bind() \00"
define i32 @udpsock_bind(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
16: ; preds = %2
%17 = bitcast %struct.sockaddr_in* %6 to i8*
%18 = call i32 @memset(i8* %17, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* %5
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i8*, i8** %4
%25 = call i32 @inet_addr(i8* %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %7
%29 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %16
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %3
br label %38
36: ; preds = %16
%37 = load i32, i32* %7
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %32, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [10 x i8] c"socket() \00"
@.str.1 = [8 x i8] c"bind() \00"
define i32 @udpsock_bind(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @IPPROTO_UDP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %26
12: ; preds = %2
%13 = call i32 @memset(i8* %4, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 %1)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @inet_addr(i8* %0)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i64 @bind(i32 %8, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @close(i32 %8)
br label %26
26: ; preds = %12, %23, %10
%27 = phi i32 [ -1, %10 ], [ -1, %23 ], [ %8, %12 ]
ret i32 %27
}