#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_CREAT;
int O_RDONLY;
int /*<<< orphan*/ STDIN_FILENO;
int S_IRUSR;
int S_IWUSR;
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int main(void) {
  int fd;
  if ((fd = open("my.file", O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {
    perror("Couldn't open my.file");
    exit(1);
  }
  if (dup2(fd, STDIN_FILENO) == -1) {
    perror("Could not redirect stdin");
    exit(2);
  }
  execl("/usr/bin/sort", "sort", "-k+7", NULL);
  perror("Exec failed");
  exit(3);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    0x2e57(%rip),%rax
or     (%rax),%edx
lea    0xe84(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11b1 <main+0x61>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11e1 <main+0x91>
lea    0xe44(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
lea    0xe47(%rip),%rdi
lea    0xe49(%rip),%rsi
lea    0xe47(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <execl@plt>
lea    0xe3c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e88(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5e(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe92(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    119c <main+0x4c>
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11c7 <main+0x77>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
lea    0xe61(%rip),%rdi
lea    0xe63(%rip),%rsi
lea    0xe61(%rip),%rdx
xor    %ecx,%ecx
call   1070 <execl@plt>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"my.file\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [22 x i8] c"Couldn't open my.file\00"
@STDIN_FILENO = global i32 0
@.str.2 = [25 x i8] c"Could not redirect stdin\00"
@.str.3 = [14 x i8] c"/usr/bin/sort\00"
@.str.4 = [5 x i8] c"sort\00"
@.str.5 = [5 x i8] c"-k+7\00"
@.str.6 = [12 x i8] c"Exec failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
store i32 %9, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %14
%23 = call i32 @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32* null)
%24 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%25 = call i32 @exit(i32 3)
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"my.file\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [22 x i8] c"Couldn't open my.file\00"
@STDIN_FILENO = global i32 0
@.str.2 = [25 x i8] c"Could not redirect stdin\00"
@.str.3 = [14 x i8] c"/usr/bin/sort\00"
@.str.4 = [5 x i8] c"sort\00"
@.str.5 = [5 x i8] c"-k+7\00"
@.str.6 = [12 x i8] c"Exec failed\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @STDIN_FILENO
%14 = tail call i32 @dup2(i32 %7, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 2)
unreachable
19: ; preds = %12
%20 = tail call i32 @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32* null)
%21 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%22 = tail call i32 @exit(i32 3)
unreachable
}