#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPPROTO_TCP;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int open(char *, int);
scalar_t__ sendfile(int, int, int /*<<< orphan*/ *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int sock;
  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    fprintf(stderr, "socket() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8000);
  inet_aton("127.0.0.1", &addr.sin_addr);
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    fprintf(stderr, "connect() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  int fd = open("file.txt", O_RDWR | O_CREAT);
  if (fd == -1) {
    fprintf(stderr, "open() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  write(fd, "Blablabla", 9);
  if (sendfile(sock, fd, NULL, 10) < 0) {
    fprintf(stderr, "sendfile() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  if (unlink("file.txt")) {
    fprintf(stderr, "unlink() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1218 <main+0x78>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdfe(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cf <main+0x22f>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xdda(%rip),%rdi
call   1070 <inet_aton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1299 <main+0xf9>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd9d(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cf <main+0x22f>
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
mov    0x2d4f(%rip),%rax
or     (%rax),%esi
lea    0xd85(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1301 <main+0x161>
mov    0x2cf5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd55(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cf <main+0x22f>
mov    -0x1c(%rbp),%edi
lea    0xd49(%rip),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
mov    $0xa,%ecx
call   1080 <sendfile@plt>
cmp    $0x0,%rax
jge    1370 <main+0x1d0>
mov    0x2c86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd04(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2c42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cf <main+0x22f>
lea    0xcc0(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     13c3 <main+0x223>
mov    0x2c33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c17(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xcc9(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2bef(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cf <main+0x22f>
mov    0x2bd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e2b(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1279 <main+0xd9>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xe29(%rip),%rdi
lea    0x8(%rsp),%rbp
mov    %rbp,%rsi
call   1070 <inet_aton@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     129b <main+0xfb>
mov    0x2dc7(%rip),%rax
mov    0x2dd8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe0c(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x104>
mov    %eax,%ebp
lea    0xe16(%rip),%rsi
mov    %eax,%edi
mov    $0x9,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %ebp,%esi
xor    %edx,%edx
mov    $0xa,%ecx
call   1080 <sendfile@plt>
test   %rax,%rax
js     12ad <main+0x10d>
lea    0xdd1(%rip),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     12e8 <main+0x148>
lea    0xdff(%rip),%rbx
jmp    12b4 <main+0x114>
mov    0x2d40(%rip),%rax
mov    (%rax),%ebx
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd69(%rip),%rsi
mov    %ebx,%edi
jmp    12d0 <main+0x130>
lea    0xd7e(%rip),%rbx
jmp    12b4 <main+0x114>
lea    0xd95(%rip),%rbx
jmp    12b4 <main+0x114>
lea    0xdaa(%rip),%rbx
mov    0x2d05(%rip),%rax
mov    (%rax),%ebp
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2cb1(%rip),%rax
jmp    12df <main+0x13f>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"socket() failed: %s\0A.\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [23 x i8] c"connect() failed: %s\0A.\00"
@.str.3 = [9 x i8] c"file.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.4 = [20 x i8] c"open() failed: %s\0A.\00"
@.str.5 = [10 x i8] c"Blablabla\00"
@.str.6 = [24 x i8] c"sendfile() failed: %s\0A.\00"
@.str.7 = [22 x i8] c"unlink() failed: %s\0A.\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %1
br label %70
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 8000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%22 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %21)
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %16
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i32, i32* @EXIT_FAILURE
store i32 %32, i32* %1
br label %70
33: ; preds = %16
%34 = load i32, i32* @O_RDWR
%35 = load i32, i32* @O_CREAT
%36 = or i32 %34, %35
%37 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %36)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = load i32, i32* @EXIT_FAILURE
store i32 %45, i32* %1
br label %70
46: ; preds = %33
%47 = load i32, i32* %4
%48 = call i32 @write(i32 %47, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 9)
%49 = load i32, i32* %2
%50 = load i32, i32* %4
%51 = call i64 @sendfile(i32 %49, i32 %50, i32* null, i32 10)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %46
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %56)
%58 = load i32, i32* @EXIT_FAILURE
store i32 %58, i32* %1
br label %70
59: ; preds = %46
%60 = call i64 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %65)
%67 = load i32, i32* @EXIT_FAILURE
store i32 %67, i32* %1
br label %70
68: ; preds = %59
%69 = load i32, i32* @EXIT_SUCCESS
store i32 %69, i32* %1
br label %70
70: ; preds = %68, %62, %53, %40, %27, %10
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"socket() failed: %s\0A.\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [23 x i8] c"connect() failed: %s\0A.\00"
@.str.3 = [9 x i8] c"file.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.4 = [20 x i8] c"open() failed: %s\0A.\00"
@.str.5 = [10 x i8] c"Blablabla\00"
@.str.6 = [24 x i8] c"sendfile() failed: %s\0A.\00"
@.str.7 = [22 x i8] c"unlink() failed: %s\0A.\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = load i32, i32* @IPPROTO_TCP
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
br label %46
13: ; preds = %0
%14 = bitcast %struct.sockaddr_in* %1 to i8*
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 8000)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%20 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %19)
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i64 @connect(i32 %5, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %13
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_CREAT
%27 = or i32 %26, %25
%28 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %37, label %30
30: ; preds = %24
%31 = call i32 @write(i32 %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 9)
%32 = call i64 @sendfile(i32 %5, i32 %28, i32* null, i32 10)
%33 = icmp slt i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = call i64 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %34, %30, %24, %13
%38 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), %13 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), %24 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), %30 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), %34 ]
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %39, i8* %38, i8* %41)
br label %43
43: ; preds = %37, %34
%44 = phi i32* [ @EXIT_SUCCESS, %34 ], [ @EXIT_FAILURE, %37 ]
%45 = load i32, i32* %44
br label %46
46: ; preds = %43, %7
%47 = phi i32 [ %12, %7 ], [ %45, %43 ]
ret i32 %47
}