#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int get_fd_num(char *);
int open(char *, int /*<<< orphan*/);
int send_fd(int, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv, char **envp) {
  int sock, fd, ret;
  if (argc != 3) {
    fprintf(stderr, "Usage: %s < socket-fd > < file-path >\n", argv[0]);
    return EXIT_FAILURE;
  }
  sock = get_fd_num(argv[1]);
  if (sock < 0) {
    return EXIT_FAILURE;
  }
  fd = open(argv[2], O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open file '%s'\n", argv[2]);
    return EXIT_FAILURE;
  }
  ret = send_fd(sock, fd);
  if (ret < 0) {
    close(fd);
    return EXIT_FAILURE;
  }
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a1 <main+0x51>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe75(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <main+0x112>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <get_fd_num@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cc <main+0x7c>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <main+0x112>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    121d <main+0xcd>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe20(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <main+0x112>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <send_fd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    124e <main+0xfe>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <main+0x112>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    0x2d6b(%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   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    11b0 <main+0x60>
mov    0x8(%rbx),%rdi
call   1040 <get_fd_num@plt>
mov    0x2e61(%rip),%rbp
test   %eax,%eax
js     11cf <main+0x7f>
mov    %eax,%r15d
mov    0x10(%rbx),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11dd <main+0x8d>
mov    %eax,%r14d
mov    %r15d,%edi
mov    %eax,%esi
call   1030 <send_fd@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
test   %ebx,%ebx
js     11c8 <main+0x78>
mov    0x2e1a(%rip),%rbp
jmp    11cf <main+0x7f>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe3d(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e01(%rip),%rbp
mov    0x0(%rbp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xe36(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11cf <main+0x7f>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"Usage: %s < socket-fd > < file-path >\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Failed to open file '%s'\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %20
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %6
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %4
br label %58
20: ; preds = %3
%21 = load i8**, i8*** %6
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @get_fd_num(i8* %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = load i32, i32* @EXIT_FAILURE
store i32 %28, i32* %4
br label %58
29: ; preds = %20
%30 = load i8**, i8*** %6
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %29
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %6
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
store i32 %43, i32* %4
br label %58
44: ; preds = %29
%45 = load i32, i32* %8
%46 = load i32, i32* %9
%47 = call i32 @send_fd(i32 %45, i32 %46)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = load i32, i32* %9
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* @EXIT_FAILURE
store i32 %53, i32* %4
br label %58
54: ; preds = %44
%55 = load i32, i32* %9
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* @EXIT_SUCCESS
store i32 %57, i32* %4
br label %58
58: ; preds = %54, %50, %37, %27, %13
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"Usage: %s < socket-fd > < file-path >\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Failed to open file '%s'\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = icmp eq i32 %0, 3
br i1 %4, label %9, label %5
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %29
9: ; preds = %3
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @get_fd_num(i8* %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %29, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %15
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %29
24: ; preds = %14
%25 = tail call i32 @send_fd(i32 %12, i32 %18)
%26 = icmp slt i32 %25, 0
%27 = tail call i32 @close(i32 %18)
%28 = select i1 %26, i32* @EXIT_FAILURE, i32* @EXIT_SUCCESS
br label %29
29: ; preds = %24, %9, %20, %5
%30 = phi i32* [ @EXIT_FAILURE, %5 ], [ @EXIT_FAILURE, %20 ], [ @EXIT_FAILURE, %9 ], [ %28, %24 ]
%31 = load i32, i32* %30
ret i32 %31
}