#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 creat(char *, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  char *fname = "test.txt";
  int fd;
  if ((fd = creat(fname, 0666)) < 0) {
    perror("creat()");
    exit(-1);
  }
  printf("First printf is on the screen.\n");
  dup2(fd, 1);
  printf("Second printf is in this file.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    119a <main+0x4a>
lea    0xe7e(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
test   %eax,%eax
js     1190 <main+0x40>
mov    %eax,%ebx
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe72(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [8 x i8] c"creat()\00"
@.str.2 = [32 x i8] c"First printf is on the screen.\0A\00"
@.str.3 = [32 x i8] c"Second printf is in this file.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @creat(i8* %4, i32 438)
store i32 %5, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* %3
%13 = call i32 @dup2(i32 %12, i32 1)
%14 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [8 x i8] c"creat()\00"
@str = [31 x i8] c"First printf is on the screen.\00"
@str.4 = [31 x i8] c"Second printf is in this file.\00"
define i32 @main() {
%1 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 438)
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @dup2(i32 %1, i32 1)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0))
ret i32 0
}