#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 sin_port;
  int /*<<< orphan*/ sin_family;
};
typedef int /*<<< orphan*/ in;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int getpid();
int htons(short);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int rresvport(int *p) {
  int fd;
  short i;
  struct sockaddr_in in;
  static int next;
  fd = socket(PF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  i = 600 + ((getpid() + next++) % (1024 - 600));
  memset(&in, 0, sizeof(in));
  in.sin_family = AF_INET;
  in.sin_port = htons(i);
  printf("in.sin_port = %d\n", in.sin_port);
  if (bind(fd, &in, sizeof(in)) < 0) {
    close(fd);
    return -1;
  }
  if (p)
    *p = i;
  return fd;
}


x86 assembly (clang, -O0):
<rresvport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ae <rresvport+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <rresvport+0xee>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    0x2ea1(%rip),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,0x2e96(%rip)
add    %ecx,%eax
mov    $0x1a8,%ecx
cltd
idiv   %ecx
add    $0x258,%edx
mov    %dx,%ax
mov    %ax,-0x16(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movswl -0x16(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdf1(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1243 <rresvport+0xd3>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <rresvport+0xee>
cmpq   $0x0,-0x10(%rbp)
je     1258 <rresvport+0xe8>
movswl -0x16(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rresvport>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     124b <rresvport+0xdb>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2ea7(%rip),%ecx
lea    0x1(%rcx),%edx
mov    %edx,0x2e9e(%rip)
add    %ecx,%eax
movslq %eax,%rbx
imul   $0x4d4873ed,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x27,%rax
add    %ecx,%eax
imul   $0x1a8,%eax,%eax
sub    %eax,%ebx
add    $0x258,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movswl %bx,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0xde7(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1244 <rresvport+0xd4>
test   %r14,%r14
je     123f <rresvport+0xcf>
movzwl %bx,%eax
mov    %eax,(%r14)
mov    %ebp,%r12d
jmp    124b <rresvport+0xdb>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@rresvport.next = internal global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [18 x i8] c"in.sin_port = %d\0A\00"
define i32 @rresvport(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i16
%6 = alloca %struct.sockaddr_in
store i32* %0, i32** %3
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %45
13: ; preds = %1
%14 = call i32 (...) @getpid()
%15 = load i32, i32* @rresvport.next
%16 = add nsw i32 %15, 1
store i32 %16, i32* @rresvport.next
%17 = add nsw i32 %14, %15
%18 = srem i32 %17, 424
%19 = add nsw i32 600, %18
%20 = trunc i32 %19 to i16
store i16 %20, i16* %5
%21 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 8)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i16, i16* %5
%25 = call i32 @htons(i16 signext %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %4
%31 = call i64 @bind(i32 %30, %struct.sockaddr_in* %6, i32 8)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %13
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %2
br label %45
36: ; preds = %13
%37 = load i32*, i32** %3
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i16, i16* %5
%41 = sext i16 %40 to i32
%42 = load i32*, i32** %3
store i32 %41, i32* %42
br label %43
43: ; preds = %39, %36
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %33, %12
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@rresvport.next = internal unnamed_addr global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [18 x i8] c"in.sin_port = %d\0A\00"
define i32 @rresvport(i32* writeonly %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %30, label %8
8: ; preds = %1
%9 = tail call i32 (...) @getpid()
%10 = load i32, i32* @rresvport.next
%11 = add nsw i32 %10, 1
store i32 %11, i32* @rresvport.next
%12 = add nsw i32 %10, %9
%13 = srem i32 %12, 424
%14 = trunc i32 %13 to i16
%15 = add nsw i16 %14, 600
%16 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = call i32 @htons(i16 signext %15)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %19, i32* %20
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %19)
%22 = call i64 @bind(i32 %6, %struct.sockaddr_in* %2, i32 8)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %8
%25 = call i32 @close(i32 %6)
br label %30
26: ; preds = %8
%27 = icmp eq i32* %0, null
br i1 %27, label %30, label %28
28: ; preds = %26
%29 = zext i16 %15 to i32
store i32 %29, i32* %0
br label %30
30: ; preds = %26, %28, %1, %24
%31 = phi i32 [ -1, %24 ], [ -1, %1 ], [ %6, %28 ], [ %6, %26 ]
ret i32 %31
}