#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};
struct message_s {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ close_serving_thread(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fstat(int, struct stat *);
char *make_path(char *);
int open(char *, int /*<<< orphan*/);
char *payload_malloc(int, struct message_s *, int);
int /*<<< orphan*/ printf(char *, char *);
int transfer_file_sys(int, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ write_head(int, int, int, int /*<<< orphan*/);
int req_get(int sockfd, struct message_s *msg) {
  char *filepath = payload_malloc(sockfd, msg, true);
  char *repopath = make_path(filepath);
  printf("client wants %s\n", repopath);
  int local_fd = open(repopath, O_RDONLY);
  free(filepath);
  free(repopath);
  if (local_fd == -1) {
    write_head(sockfd, 0xA8, 0, 0);
    return -1;
  }
  write_head(sockfd, 0xA8, 1, 0);
  struct stat st;
  fstat(local_fd, &st);
  write_head(sockfd, 0xFF, 0x00, st.st_size);
  int ret = transfer_file_sys(sockfd, local_fd, st.st_size, NULL);
  close(local_fd);
  if (ret == -1)
    close_serving_thread(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<req_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <payload_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <make_path@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe24(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x24(%rbp)
jne    122f <req_get+0x8f>
mov    -0x8(%rbp),%edi
mov    $0xa8,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <write_head@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <req_get+0xf7>
mov    -0x8(%rbp),%edi
mov    $0xa8,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1070 <write_head@plt>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10c0 <fstat@plt>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%ecx
mov    $0xff,%esi
xor    %edx,%edx
call   1070 <write_head@plt>
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <transfer_file_sys@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1290 <req_get+0xf0>
mov    -0x8(%rbp),%edi
call   1040 <close_serving_thread@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<req_get>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r15d
mov    $0x1,%edx
call   10b0 <payload_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <make_path@plt>
mov    %rax,%rbp
lea    0xe32(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%r12d
je     1261 <req_get+0xc1>
xor    %r14d,%r14d
mov    %r15d,%edi
mov    $0xa8,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1070 <write_head@plt>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
call   10c0 <fstat@plt>
mov    0x8(%rsp),%ebx
mov    %r15d,%edi
mov    $0xff,%esi
xor    %edx,%edx
mov    %ebx,%ecx
call   1070 <write_head@plt>
mov    %r15d,%edi
mov    %r12d,%esi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1080 <transfer_file_sys@plt>
mov    %eax,%ebx
mov    %r12d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%ebx
jne    1278 <req_get+0xd8>
mov    %r15d,%edi
call   1040 <close_serving_thread@plt>
jmp    1278 <req_get+0xd8>
mov    %r15d,%edi
mov    $0xa8,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <write_head@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.message_s = type { i32 }
%struct.stat = type { i32 }
@.str = [17 x i8] c"client wants %s\0A\00"
@O_RDONLY = global i32 0
define i32 @req_get(i32 %0, %struct.message_s* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.message_s*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i32
store i32 %0, i32* %4
store %struct.message_s* %1, %struct.message_s** %5
%11 = load i32, i32* %4
%12 = load %struct.message_s*, %struct.message_s** %5
%13 = call i8* @payload_malloc(i32 %11, %struct.message_s* %12, i32 1)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = call i8* @make_path(i8* %14)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %7
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %8
%21 = load i8*, i8** %6
%22 = call i32 @free(i8* %21)
%23 = load i8*, i8** %7
%24 = call i32 @free(i8* %23)
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %2
%28 = load i32, i32* %4
%29 = call i32 @write_head(i32 %28, i32 168, i32 0, i32 0)
store i32 -1, i32* %3
br label %52
30: ; preds = %2
%31 = load i32, i32* %4
%32 = call i32 @write_head(i32 %31, i32 168, i32 1, i32 0)
%33 = load i32, i32* %8
%34 = call i32 @fstat(i32 %33, %struct.stat* %9)
%35 = load i32, i32* %4
%36 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @write_head(i32 %35, i32 255, i32 0, i32 %37)
%39 = load i32, i32* %4
%40 = load i32, i32* %8
%41 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @transfer_file_sys(i32 %39, i32 %40, i32 %42, i32* null)
store i32 %43, i32* %10
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %10
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %30
%49 = load i32, i32* %4
%50 = call i32 @close_serving_thread(i32 %49)
br label %51
51: ; preds = %48, %30
store i32 0, i32* %3
br label %52
52: ; preds = %51, %27
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.message_s = type { i32 }
%struct.stat = type { i32 }
@.str = [17 x i8] c"client wants %s\0A\00"
@O_RDONLY = global i32 0
define i32 @req_get(i32 %0, %struct.message_s* %1) {
%3 = alloca %struct.stat
%4 = tail call i8* @payload_malloc(i32 %0, %struct.message_s* %1, i32 1)
%5 = tail call i8* @make_path(i8* %4)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %5, i32 %7)
%9 = tail call i32 @free(i8* %4)
%10 = tail call i32 @free(i8* %5)
%11 = icmp eq i32 %8, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @write_head(i32 %0, i32 168, i32 0, i32 0)
br label %27
14: ; preds = %2
%15 = tail call i32 @write_head(i32 %0, i32 168, i32 1, i32 0)
%16 = bitcast %struct.stat* %3 to i8*
%17 = call i32 @fstat(i32 %8, %struct.stat* %3)
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 @write_head(i32 %0, i32 255, i32 0, i32 %19)
%21 = tail call i32 @transfer_file_sys(i32 %0, i32 %8, i32 %19, i32* null)
%22 = tail call i32 @close(i32 %8)
%23 = icmp eq i32 %21, -1
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = tail call i32 @close_serving_thread(i32 %0)
br label %26
26: ; preds = %24, %14
br label %27
27: ; preds = %26, %12
%28 = phi i32 [ -1, %12 ], [ 0, %26 ]
ret i32 %28
}