#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*/ atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(scalar_t__);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int send_fd(int /*<<< orphan*/, int);
int main(int argc, char **argv) {
  int fd;
  if (argc != 4) {
    perror("openfile <sockfd#> <filename> <mode>");
    exit(1);
  }
  if ((fd = open(argv[2], atoi(argv[3]))) < 0)
    exit((errno > 0) ? errno : 255);
  return send_fd(atoi(argv[1]), fd);
}


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)
cmpl   $0x4,-0x8(%rbp)
je     1186 <main+0x36>
lea    0xe89(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1060 <atoi@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f1 <main+0xa1>
mov    0x2e23(%rip),%rax
cmpq   $0x0,(%rax)
jle    11da <main+0x8a>
mov    0x2e12(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11e8 <main+0x98>
mov    $0xff,%eax
mov    %rax,-0x28(%rbp)
jmp    11e8 <main+0x98>
mov    -0x28(%rbp),%rdi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%esi
call   1030 <send_fd@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x4,%edi
jne    1190 <main+0x40>
mov    %rsi,%rbx
mov    0x10(%rsi),%rbp
mov    0x18(%rsi),%rdi
call   1060 <atoi@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1040 <open@plt>
test   %eax,%eax
js     11a6 <main+0x56>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   1060 <atoi@plt>
mov    %eax,%edi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <send_fd@plt>
lea    0xe69(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
test   %rax,%rax
mov    $0xff,%edi
cmovg  %rax,%rdi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"openfile <sockfd#> <filename> <mode>\00"
@errno = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 4
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i64 1)
unreachable
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 2
%15 = load i8*, i8** %14
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 3
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
%20 = call i32 @open(i8* %15, i32 %19)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %12
%23 = load i64, i64* @errno
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i64, i64* @errno
br label %28
27: ; preds = %22
br label %28
28: ; preds = %27, %25
%29 = phi i64 [ %26, %25 ], [ 255, %27 ]
%30 = call i32 @exit(i64 %29)
unreachable
31: ; preds = %12
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
%36 = load i32, i32* %6
%37 = call i32 @send_fd(i32 %35, i32 %36)
ret i32 %37
}

LLVM IR (-O2):
@.str = [37 x i8] c"openfile <sockfd#> <filename> <mode>\00"
@errno = global i64 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i64 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 3
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = tail call i32 @open(i8* %9, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %7
%16 = load i64, i64* @errno
%17 = icmp sgt i64 %16, 0
%18 = select i1 %17, i64 %16, i64 255
%19 = tail call i32 @exit(i64 %18)
unreachable
20: ; preds = %7
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i32 @atoi(i8* %22)
%24 = tail call i32 @send_fd(i32 %23, i32 %13)
ret i32 %24
}