#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 protoent {
  int /*<<< orphan*/ p_proto;
};
typedef int /*<<< orphan*/ local_addr;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, void *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct protoent *getprotobyname(char *);
scalar_t__ getsockname(int, void *, unsigned int *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct sockaddr_in address;
  struct sockaddr_in local_addr;
  struct protoent *pr;
  int sock;
  unsigned int len = sizeof(local_addr);
  if (argc != 2) {
    fprintf(stderr, "Uso: %s <protocollo>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((pr = getprotobyname(argv[1])) == NULL) {
    fprintf(stderr, "Err. getprotobyname()\n");
    exit(EXIT_FAILURE);
  }
  if ((sock = socket(AF_INET, SOCK_STREAM, pr->p_proto)) < 0) {
    fprintf(stderr, "Err. socket() - %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons(0);
  if (bind(sock, (void *)&address, sizeof(address)) < 0) {
    fprintf(stderr, "Err. bind() - %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (getsockname(sock, (void *)&local_addr, &len) < 0) {
    fprintf(stderr, "Err. getsockname() - %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  printf("Porta allocata: %d\n", ntohs(local_addr.sin_port));
  sleep(30);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xc,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1203 <main+0x53>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <getprotobyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1243 <main+0x93>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    12a8 <main+0xf8>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xd9a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1321 <main+0x171>
mov    0x2ce4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xd35(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1090 <getsockname@plt>
cmp    $0x0,%rax
jge    1377 <main+0x1c7>
mov    0x2c8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xcf1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c48(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%edi
call   1070 <ntohs@plt>
mov    %eax,%esi
lea    0xce4(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1e,%edi
call   10c0 <sleep@plt>
mov    0x2c12(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movl   $0xc,0x4(%rsp)
cmp    $0x2,%edi
jne    1283 <main+0xd3>
mov    0x8(%rsi),%rdi
call   1060 <getprotobyname@plt>
test   %rax,%rax
je     1298 <main+0xe8>
mov    0x2e07(%rip),%r14
mov    (%r14),%edi
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12b1 <main+0x101>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
xor    %edi,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     12d1 <main+0x121>
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1090 <getsockname@plt>
test   %rax,%rax
js     12f1 <main+0x141>
mov    0x18(%rsp),%edi
call   1070 <ntohs@plt>
lea    0xe0b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x1e,%edi
call   10c0 <sleep@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd6a(%rip),%rsi
jmp    1314 <main+0x164>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    131b <main+0x16b>
mov    0x2d18(%rip),%rax
mov    (%rax),%ebx
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd5e(%rip),%rsi
jmp    130f <main+0x15f>
mov    0x2cf8(%rip),%rax
mov    (%rax),%ebx
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd52(%rip),%rsi
jmp    130f <main+0x15f>
mov    0x2cd8(%rip),%rax
mov    (%rax),%ebx
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd44(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2c96(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.protoent = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Uso: %s <protocollo>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [23 x i8] c"Err. getprotobyname()\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [20 x i8] c"Err. socket() - %s\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [18 x i8] c"Err. bind() - %s\0A\00"
@.str.4 = [25 x i8] c"Err. getsockname() - %s\0A\00"
@.str.5 = [20 x i8] c"Porta allocata: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.protoent*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12, i32* %10
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call %struct.protoent* @getprotobyname(i8* %24)
store %struct.protoent* %25, %struct.protoent** %8
%26 = icmp eq %struct.protoent* %25, null
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %21
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_STREAM
%35 = load %struct.protoent*, %struct.protoent** %8
%36 = getelementptr inbounds %struct.protoent, %struct.protoent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @socket(i32 %33, i32 %34, i32 %37)
store i32 %38, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %32
%48 = load i32, i32* @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %48, i32* %49
%50 = load i32, i32* @INADDR_ANY
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
store i32 %50, i32* %52
%53 = call i32 @htons(i32 0)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %53, i32* %54
%55 = load i32, i32* %9
%56 = bitcast %struct.sockaddr_in* %6 to i8*
%57 = call i64 @bind(i32 %55, i8* %56, i32 12)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %66
59: ; preds = %47
%60 = load i32, i32* @stderr
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %47
%67 = load i32, i32* %9
%68 = bitcast %struct.sockaddr_in* %7 to i8*
%69 = call i64 @getsockname(i32 %67, i8* %68, i32* %10)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %66
%72 = load i32, i32* @stderr
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %74)
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %66
%79 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%80 = load i32, i32* %79
%81 = call i32 @ntohs(i32 %80)
%82 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %81)
%83 = call i32 @sleep(i32 30)
%84 = load i32, i32* @EXIT_SUCCESS
ret i32 %84
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.protoent = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Uso: %s <protocollo>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [23 x i8] c"Err. getprotobyname()\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [20 x i8] c"Err. socket() - %s\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [18 x i8] c"Err. bind() - %s\0A\00"
@.str.4 = [25 x i8] c"Err. getsockname() - %s\0A\00"
@.str.5 = [20 x i8] c"Porta allocata: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%9 = icmp eq i32 %0, 2
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call %struct.protoent* @getprotobyname(i8* %18)
%20 = icmp eq %struct.protoent* %19, null
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = getelementptr inbounds %struct.protoent, %struct.protoent* %19, i64 0, i32 0
%30 = load i32, i32* %29
%31 = tail call i32 @socket(i32 %27, i32 %28, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %26
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %41, i32* %42
%43 = load i32, i32* @INADDR_ANY
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %43, i32* %44
%45 = tail call i32 @htons(i32 0)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %45, i32* %46
%47 = call i64 @bind(i32 %31, i8* %6, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %40
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %40
%57 = call i64 @getsockname(i32 %31, i8* %7, i32* %5)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %66
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %56
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @ntohs(i32 %68)
%70 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %69)
%71 = call i32 @sleep(i32 30)
%72 = load i32, i32* @EXIT_SUCCESS
ret i32 %72
}