#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 */

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ handle_new_connection(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setup_server(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int port;
  switch (argc) {
  case 1:
    printf("No port number defined: Your port number will be 3456\n");
    port = 3456;
    break;
  case 2:
    printf("Your port number is %s\n", argv[1]);
    port = atoi(argv[1]);
    break;
  default:
    printf("Too many arguments, try again with only one\n");
    return 1;
  }
  int socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc == -1) {
    fprintf(stderr, "Can not open an IPv4 socket ! Closing now.");
    return 1;
  }
  setup_server(socket_desc, port);
  listen(socket_desc, 128);
  printf("Yeah ! Socket is opened with descriptor %d\n", socket_desc);
  handle_new_connection(socket_desc);
  close(socket_desc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x1,%eax
je     11bb <main+0x3b>
jmp    11aa <main+0x2a>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     11d5 <main+0x55>
jmp    1200 <main+0x80>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xd80,-0x14(%rbp)
jmp    121a <main+0x9a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    121a <main+0x9a>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a1 <main+0x121>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1261 <main+0xe1>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a1 <main+0x121>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <setup_server@plt>
mov    -0x18(%rbp),%edi
mov    $0x80,%esi
call   1080 <listen@plt>
mov    -0x18(%rbp),%esi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1030 <handle_new_connection@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
je     11b6 <main+0x26>
cmp    $0x1,%edi
jne    122f <main+0x9f>
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xd80,%r14d
jmp    11d7 <main+0x47>
mov    %rsi,%rbx
mov    0x8(%rsi),%rsi
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     123d <main+0xad>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14d,%esi
call   1060 <setup_server@plt>
mov    %ebp,%edi
mov    $0x80,%esi
call   1090 <listen@plt>
lea    0xe2d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1040 <handle_new_connection@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1257 <main+0xc7>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
jmp    1252 <main+0xc2>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [55 x i8] c"No port number defined: Your port number will be 3456\0A\00"
@.str.1 = [24 x i8] c"Your port number is %s\0A\00"
@.str.2 = [45 x i8] c"Too many arguments, try again with only one\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.3 = [43 x i8] c"Can not open an IPv4 socket ! Closing now.\00"
@.str.4 = [44 x i8] c"Yeah ! Socket is opened with descriptor %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
switch i32 %8, label %20 [
i32 1, label %9
i32 2, label %11
]
9: ; preds = %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
store i32 3456, i32* %6
br label %22
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
br label %22
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %43
22: ; preds = %11, %9
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %43
31: ; preds = %22
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = call i32 @setup_server(i32 %32, i32 %33)
%35 = load i32, i32* %7
%36 = call i32 @listen(i32 %35, i32 128)
%37 = load i32, i32* %7
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %7
%40 = call i32 @handle_new_connection(i32 %39)
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
store i32 0, i32* %3
br label %43
43: ; preds = %31, %28, %20
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str.1 = [24 x i8] c"Your port number is %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.3 = [43 x i8] c"Can not open an IPv4 socket ! Closing now.\00"
@.str.4 = [44 x i8] c"Yeah ! Socket is opened with descriptor %d\0A\00"
@str = [54 x i8] c"No port number defined: Your port number will be 3456\00"
@str.5 = [44 x i8] c"Too many arguments, try again with only one\00"
define i32 @main(i32 %0, i8** nocapture %1) {
switch i32 %0, label %11 [
i32 1, label %3
i32 2, label %5
]
3: ; preds = %2
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
br label %13
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %6
%10 = tail call i32 @atoi(i8* %9)
br label %13
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.5, i64 0, i64 0))
br label %28
13: ; preds = %5, %3
%14 = phi i32 [ %10, %5 ], [ 3456, %3 ]
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %28
22: ; preds = %13
%23 = tail call i32 @setup_server(i32 %17, i32 %14)
%24 = tail call i32 @listen(i32 %17, i32 128)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%26 = tail call i32 @handle_new_connection(i32 %17)
%27 = tail call i32 @close(i32 %17)
br label %28
28: ; preds = %19, %22, %11
%29 = phi i32 [ 1, %11 ], [ 1, %19 ], [ 0, %22 ]
ret i32 %29
}