#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*/ fprintf(int /*<<< orphan*/, char *, int, ...);
char *malloc(int);
int read(int, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void read_header(int session_fd) {
  char header[4];
  char *body;
  int nbytes = recv(session_fd, header, 4, 0);
  fprintf(stderr, "read %d bytes\n", nbytes);
  fprintf(stderr, "%02x %02x %02x %02x\n", header[0], header[1], header[2],
          header[3]);
  int length = header[0] + (header[1] << 8) + (header[2] << 8);
  int seq_num = header[3];
  body = malloc(length);
  nbytes = read(session_fd, body, length);
  fprintf(stderr, "length: %d, seq_num: %d\n", length, seq_num);
}


x86 assembly (clang, -O0):
<read_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe8c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
movsbl -0x8(%rbp),%edx
movsbl -0x7(%rbp),%ecx
movsbl -0x6(%rbp),%r8d
movsbl -0x5(%rbp),%r9d
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movsbl -0x8(%rbp),%eax
movsbl -0x7(%rbp),%ecx
shl    $0x8,%ecx
add    %ecx,%eax
movsbl -0x6(%rbp),%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    %eax,-0x18(%rbp)
movsbl -0x5(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_header>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    0x2e7e(%rip),%r15
mov    (%r15),%edi
lea    0xe94(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
movsbl 0x4(%rsp),%edx
movsbl 0x5(%rsp),%ecx
movsbl 0x6(%rsp),%r8d
movsbl 0x7(%rsp),%r9d
lea    0xe7a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
movsbl 0x4(%rsp),%eax
movsbl 0x5(%rsp),%ecx
shl    $0x8,%ecx
add    %eax,%ecx
movsbl 0x6(%rsp),%ebp
shl    $0x8,%ebp
add    %ecx,%ebp
movsbl 0x7(%rsp),%ebx
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %r14d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
mov    (%r15),%edi
lea    0xe4c(%rip),%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"read %d bytes\0A\00"
@.str.1 = [21 x i8] c"%02x %02x %02x %02x\0A\00"
@.str.2 = [25 x i8] c"length: %d, seq_num: %d\0A\00"
define void @read_header(i32 %0) {
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%10 = call i32 @recv(i32 %8, i8* %9, i32 4, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* @stderr
%12 = load i32, i32* %5
%13 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @stderr
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %20, i32 %23, i32 %26)
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = shl i32 %33, 8
%35 = add nsw i32 %30, %34
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = shl i32 %38, 8
%40 = add nsw i32 %35, %39
store i32 %40, i32* %6
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
store i32 %43, i32* %7
%44 = load i32, i32* %6
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %4
%46 = load i32, i32* %2
%47 = load i8*, i8** %4
%48 = load i32, i32* %6
%49 = call i32 @read(i32 %46, i8* %47, i32 %48)
store i32 %49, i32* %5
%50 = load i32, i32* @stderr
%51 = load i32, i32* %6
%52 = load i32, i32* %7
%53 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %51, i32 %52)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"read %d bytes\0A\00"
@.str.1 = [21 x i8] c"%02x %02x %02x %02x\0A\00"
@.str.2 = [25 x i8] c"length: %d, seq_num: %d\0A\00"
define void @read_header(i32 %0) {
%2 = alloca [4 x i8]
%3 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%4 = call i32 @recv(i32 %0, i8* %3, i32 4, i32 0)
%5 = load i32, i32* @stderr
%6 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%7 = load i32, i32* @stderr
%8 = load i8, i8* %3
%9 = sext i8 %8 to i32
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 2
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 3
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %12, i32 %15, i32 %18)
%20 = load i8, i8* %3
%21 = sext i8 %20 to i32
%22 = load i8, i8* %10
%23 = sext i8 %22 to i32
%24 = shl nsw i32 %23, 8
%25 = add nsw i32 %24, %21
%26 = load i8, i8* %13
%27 = sext i8 %26 to i32
%28 = shl nsw i32 %27, 8
%29 = add nsw i32 %25, %28
%30 = load i8, i8* %16
%31 = sext i8 %30 to i32
%32 = call i8* @malloc(i32 %29)
%33 = call i32 @read(i32 %0, i8* %32, i32 %29)
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %31)
ret void
}