#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*/ fatalError(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int read(int, ...);
int write(int, int *, int);
int receiveHello(int socketFD, char **username) {
  int nBytesRead = 0;
  int n = 1;
  char command[4];
  int argLength;
  int response = 0;
  n = read(socketFD, &argLength, 4);
  if (n != 4)
    fatalError("Server protocol error 0");
  if (write(socketFD, &response, 4) == -1)
    fatalError("Server protocol error 2");
  n = read(socketFD, &argLength, 4);
  if (n != 4)
    fatalError("Server protocol error 3");
  if (write(socketFD, &response, 4) == -1)
    fatalError("Server protocol error 4");
  if (argLength != 1) {
    *username = (char *)malloc(argLength * sizeof(char) + 1);
    if (*username == NULL)
      perror("malloc");
    n = read(socketFD, *username, argLength);
    write(socketFD, &response, 4);
  }
  return 0;
}


x86 assembly (clang, -O0):
<receiveHello>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
je     11a0 <receiveHello+0x50>
lea    0xe65(%rip),%rdi
call   1060 <fatalError@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11c6 <receiveHello+0x76>
lea    0xe57(%rip),%rdi
call   1060 <fatalError@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
je     11f2 <receiveHello+0xa2>
lea    0xe43(%rip),%rdi
call   1060 <fatalError@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1218 <receiveHello+0xc8>
lea    0xe35(%rip),%rdi
call   1060 <fatalError@plt>
cmpl   $0x1,-0x20(%rbp)
je     1281 <receiveHello+0x131>
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1259 <receiveHello+0x109>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receiveHello>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
movl   $0x0,(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <read@plt>
cmp    $0x4,%eax
je     1182 <receiveHello+0x32>
lea    0xe83(%rip),%rdi
call   1060 <fatalError@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11a2 <receiveHello+0x52>
lea    0xe7b(%rip),%rdi
call   1060 <fatalError@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <read@plt>
cmp    $0x4,%eax
je     11c6 <receiveHello+0x76>
lea    0xe6f(%rip),%rdi
call   1060 <fatalError@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11e6 <receiveHello+0x96>
lea    0xe67(%rip),%rdi
call   1060 <fatalError@plt>
mov    0x4(%rsp),%edi
cmp    $0x1,%edi
je     121e <receiveHello+0xce>
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1228 <receiveHello+0xd8>
mov    0x4(%rsp),%edx
mov    %ebx,%edi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <read@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
mov    (%r14),%rax
jmp    11ff <receiveHello+0xaf>

LLVM IR (-O0):
@.str = [24 x i8] c"Server protocol error 0\00"
@.str.1 = [24 x i8] c"Server protocol error 2\00"
@.str.2 = [24 x i8] c"Server protocol error 3\00"
@.str.3 = [24 x i8] c"Server protocol error 4\00"
@.str.4 = [7 x i8] c"malloc\00"
define i32 @receiveHello(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %5
store i32 1, i32* %6
store i32 0, i32* %9
%10 = load i32, i32* %3
%11 = call i32 (i32, ...) @read(i32 %10, i32* %8, i32 4)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @fatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* %3
%18 = call i32 @write(i32 %17, i32* %9, i32 4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @fatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* %3
%24 = call i32 (i32, ...) @read(i32 %23, i32* %8, i32 4)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 4
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @fatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = load i32, i32* %3
%31 = call i32 @write(i32 %30, i32* %9, i32 4)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @fatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* %8
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %60
38: ; preds = %35
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = mul i64 %40, 1
%42 = add i64 %41, 1
%43 = trunc i64 %42 to i32
%44 = call i64 @malloc(i32 %43)
%45 = inttoptr i64 %44 to i8*
%46 = load i8**, i8*** %4
store i8* %45, i8** %46
%47 = load i8**, i8*** %4
%48 = load i8*, i8** %47
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %38
%51 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %38
%53 = load i32, i32* %3
%54 = load i8**, i8*** %4
%55 = load i8*, i8** %54
%56 = load i32, i32* %8
%57 = call i32 (i32, ...) @read(i32 %53, i8* %55, i32 %56)
store i32 %57, i32* %6
%58 = load i32, i32* %3
%59 = call i32 @write(i32 %58, i32* %9, i32 4)
br label %60
60: ; preds = %52, %35
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"Server protocol error 0\00"
@.str.1 = [24 x i8] c"Server protocol error 2\00"
@.str.2 = [24 x i8] c"Server protocol error 3\00"
@.str.3 = [24 x i8] c"Server protocol error 4\00"
@.str.4 = [7 x i8] c"malloc\00"
define i32 @receiveHello(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = call i32 (i32, ...) @read(i32 %0, i32* %3, i32 4)
%8 = icmp eq i32 %7, 4
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @fatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 @write(i32 %0, i32* %4, i32 4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @fatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = call i32 (i32, ...) @read(i32 %0, i32* %3, i32 4)
%18 = icmp eq i32 %17, 4
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @fatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = call i32 @write(i32 %0, i32* %4, i32 4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @fatalError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 1
br i1 %28, label %42, label %29
29: ; preds = %26
%30 = add i32 %27, 1
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %1
%33 = icmp eq i64 %31, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%36 = load i8*, i8** %1
br label %37
37: ; preds = %34, %29
%38 = phi i8* [ %36, %34 ], [ %32, %29 ]
%39 = load i32, i32* %3
%40 = call i32 (i32, ...) @read(i32 %0, i8* %38, i32 %39)
%41 = call i32 @write(i32 %0, i32* %4, i32 4)
br label %42
42: ; preds = %37, %26
ret i32 0
}