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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_TOS;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntop(int /*<<< orphan*/, TYPE_1__ *, char *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, unsigned char *,
               int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int sock, err;
  struct sockaddr_in sa, sa2;
  socklen_t sa_len;
  int port = 4444;
  char junk[128];
  unsigned char tos;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1) {
    perror("socket");
    return 1;
  }
  if (argc >= 2)
    port = strtol(argv[1], NULL, 10);
  memset(&sa, 0, sizeof(struct sockaddr));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = htonl(INADDR_ANY);
  sa_len = sizeof(struct sockaddr_in);
  err = bind(sock, (struct sockaddr *)&sa, sa_len);
  if (err != 0) {
    perror("bind");
    return 1;
  }
  err = getsockname(sock, (struct sockaddr *)&sa2, &sa_len);
  if (err != 0) {
    perror("getsockname");
    return 1;
  }
  fprintf(stderr, "Socket bound to %s/%d.\n",
          inet_ntop(sa2.sin_family, &sa2.sin_addr, junk, sa_len),
          ntohs(sa2.sin_port));
  tos = 0x00;
  err = setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, 1);
  if (err != 0)
    perror("setsockopt");
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x115c,-0x40(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122e <main+0x5e>
lea    0xde3(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a2 <main+0x1d2>
cmpl   $0x2,-0x8(%rbp)
jl     1251 <main+0x81>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %eax,-0x40(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x40(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   10c0 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12c4 <main+0xf4>
lea    0xd54(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a2 <main+0x1d2>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10e0 <getsockname@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12f9 <main+0x129>
lea    0xd24(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a2 <main+0x1d2>
mov    0x2cc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xd4(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x38(%rbp),%rsi
add    $0x4,%rsi
lea    -0xc0(%rbp),%rdx
mov    -0x3c(%rbp),%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0xd0(%rbp)
mov    -0x38(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0xd4(%rbp),%edi
mov    -0xd0(%rbp),%rdx
mov    %eax,%ecx
lea    0xcd1(%rip),%rsi
call   1090 <fprintf@plt>
movb   $0x0,-0xc1(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2c83(%rip),%rax
mov    (%rax),%esi
mov    0x2c72(%rip),%rax
mov    (%rax),%edx
lea    -0xc1(%rbp),%rcx
mov    $0x1,%r8d
call   1040 <setsockopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1393 <main+0x1c3>
lea    0xca2(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xb0,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2dfd(%rip),%r12
mov    (%r12),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1321 <main+0x151>
mov    %eax,%r15d
mov    $0x115c,%ebp
cmp    $0x2,%ebx
jl     1227 <main+0x57>
mov    0x8(%r14),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %eax,%ebp
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x28(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x20(%rsp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x24(%rsp)
movl   $0xc,0xc(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %eax,%eax
jne    132a <main+0x15a>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   10e0 <getsockname@plt>
test   %eax,%eax
jne    1333 <main+0x163>
mov    0x2d1f(%rip),%rax
mov    (%rax),%ebx
mov    0x18(%rsp),%edi
lea    0x14(%rsp),%rsi
mov    0xc(%rsp),%ecx
lea    0x30(%rsp),%rdx
call   1030 <inet_ntop@plt>
mov    %rax,%rbp
mov    0x10(%rsp),%edi
call   10b0 <ntohs@plt>
lea    0xd4b(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1090 <fprintf@plt>
movb   $0x0,0xb(%rsp)
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
mov    0x2cea(%rip),%rax
mov    (%rax),%edx
lea    0xb(%rsp),%rcx
mov    %r15d,%edi
mov    $0x1,%r8d
call   1040 <setsockopt@plt>
test   %eax,%eax
jne    1346 <main+0x176>
mov    %r15d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd8(%rip),%rdi
jmp    133a <main+0x16a>
lea    0xcd6(%rip),%rdi
jmp    133a <main+0x16a>
lea    0xcd2(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%eax
jmp    1311 <main+0x141>
lea    0xce3(%rip),%rdi
call   10d0 <perror@plt>
jmp    1307 <main+0x137>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [12 x i8] c"getsockname\00"
@stderr = global i32 0
@.str.3 = [24 x i8] c"Socket bound to %s/%d.\0A\00"
@IPPROTO_IP = global i32 0
@IP_TOS = global i32 0
@.str.4 = [11 x i8] c"setsockopt\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca [128 x i8]
%13 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4444, i32* %11
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %79
21: ; preds = %2
%22 = load i32, i32* %4
%23 = icmp sge i32 %22, 2
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @strtol(i8* %27, i32* null, i32 10)
store i32 %28, i32* %11
br label %29
29: ; preds = %24, %21
%30 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 4)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %31, i32* %32
%33 = load i32, i32* %11
%34 = call i32 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
store i32 12, i32* %10
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%42 = load i32, i32* %10
%43 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 %42)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %29
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %79
48: ; preds = %29
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%51 = call i32 @getsockname(i32 %49, %struct.sockaddr* %50, i32* %10)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %79
56: ; preds = %48
%57 = load i32, i32* @stderr
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%59 = load i32, i32* %58
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%61 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%62 = load i32, i32* %10
%63 = call i8* @inet_ntop(i32 %59, %struct.TYPE_2__* %60, i8* %61, i32 %62)
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i32 @ntohs(i32 %65)
%67 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %63, i32 %66)
store i8 0, i8* %13
%68 = load i32, i32* %6
%69 = load i32, i32* @IPPROTO_IP
%70 = load i32, i32* @IP_TOS
%71 = call i32 @setsockopt(i32 %68, i32 %69, i32 %70, i8* %13, i32 1)
store i32 %71, i32* %7
%72 = load i32, i32* %7
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %56
%75 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %76
76: ; preds = %74, %56
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
store i32 0, i32* %3
br label %79
79: ; preds = %76, %54, %46, %19
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [12 x i8] c"getsockname\00"
@stderr = global i32 0
@.str.3 = [24 x i8] c"Socket bound to %s/%d.\0A\00"
@IPPROTO_IP = global i32 0
@IP_TOS = global i32 0
@.str.4 = [11 x i8] c"setsockopt\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca i8
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %64
18: ; preds = %2
%19 = icmp sgt i32 %0, 1
br i1 %19, label %20, label %24
20: ; preds = %18
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i32 @strtol(i8* nocapture %22, i32* null, i32 10)
br label %24
24: ; preds = %20, %18
%25 = phi i32 [ %23, %20 ], [ 4444, %18 ]
%26 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 4)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 %25)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %32, i32* %33
store i32 12, i32* %5
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i32 @bind(i32 %14, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %24
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %64
39: ; preds = %24
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = call i32 @getsockname(i32 %14, %struct.sockaddr* %40, i32* %5)
%42 = icmp eq i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %64
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%50 = load i32, i32* %5
%51 = call i8* @inet_ntop(i32 %48, %struct.TYPE_2__* %49, i8* %11, i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @ntohs(i32 %53)
%55 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %51, i32 %54)
store i8 0, i8* %7
%56 = load i32, i32* @IPPROTO_IP
%57 = load i32, i32* @IP_TOS
%58 = call i32 @setsockopt(i32 %14, i32 %56, i32 %57, i8* %7, i32 1)
%59 = icmp eq i32 %58, 0
br i1 %59, label %62, label %60
60: ; preds = %45
%61 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %62
62: ; preds = %60, %45
%63 = call i32 @close(i32 %14)
br label %64
64: ; preds = %62, %43, %37, %16
%65 = phi i32 [ 1, %16 ], [ 1, %37 ], [ 1, %43 ], [ 0, %62 ]
ret i32 %65
}