#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ getchar();
int mkstemp(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_msgs(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ unlink(char *);
int main(void) {
  char template[] = "lockexampleXXXXXX";
  int tmpfd = mkstemp(template);
  if (tmpfd < 0) {
    perror("Couldn't create temporary file");
    exit(EXIT_FAILURE);
  }
  printf("Created temporary file %s\n", template);
  pid_t f = fork();
  if (f < 0) {
    perror("fork() failed");
    exit(EXIT_FAILURE);
  }
  if (f == 0) {
    print_msgs("child: ", tmpfd);
  } else {
    print_msgs("parent: ", tmpfd);
  }
  printf("Press any key to exit\n");
  getchar();
  if (f != 0 && unlink(template) < 0) {
    fprintf(stderr, "unlink() failed on %s: %s\n", template, strerror(errno));
    exit(EXIT_FAILURE);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe4a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe47(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe44(%rip),%ax
mov    %ax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1200 <main+0x60>
lea    0xe25(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x20(%rbp),%rsi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1242 <main+0xa2>
lea    0xe1d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jne    1261 <main+0xc1>
mov    -0x24(%rbp),%esi
lea    0xe03(%rip),%rdi
call   1070 <print_msgs@plt>
jmp    1270 <main+0xd0>
mov    -0x24(%rbp),%esi
lea    0xdf7(%rip),%rdi
call   1070 <print_msgs@plt>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
cmpq   $0x0,-0x30(%rbp)
je     12e9 <main+0x149>
lea    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    12e9 <main+0x149>
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdac(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movaps 0xe43(%rip),%xmm0
movaps %xmm0,(%rsp)
movw   $0x58,0x10(%rsp)
mov    %rsp,%rdi
call   1090 <mkstemp@plt>
test   %eax,%eax
js     123d <main+0x8d>
mov    %eax,%ebp
lea    0xe54(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1246 <main+0x96>
mov    %rax,%rbx
lea    0xe5d(%rip),%rax
lea    0xe5e(%rip),%rdi
cmove  %rax,%rdi
mov    %ebp,%esi
call   1080 <print_msgs@plt>
lea    0xe70(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
test   %rbx,%rbx
je     1234 <main+0x84>
mov    %rsp,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
js     1254 <main+0xa4>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdce(%rip),%rdi
jmp    124d <main+0x9d>
lea    0xdff(%rip),%rdi
call   10a0 <perror@plt>
jmp    127f <main+0xcf>
mov    0x2d85(%rip),%rax
mov    (%rax),%ebx
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdf9(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@__const.main.template = [18 x i8] c"lockexampleXXXXXX\00"
@.str = [31 x i8] c"Couldn't create temporary file\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Created temporary file %s\0A\00"
@.str.2 = [14 x i8] c"fork() failed\00"
@.str.3 = [8 x i8] c"child: \00"
@.str.4 = [9 x i8] c"parent: \00"
@.str.5 = [23 x i8] c"Press any key to exit\0A\00"
@stderr = global i32 0
@.str.6 = [27 x i8] c"unlink() failed on %s: %s\0A\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [18 x i8]
%3 = alloca i32
%4 = alloca i64
store i32 0, i32* %1
%5 = bitcast [18 x i8]* %2 to i8*
%6 = getelementptr inbounds [18 x i8], [18 x i8]* %2, i64 0, i64 0
%7 = call i32 @mkstemp(i8* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = getelementptr inbounds [18 x i8], [18 x i8]* %2, i64 0, i64 0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = call i64 (...) @fork()
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = load i64, i64* %4
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %3
%29 = call i32 @print_msgs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %33
30: ; preds = %24
%31 = load i32, i32* %3
%32 = call i32 @print_msgs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %30, %27
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 (...) @getchar()
%36 = load i64, i64* %4
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %33
%39 = getelementptr inbounds [18 x i8], [18 x i8]* %2, i64 0, i64 0
%40 = call i64 @unlink(i8* %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = getelementptr inbounds [18 x i8], [18 x i8]* %2, i64 0, i64 0
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %44, i8* %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %38, %33
ret i32 0
}

LLVM IR (-O2):
@__const.main.template = [18 x i8] c"lockexampleXXXXXX\00"
@.str = [31 x i8] c"Couldn't create temporary file\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Created temporary file %s\0A\00"
@.str.2 = [14 x i8] c"fork() failed\00"
@.str.3 = [8 x i8] c"child: \00"
@.str.4 = [9 x i8] c"parent: \00"
@stderr = global i32 0
@.str.6 = [27 x i8] c"unlink() failed on %s: %s\0A\00"
@errno = global i32 0
@str = [22 x i8] c"Press any key to exit\00"
define i32 @main() {
%1 = alloca [18 x i8]
%2 = getelementptr inbounds [18 x i8], [18 x i8]* %1, i64 0, i64 0
%3 = call i32 @mkstemp(i8* %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%11 = call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %9
%18 = icmp eq i64 %11, 0
%19 = select i1 %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0)
%20 = call i32 @print_msgs(i8* %19, i32 %3)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%22 = call i32 (...) @getchar()
br i1 %18, label %33, label %23
23: ; preds = %17
%24 = call i64 @unlink(i8* %2)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %2, i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %23, %17
ret i32 0
}