#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*/ exit(int);
scalar_t__ open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ umask(int);
int main(void) {
  if (open("before.txt", O_WRONLY | O_CREAT | O_TRUNC, 0777) < 0) {
    printf("1st open call error\n");
    exit(1);
  }
  umask(077);
  if (open("after.txt", O_WRONLY | O_CREAT | O_TRUNC, 0777) < 0) {
    printf("2nd open call error\n");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
or     (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
lea    0xe8f(%rip),%rdi
mov    $0x1ff,%edx
call   1050 <open@plt>
cmp    $0x0,%rax
jge    119b <main+0x5b>
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x3f,%edi
call   1040 <umask@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
lea    0xe59(%rip),%rdi
mov    $0x1ff,%edx
call   1050 <open@plt>
cmp    $0x0,%rax
jge    11f1 <main+0xb1>
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e7c(%rip),%r14
mov    0x2ea5(%rip),%rbx
mov    (%rbx),%esi
or     (%r14),%esi
mov    0x2e91(%rip),%r15
or     (%r15),%esi
lea    0xe97(%rip),%rdi
mov    $0x1ff,%edx
call   1050 <open@plt>
test   %rax,%rax
js     11a8 <main+0x68>
mov    $0x3f,%edi
call   1040 <umask@plt>
mov    (%rbx),%esi
or     (%r14),%esi
or     (%r15),%esi
lea    0xe7a(%rip),%rdi
mov    $0x1ff,%edx
call   1050 <open@plt>
test   %rax,%rax
js     11b1 <main+0x71>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe7a(%rip),%rdi
jmp    11b8 <main+0x78>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"before.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [21 x i8] c"1st open call error\0A\00"
@.str.2 = [10 x i8] c"after.txt\00"
@.str.3 = [21 x i8] c"2nd open call error\0A\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %2, %3
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = call i64 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6, i32 511)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 @umask(i32 63)
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = call i64 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 511)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"before.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [10 x i8] c"after.txt\00"
@str = [20 x i8] c"2nd open call error\00"
@str.4 = [20 x i8] c"1st open call error\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 i64 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5, i32 511)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32 @umask(i32 63)
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = tail call i64 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 511)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %11
ret i32 0
}