#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
void get_file(int sd, char *f_name) {
  char buf[1024];
  int bytes_written = 1, bytes_read;
  FILE *fd;
  fd = fopen(f_name, "w");
  if (fd == NULL) {
    printf("Error! Couldn't create file!\n");
    exit(1);
  }
  while (bytes_written != 0) {
    bytes_read = recv(sd, buf, 1024, 0);
    bytes_written = fwrite(buf, sizeof(char), bytes_read, fd);
  }
  if (fclose(fd) == EOF) {
    printf("Error! Couldn't save file!\n");
    exit(1);
  }
  printf("File saved sucessfully!\n");
  return;
}


x86 assembly (clang, -O0):
<get_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x414(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    11b7 <get_file+0x57>
lea    0xe5a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11bc <get_file+0x5c>
cmpl   $0x0,-0x414(%rbp)
je     120e <get_file+0xae>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%edx
mov    -0x420(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %eax,-0x414(%rbp)
jmp    11bc <get_file+0x5c>
mov    -0x420(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
jne    1240 <get_file+0xe0>
lea    0xdef(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdf5(%rip),%rdi
call   1050 <printf@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rax
mov    %edi,%r14d
lea    0xe88(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e4 <get_file+0x84>
mov    %rax,%rbx
mov    %rsp,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
test   %eax,%eax
jne    1190 <get_file+0x30>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
je     11ed <get_file+0x8d>
lea    0xe2f(%rip),%rdi
call   1040 <puts@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4a(%rip),%rdi
jmp    11f4 <get_file+0x94>
lea    0xe26(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [30 x i8] c"Error! Couldn't create file!\0A\00"
@EOF = global i64 0
@.str.2 = [28 x i8] c"Error! Couldn't save file!\0A\00"
@.str.3 = [25 x i8] c"File saved sucessfully!\0A\00"
define void @get_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 1, i32* %6
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
br label %17
17: ; preds = %20, %16
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %17
%21 = load i32, i32* %3
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i32 @recv(i32 %21, i8* %22, i32 1024, i32 0)
store i32 %23, i32* %7
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%25 = load i32, i32* %7
%26 = load i32*, i32** %8
%27 = call i32 @fwrite(i8* %24, i32 1, i32 %25, i32* %26)
store i32 %27, i32* %6
br label %17
28: ; preds = %17
%29 = load i32*, i32** %8
%30 = call i64 @fclose(i32* %29)
%31 = load i64, i64* @EOF
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@EOF = global i64 0
@str = [24 x i8] c"File saved sucessfully!\00"
@str.4 = [27 x i8] c"Error! Couldn't save file!\00"
@str.5 = [29 x i8] c"Error! Couldn't create file!\00"
define void @get_file(i32 %0, i8* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2, %10
%11 = call i32 @recv(i32 %0, i8* %4, i32 1024, i32 0)
%12 = call i32 @fwrite(i8* %4, i32 1, i32 %11, i32* %5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %10
14: ; preds = %10
%15 = call i64 @fclose(i32* %5)
%16 = load i64, i64* @EOF
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.4, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
ret void
}