#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 */
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ tempAdr;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  void *sin_port;
  TYPE_1__ sin_addr;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
void *htons(int /*<<< orphan*/);
int /*<<< orphan*/ ntohs(void *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int, int, int);
int MakeSocket(uint16_t *port) {
  int domain, type, protocol, serverFD;
  struct sockaddr_in address, tempAdr;
  domain = AF_INET;
  type = SOCK_DGRAM;
  protocol = 0;
  serverFD = socket(domain, type, protocol);
  bzero(&address, sizeof(struct sockaddr_in));
  bzero(&tempAdr, sizeof(struct sockaddr_in));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = htonl(INADDR_ANY);
  if (*port == 0)
    address.sin_port = htons(0);
  else
    address.sin_port = htons(*port);
  if (bind(serverFD, (struct sockaddr *)&address, sizeof(struct sockaddr_in)) ==
      -1) {
    printf("Bind() Error\n");
    printf("Errno: %d\n", errno);
    exit(EXIT_FAILURE);
  }
  socklen_t tempsize = sizeof(tempAdr);
  if (getsockname(serverFD, (struct sockaddr *)&tempAdr, &tempsize)) {
    printf("getsockname() broke\n");
    printf("Errno: %d\n", errno);
    exit(EXIT_FAILURE);
  }
  *port = ntohs(tempAdr.sin_port);
  return serverFD;
}


x86 assembly (clang, -O0):
<MakeSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0xc(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x18,%esi
call   1080 <bzero@plt>
lea    -0x48(%rbp),%rdi
mov    $0x18,%esi
call   1080 <bzero@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1225 <MakeSocket+0x95>
xor    %edi,%edi
call   1030 <htons@plt>
mov    %rax,-0x30(%rbp)
jmp    1234 <MakeSocket+0xa4>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1281 <MakeSocket+0xf1>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x18,-0x4c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x4c(%rbp),%rdx
call   1090 <getsockname@plt>
cmp    $0x0,%rax
je     12d5 <MakeSocket+0x145>
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%rdi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MakeSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
mov    0x2e34(%rip),%r15
mov    (%r15),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
lea    0x20(%rsp),%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
mov    (%r15),%rax
mov    %rax,0x18(%rsp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     125b <MakeSocket+0xbb>
movl   $0x18,0x4(%rsp)
lea    0x20(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   10a0 <getsockname@plt>
test   %rax,%rax
jne    1264 <MakeSocket+0xc4>
mov    0x20(%rsp),%rdi
call   1070 <ntohs@plt>
mov    %eax,(%rbx)
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbd(%rip),%rdi
jmp    126b <MakeSocket+0xcb>
lea    0xda0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    0xd80(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [14 x i8] c"Bind() Error\0A\00"
@.str.1 = [11 x i8] c"Errno: %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [21 x i8] c"getsockname() broke\0A\00"
define i32 @MakeSocket(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i32* %0, i32** %2
%10 = load i8*, i8** @AF_INET
%11 = ptrtoint i8* %10 to i32
store i32 %11, i32* %3
%12 = load i32, i32* @SOCK_DGRAM
store i32 %12, i32* %4
store i32 0, i32* %5
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %6
%17 = call i32 @bzero(%struct.sockaddr_in* %7, i32 24)
%18 = call i32 @bzero(%struct.sockaddr_in* %8, i32 24)
%19 = load i8*, i8** @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i8* %19, i8** %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32*, i32** %2
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %1
%29 = call i8* @htons(i32 0)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i8* %29, i8** %30
br label %36
31: ; preds = %1
%32 = load i32*, i32** %2
%33 = load i32, i32* %32
%34 = call i8* @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i8* %34, i8** %35
br label %36
36: ; preds = %31, %28
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 24)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %47
41: ; preds = %36
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%43 = load i32, i32* @errno
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %36
store i32 24, i32* %9
%48 = load i32, i32* %6
%49 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%50 = call i64 @getsockname(i32 %48, %struct.sockaddr* %49, i32* %9)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %47
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* @errno
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %54)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %47
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = call i32 @ntohs(i8* %60)
%62 = load i32*, i32** %2
store i32 %61, i32* %62
%63 = load i32, i32* %6
ret i32 %63
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [11 x i8] c"Errno: %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@str = [20 x i8] c"getsockname() broke\00"
@str.3 = [13 x i8] c"Bind() Error\00"
define i32 @MakeSocket(i32* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i8*, i8** @AF_INET
%8 = ptrtoint i8* %7 to i64
%9 = trunc i64 %8 to i32
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = call i32 @bzero(%struct.sockaddr_in* %2, i32 24)
%13 = call i32 @bzero(%struct.sockaddr_in* %3, i32 24)
%14 = load i8*, i8** @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i8* %14, i8** %15
%16 = load i32, i32* @INADDR_ANY
%17 = tail call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %0
%20 = tail call i8* @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i8* %20, i8** %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %11, %struct.sockaddr* %22, i32 24)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %1
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.3, i64 0, i64 0))
%27 = load i32, i32* @errno
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %1
%32 = bitcast i32* %4 to i8*
store i32 24, i32* %4
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @getsockname(i32 %11, %struct.sockaddr* %33, i32* %4)
%35 = icmp eq i64 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %31
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%38 = load i32, i32* @errno
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %31
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @ntohs(i8* %44)
store i32 %45, i32* %0
ret i32 %11
}