#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 O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ dup2(int, int);
int errno;
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int main(int args, char *argv[]) {
  int err;
  if (args != 2) {
    printf("bad usage: ie ./dup /dev/ttyUSB0");
    return -1;
  }
  int fd = open(argv[1], O_RDONLY | O_NONBLOCK);
  if (fd == -1) {
    err = errno;
    printf("open %s\n", strerror(err));
    return -1;
  }
  dup2(fd, 1);
  while (1) {
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     117a <main+0x3a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <main+0xa9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11d7 <main+0x97>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <main+0xa9>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
jmp    11e4 <main+0xa4>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
cmp    $0x2,%edi
jne    1182 <main+0x42>
mov    0x8(%rsi),%rdi
mov    0x2e8f(%rip),%rax
mov    0x2e90(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1192 <main+0x52>
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
jmp    1180 <main+0x40>
lea    0xe77(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b1 <main+0x71>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe7a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [33 x i8] c"bad usage: ie ./dup /dev/ttyUSB0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.1 = [9 x i8] c"open %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %31
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = load i32, i32* @O_NONBLOCK
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %15, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %12
%23 = load i32, i32* @errno
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 -1, i32* %3
br label %31
27: ; preds = %12
%28 = load i32, i32* %7
%29 = call i32 @dup2(i32 %28, i32 1)
br label %30
30: ; preds = %27, %30
br label %30
31: ; preds = %22, %10
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [33 x i8] c"bad usage: ie ./dup /dev/ttyUSB0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.1 = [9 x i8] c"open %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %21
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %8, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %6
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %21
18: ; preds = %6
%19 = tail call i32 @dup2(i32 %12, i32 1)
br label %20
20: ; preds = %20, %18
br label %20
21: ; preds = %14, %4
ret i32 -1
}