#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 */
struct flock {
  int l_len;
  int /*<<< orphan*/ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ child(int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int fork();
int mkstemp(char *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int /*<<< orphan*/ unlink(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int main(void) {
  char path_template[] = "/tmp/fork-11-1-XXXXXX";
  int fd, child_stat, result = 2;
  pid_t child_pid;
  struct flock fl = {
      .l_type = F_WRLCK,
      .l_whence = SEEK_SET,
      .l_start = 0,
      .l_len = 100,
  };
  fd = mkstemp(path_template);
  if (fd == -1) {
    printf("Could not open temporary file: %s (%d)\n", strerror(errno), errno);
    return result;
  }
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    printf("Could not set initial lock: %s (%d)\n", strerror(errno), errno);
    goto cleanup;
  }
  child_pid = fork();
  if (child_pid == -1) {
    printf("Fork failed: %s (%d)\n", strerror(errno), errno);
    goto cleanup;
  }
  if (child_pid == 0)
    exit(child(fd));
  if (waitpid(child_pid, &child_stat, 0) == -1) {
    printf("Wait failed: %s (%d)\n", strerror(errno), errno);
    goto cleanup;
  }
  if (WIFEXITED(child_stat))
    result = WEXITSTATUS(child_stat);
  else
    printf("Child terminated abnormally!\n");
cleanup:
  close(fd);
  unlink(path_template);
  return result;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe2a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe27(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe25(%rip),%eax
mov    %eax,-0x10(%rbp)
mov    0xe1f(%rip),%ax
mov    %ax,-0xc(%rbp)
movl   $0x2,-0x2c(%rbp)
movl   $0x64,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    126f <main+0xaf>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13a4 <main+0x1e4>
mov    -0x24(%rbp),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12ba <main+0xfa>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    138d <main+0x1cd>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    12fb <main+0x13b>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cf2(%rip),%rax
mov    (%rax),%edx
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    138d <main+0x1cd>
cmpl   $0x0,-0x30(%rbp)
jne    1314 <main+0x154>
mov    -0x24(%rbp),%edi
call   1030 <child@plt>
mov    %eax,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1358 <main+0x198>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2c95(%rip),%rax
mov    (%rax),%edx
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    138d <main+0x1cd>
mov    -0x28(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     137a <main+0x1ba>
mov    -0x28(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,-0x2c(%rbp)
jmp    1388 <main+0x1c8>
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    138d <main+0x1cd>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
lea    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
movaps 0xe23(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movabs $0x5858585858582d,%rax
mov    %rax,0x2e(%rsp)
movq   $0x64,0x10(%rsp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x20(%rsp),%rdi
call   1090 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1290 <main+0xc0>
mov    %eax,%ebx
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1060 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12b8 <main+0xe8>
xor    %eax,%eax
call   10f0 <fork@plt>
cmp    $0xffffffff,%eax
je     12d3 <main+0x103>
test   %eax,%eax
je     133e <main+0x16e>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    $0xffffffff,%eax
je     12ee <main+0x11e>
mov    0xc(%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
je     1330 <main+0x160>
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%ebp
jmp    1316 <main+0x146>
mov    0x2d41(%rip),%rbx
mov    (%rbx),%edi
call   10e0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd6f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%ebp
jmp    1327 <main+0x157>
mov    0x2d19(%rip),%rbp
mov    0x0(%rbp),%edi
call   10e0 <strerror@plt>
mov    0x0(%rbp),%edx
lea    0xd6d(%rip),%rdi
jmp    1307 <main+0x137>
mov    0x2cfe(%rip),%rbp
mov    0x0(%rbp),%edi
call   10e0 <strerror@plt>
mov    0x0(%rbp),%edx
lea    0xd77(%rip),%rdi
jmp    1307 <main+0x137>
mov    0x2ce3(%rip),%rbp
mov    0x0(%rbp),%edi
call   10e0 <strerror@plt>
mov    0x0(%rbp),%edx
lea    0xd72(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
lea    0x20(%rsp),%rdi
call   1040 <unlink@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd58(%rip),%rdi
call   1050 <puts@plt>
jmp    1311 <main+0x141>
mov    %ebx,%edi
call   1030 <child@plt>
mov    %eax,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32 }
@__const.main.path_template = [22 x i8] c"/tmp/fork-11-1-XXXXXX\00"
@SEEK_SET = global i32 0
@F_WRLCK = global i32 0
@.str = [40 x i8] c"Could not open temporary file: %s (%d)\0A\00"
@errno = global i32 0
@F_SETLK = global i32 0
@.str.1 = [37 x i8] c"Could not set initial lock: %s (%d)\0A\00"
@.str.2 = [22 x i8] c"Fork failed: %s (%d)\0A\00"
@.str.3 = [22 x i8] c"Wait failed: %s (%d)\0A\00"
@.str.4 = [30 x i8] c"Child terminated abnormally!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [22 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.flock
store i32 0, i32* %1
%8 = bitcast [22 x i8]* %2 to i8*
store i32 2, i32* %5
%9 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 100, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
%12 = load i32, i32* @SEEK_SET
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
%14 = load i32, i32* @F_WRLCK
store i32 %14, i32* %13
%15 = getelementptr inbounds [22 x i8], [22 x i8]* %2, i64 0, i64 0
%16 = call i32 @mkstemp(i8* %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %0
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = load i32, i32* @errno
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %22)
%24 = load i32, i32* %5
store i32 %24, i32* %1
br label %76
25: ; preds = %0
%26 = load i32, i32* %3
%27 = load i32, i32* @F_SETLK
%28 = call i32 @fcntl(i32 %26, i32 %27, %struct.flock* %7)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = load i32, i32* @errno
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %32, i32 %33)
br label %70
35: ; preds = %25
%36 = call i32 (...) @fork()
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = load i32, i32* @errno
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32 %42)
br label %70
44: ; preds = %35
%45 = load i32, i32* %6
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32, i32* %3
%49 = call i32 @child(i32 %48)
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %44
%52 = load i32, i32* %6
%53 = call i32 @waitpid(i32 %52, i32* %4, i32 0)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %51
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = load i32, i32* @errno
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %57, i32 %58)
br label %70
60: ; preds = %51
%61 = load i32, i32* %4
%62 = call i64 @WIFEXITED(i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = load i32, i32* %4
%66 = call i32 @WEXITSTATUS(i32 %65)
store i32 %66, i32* %5
br label %69
67: ; preds = %60
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %69
69: ; preds = %67, %64
br label %70
70: ; preds = %69, %55, %39, %30
%71 = load i32, i32* %3
%72 = call i32 @close(i32 %71)
%73 = getelementptr inbounds [22 x i8], [22 x i8]* %2, i64 0, i64 0
%74 = call i32 @unlink(i8* %73)
%75 = load i32, i32* %5
store i32 %75, i32* %1
br label %76
76: ; preds = %70, %19
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32 }
@__const.main.path_template = [22 x i8] c"/tmp/fork-11-1-XXXXXX\00"
@SEEK_SET = global i32 0
@F_WRLCK = global i32 0
@.str = [40 x i8] c"Could not open temporary file: %s (%d)\0A\00"
@errno = global i32 0
@F_SETLK = global i32 0
@.str.1 = [37 x i8] c"Could not set initial lock: %s (%d)\0A\00"
@.str.2 = [22 x i8] c"Fork failed: %s (%d)\0A\00"
@.str.3 = [22 x i8] c"Wait failed: %s (%d)\0A\00"
@str = [29 x i8] c"Child terminated abnormally!\00"
define i32 @main() {
%1 = alloca [22 x i8]
%2 = alloca i32
%3 = alloca %struct.flock
%4 = getelementptr inbounds [22 x i8], [22 x i8]* %1, i64 0, i64 0
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.flock* %3 to i8*
%7 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 100, i32* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%10 = load i32, i32* @SEEK_SET
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
%12 = load i32, i32* @F_WRLCK
store i32 %12, i32* %11
%13 = call i32 @mkstemp(i8* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = load i32, i32* @errno
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
br label %60
20: ; preds = %0
%21 = load i32, i32* @F_SETLK
%22 = call i32 @fcntl(i32 %13, i32 %21, %struct.flock* %3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = load i32, i32* @errno
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %27)
br label %56
29: ; preds = %20
%30 = call i32 (...) @fork()
switch i32 %30, label %39 [
i32 -1, label %31
i32 0, label %36
]
31: ; preds = %29
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = load i32, i32* @errno
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %33, i32 %34)
br label %56
36: ; preds = %29
%37 = call i32 @child(i32 %13)
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %29
%40 = call i32 @waitpid(i32 %30, i32* %2, i32 0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = load i32, i32* @errno
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %45)
br label %56
47: ; preds = %39
%48 = load i32, i32* %2
%49 = call i64 @WIFEXITED(i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %47
%52 = load i32, i32* %2
%53 = call i32 @WEXITSTATUS(i32 %52)
br label %56
54: ; preds = %47
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %56
56: ; preds = %51, %54, %42, %31, %24
%57 = phi i32 [ 2, %24 ], [ 2, %31 ], [ 2, %42 ], [ %53, %51 ], [ 2, %54 ]
%58 = call i32 @close(i32 %13)
%59 = call i32 @unlink(i8* %4)
br label %60
60: ; preds = %56, %15
%61 = phi i32 [ 2, %15 ], [ %57, %56 ]
ret i32 %61
}