#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int main() {
  int fd;
  fd = open("/tmp/out", O_WRONLY | O_CREAT | O_TRUNC, 0600);
  if (fd < 0) {
    perror("open()");
    exit(1);
  }
  dup2(fd, 1);
  if (fd != 1)
    close(fd);
  puts("Hello!");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11be <main+0x5e>
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmpl   $0x1,-0x8(%rbp)
je     11dd <main+0x7d>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e60(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
lea    0xe7d(%rip),%rdi
mov    $0x180,%edx
call   1060 <open@plt>
test   %eax,%eax
jns    11a7 <main+0x47>
lea    0xe71(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmp    $0x1,%ebx
je     11c1 <main+0x61>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/tmp/out\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"Hello!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 384)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i32 @dup2(i32 %15, i32 1)
%17 = load i32, i32* %2
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %19, %14
%23 = call i32 @puts(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"/tmp/out\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"Hello!\00"
define i32 @main() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i32 384)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32 @dup2(i32 %6, i32 1)
%13 = icmp eq i32 %6, 1
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @close(i32 %6)
br label %16
16: ; preds = %14, %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 0)
unreachable
}