#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*/ MSG_DONTWAIT;
int O_CREAT;
int O_RDWR;
int errno;
int open(char const *, int);
int /*<<< orphan*/ printf(char *, ...);
int send(int, char const *, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int /*<<< orphan*/ strlen(char const *);
int main(int argc, char **argv) {
  const char *file = "test";
  if (argc == 2)
    file = argv[1];
  int f = open(file, O_CREAT | O_RDWR);
  if (f < 0) {
    printf("open(%s) error: %d %d %s\n", file, f, errno, strerror(errno));
    return -1;
  }
  const char *foo = "foo";
  int r = send(f, foo, strlen(foo), MSG_DONTWAIT);
  if (r < 0) {
    printf("send() error: %d %d %s\n", r, errno, strerror(errno));
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1187 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11fe <main+0xae>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    %rax,%r8
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <main+0x13e>
lea    0xe1a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
mov    0x2d98(%rip),%rax
mov    (%rax),%ecx
call   1040 <send@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1287 <main+0x137>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%edx
mov    %rax,%rcx
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    128e <main+0x13e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    115f <main+0xf>
mov    0x8(%rsi),%rbx
jmp    1166 <main+0x16>
lea    0xe9a(%rip),%rbx
mov    0x2e8b(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11de <main+0x8e>
lea    0xe92(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <send@plt>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebx,%ebx
jns    120e <main+0xbe>
mov    0x2e17(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xe5a(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xfffffffe,%eax
jmp    120e <main+0xbe>
mov    0x2deb(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   1070 <strerror@plt>
lea    0xe0e(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"test\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"open(%s) error: %d %d %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@MSG_DONTWAIT = global i32 0
@.str.3 = [24 x i8] c"send() error: %d %d %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %6
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %6
br label %16
16: ; preds = %12, %2
%17 = load i8*, i8** %6
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_RDWR
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %17, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %16
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = load i32, i32* @errno
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %26, i32 %27, i8* %29)
store i32 -1, i32* %3
br label %47
31: ; preds = %16
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %8
%32 = load i32, i32* %7
%33 = load i8*, i8** %8
%34 = load i8*, i8** %8
%35 = call i32 @strlen(i8* %34)
%36 = load i32, i32* @MSG_DONTWAIT
%37 = call i32 @send(i32 %32, i8* %33, i32 %35, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %31
%41 = load i32, i32* %9
%42 = load i32, i32* @errno
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %42, i8* %44)
store i32 -2, i32* %3
br label %47
46: ; preds = %31
store i32 0, i32* %3
br label %47
47: ; preds = %46, %40, %24
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [5 x i8] c"test\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"open(%s) error: %d %d %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@MSG_DONTWAIT = global i32 0
@.str.3 = [24 x i8] c"send() error: %d %d %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %4, %2
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %8, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %8, i32 %12, i32 %15, i8* %16)
br label %27
18: ; preds = %7
%19 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @MSG_DONTWAIT
%21 = tail call i32 @send(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %24, i8* %25)
br label %27
27: ; preds = %23, %18, %14
%28 = phi i32 [ -1, %14 ], [ -2, %23 ], [ 0, %18 ]
ret i32 %28
}