#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char const *);
int /*<<< orphan*/ client_mode(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ server_mode(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char const *argv[]) {
  int server_fd;
  int mode;
  if (argc < 2) {
    perror("ERROR: no arguments");
    exit(EXIT_FAILURE);
  }
  mode = atoi(argv[1]);
  if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
    perror("ERROR: socket failed");
    exit(EXIT_FAILURE);
  }
  if (mode == 1) {
    printf("SERVER MODE");
    server_mode(server_fd, 8080);
  } else {
    printf("CLIENT MODE");
    client_mode(server_fd, 8080);
  }
}


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)
cmpl   $0x2,-0x8(%rbp)
jge    11aa <main+0x3a>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    11f9 <main+0x89>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x1,-0x18(%rbp)
jne    1221 <main+0xb1>
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x1f90,%esi
call   1030 <server_mode@plt>
jmp    123a <main+0xca>
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x1f90,%esi
call   1050 <client_mode@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    11e2 <main+0x72>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
je     11eb <main+0x7b>
mov    %eax,%ebx
cmp    $0x1,%ebp
jne    11c1 <main+0x51>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%edi
mov    $0x1f90,%esi
call   1030 <server_mode@plt>
jmp    11d9 <main+0x69>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%edi
mov    $0x1f90,%esi
call   1050 <client_mode@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe17(%rip),%rdi
jmp    11f2 <main+0x82>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"ERROR: no arguments\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR: socket failed\00"
@.str.2 = [12 x i8] c"SERVER MODE\00"
@.str.3 = [12 x i8] c"CLIENT MODE\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
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %7
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %14
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %14
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = call i32 @server_mode(i32 %32, i32 8080)
br label %38
34: ; preds = %27
%35 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* %6
%37 = call i32 @client_mode(i32 %36, i32 8080)
br label %38
38: ; preds = %34, %30
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [20 x i8] c"ERROR: no arguments\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR: socket failed\00"
@.str.2 = [12 x i8] c"SERVER MODE\00"
@.str.3 = [12 x i8] c"CLIENT MODE\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%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, 0
br i1 %15, label %16, label %20
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %8
%21 = icmp eq i32 %11, 1
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @server_mode(i32 %14, i32 8080)
br label %28
25: ; preds = %20
%26 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @client_mode(i32 %14, i32 8080)
br label %28
28: ; preds = %25, %22
ret i32 0
}