#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 */
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ close(int);
scalar_t__ getpid();
scalar_t__ getsid(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sigwait(int /*<<< orphan*/ *, int *);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd, len, ret;
  sigset_t mask;
  int sig;
  if (getsid(0) != getpid())
    return 1;
  sigemptyset(&mask);
  sigaddset(&mask, SIGINT);
  sigprocmask(SIG_BLOCK, &mask, NULL);
  write(1, "ok\n", 3);
  sigwait(&mask, &sig);
  if (argc < 3)
    return 1;
  fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0600);
  if (fd < 0)
    return 1;
  len = strlen(argv[2]);
  ret = write(fd, argv[2], len);
  close(fd);
  if (ret != len) {
    unlink(argv[1]);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   1080 <getsid@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
je     11f4 <main+0x44>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x14e>
lea    -0x20(%rbp),%rdi
call   10a0 <sigemptyset@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   10d0 <sigaddset@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    $0x1,%edi
lea    0xdcf(%rip),%rsi
mov    $0x3,%edx
call   1050 <write@plt>
lea    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   10c0 <sigwait@plt>
cmpl   $0x3,-0x8(%rbp)
jge    125e <main+0xae>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x14e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    0x2d82(%rip),%rax
or     (%rax),%esi
mov    0x2d71(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a4 <main+0xf4>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x14e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x18(%rbp),%edx
call   1050 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     12f7 <main+0x147>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x14e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %edi,%edi
call   1080 <getsid@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1060 <getpid@plt>
mov    $0x1,%ebx
cmp    %rax,%rbp
jne    12a7 <main+0xf7>
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   10a0 <sigemptyset@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10d0 <sigaddset@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
lea    0xde7(%rip),%rsi
mov    $0x1,%ebx
mov    $0x1,%edi
mov    $0x3,%edx
call   1050 <write@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   10c0 <sigwait@plt>
cmp    $0x3,%r15d
jl     12a7 <main+0xf7>
mov    0x2d71(%rip),%rax
mov    0x2daa(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r14),%rdi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
test   %eax,%eax
js     12a7 <main+0xf7>
mov    %eax,%ebp
mov    0x10(%r14),%rdi
call   1070 <strlen@plt>
mov    %eax,%r15d
mov    0x10(%r14),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,%r12d
mov    %ebp,%edi
call   1090 <close@plt>
xor    %ebx,%ebx
cmp    %r15d,%r12d
je     12a7 <main+0xf7>
mov    0x8(%r14),%rdi
call   1040 <unlink@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIGINT = global i32 0
@SIG_BLOCK = global i32 0
@.str = [4 x i8] c"ok\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i64 @getsid(i32 0)
%12 = call i64 (...) @getpid()
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 1, i32* %3
br label %61
15: ; preds = %2
%16 = call i32 @sigemptyset(i32* %9)
%17 = load i32, i32* @SIGINT
%18 = call i32 @sigaddset(i32* %9, i32 %17)
%19 = load i32, i32* @SIG_BLOCK
%20 = call i32 @sigprocmask(i32 %19, i32* %9, i32* null)
%21 = call i32 @write(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%22 = call i32 @sigwait(i32* %9, i32* %10)
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 3
br i1 %24, label %25, label %26
25: ; preds = %15
store i32 1, i32* %3
br label %61
26: ; preds = %15
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %29, i32 %34, i32 384)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %26
store i32 1, i32* %3
br label %61
39: ; preds = %26
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32 @strlen(i8* %42)
store i32 %43, i32* %7
%44 = load i32, i32* %6
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = load i32, i32* %7
%49 = call i32 @write(i32 %44, i8* %47, i32 %48)
store i32 %49, i32* %8
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %8
%53 = load i32, i32* %7
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %60
55: ; preds = %39
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 1
%58 = load i8*, i8** %57
%59 = call i32 @unlink(i8* %58)
store i32 1, i32* %3
br label %61
60: ; preds = %39
store i32 0, i32* %3
br label %61
61: ; preds = %60, %55, %38, %25, %14
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@SIGINT = global i32 0
@SIG_BLOCK = global i32 0
@.str = [4 x i8] c"ok\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i64 @getsid(i32 0)
%8 = tail call i64 (...) @getpid()
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %40
10: ; preds = %2
%11 = call i32 @sigemptyset(i32* %3)
%12 = load i32, i32* @SIGINT
%13 = call i32 @sigaddset(i32* %3, i32 %12)
%14 = load i32, i32* @SIG_BLOCK
%15 = call i32 @sigprocmask(i32 %14, i32* %3, i32* null)
%16 = call i32 @write(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%17 = call i32 @sigwait(i32* %3, i32* %4)
%18 = icmp slt i32 %0, 3
br i1 %18, label %40, label %19
19: ; preds = %10
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %21, i32 %26, i32 384)
%28 = icmp slt i32 %27, 0
br i1 %28, label %40, label %29
29: ; preds = %19
%30 = getelementptr inbounds i8*, i8** %1, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @strlen(i8* %31)
%33 = load i8*, i8** %30
%34 = call i32 @write(i32 %27, i8* %33, i32 %32)
%35 = call i32 @close(i32 %27)
%36 = icmp eq i32 %34, %32
br i1 %36, label %40, label %37
37: ; preds = %29
%38 = load i8*, i8** %20
%39 = call i32 @unlink(i8* %38)
br label %40
40: ; preds = %29, %19, %10, %2, %37
%41 = phi i32 [ 1, %37 ], [ 1, %2 ], [ 1, %10 ], [ 1, %19 ], [ 0, %29 ]
ret i32 %41
}