#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
void Josephusproblem4() {
  struct josephus {
    int playerid;
    struct josephus *next;
  };
  struct josephus *start4, *ptr, *newnode;
  int n, k, i, count;
  printf("\n\t\t\t\t\t\t\t\t\tEnter the number of players: ");
  scanf("%d", &n);
  printf("\n\t\t\t\t\t\t\tEnter the value of k (every kth player gets "
         "eliminated): ");
  scanf("%d", &k);
  start4 = (struct josephus *)malloc(sizeof(struct josephus));
  start4->playerid = 1;
  ptr = start4;
  for (i = 2; i <= n; i++) {
    newnode = (struct josephus *)malloc(sizeof(struct josephus));
    ptr->next = newnode;
    newnode->playerid = i;
    newnode->next = start4;
    ptr = newnode;
  }
  for (count = n; count > 1; count--) {
    for (i = 0; i < k - 1; ++i)
      ptr = ptr->next;
    ptr->next = ptr->next->next;
  }
  printf("\n\t\t\t\t\t\t\t\t\t     The winner is player %d", ptr->playerid);
}


x86 assembly (clang, -O0):
<Josephusproblem4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf41(%rip),%rdi
lea    -0x1c(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf23(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <scanf@plt>
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x2,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     11ec <Josephusproblem4+0xbc>
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119b <Josephusproblem4+0x6b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jle    1250 <Josephusproblem4+0x120>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    122e <Josephusproblem4+0xfe>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1203 <Josephusproblem4+0xd3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    11f2 <Josephusproblem4+0xc2>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Josephusproblem4>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf41(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0xec7(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
mov    $0x10,%edi
call   1040 <malloc@plt>
movl   $0x1,(%rax)
cmpl   $0x2,0x8(%rsp)
jl     125b <Josephusproblem4+0x12b>
mov    %rax,%r14
mov    $0x1,%ebp
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,0x8(%rbx)
mov    %ebp,(%rax)
mov    %r14,0x8(%rax)
mov    0x8(%rsp),%ecx
mov    %rax,%rbx
cmp    %ecx,%ebp
jl     11a0 <Josephusproblem4+0x70>
cmp    $0x2,%ecx
jl     125b <Josephusproblem4+0x12b>
mov    0xc(%rsp),%edx
lea    -0x1(%rdx),%r8d
lea    -0x2(%rdx),%edi
mov    %r8d,%ebp
and    $0x7,%ebp
and    $0xfffffff8,%r8d
jmp    1206 <Josephusproblem4+0xd6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rax),%rsi
mov    0x8(%rsi),%rsi
mov    %rsi,0x8(%rax)
lea    -0x1(%rcx),%esi
cmp    $0x2,%ecx
mov    %esi,%ecx
jle    125b <Josephusproblem4+0x12b>
cmp    $0x2,%edx
jl     11f0 <Josephusproblem4+0xc0>
cmp    $0x7,%edi
jb     1245 <Josephusproblem4+0x115>
mov    %r8d,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rax),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%rax
add    $0xfffffff8,%ebx
jne    1220 <Josephusproblem4+0xf0>
test   %ebp,%ebp
je     11f0 <Josephusproblem4+0xc0>
mov    %ebp,%esi
nopl   0x0(%rax,%rax,1)
mov    0x8(%rax),%rax
add    $0xffffffff,%esi
jne    1250 <Josephusproblem4+0x120>
jmp    11f0 <Josephusproblem4+0xc0>
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.josephus = type { i32, %struct.josephus* }
@.str = [40 x i8] c"\0A\09\09\09\09\09\09\09\09\09Enter the number of players: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [66 x i8] c"\0A\09\09\09\09\09\09\09Enter the value of k (every kth player gets eliminated): \00"
@.str.3 = [39 x i8] c"\0A\09\09\09\09\09\09\09\09\09 The winner is player %d\00"
define void @Josephusproblem4() {
%1 = alloca %struct.josephus*
%2 = alloca %struct.josephus*
%3 = alloca %struct.josephus*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%12 = call i64 @malloc(i32 16)
%13 = inttoptr i64 %12 to %struct.josephus*
store %struct.josephus* %13, %struct.josephus** %1
%14 = load %struct.josephus*, %struct.josephus** %1
%15 = getelementptr inbounds %struct.josephus, %struct.josephus* %14, i32 0, i32 0
store i32 1, i32* %15
%16 = load %struct.josephus*, %struct.josephus** %1
store %struct.josephus* %16, %struct.josephus** %2
store i32 2, i32* %6
br label %17
17: ; preds = %34, %0
%18 = load i32, i32* %6
%19 = load i32, i32* %4
%20 = icmp sle i32 %18, %19
br i1 %20, label %21, label %37
21: ; preds = %17
%22 = call i64 @malloc(i32 16)
%23 = inttoptr i64 %22 to %struct.josephus*
store %struct.josephus* %23, %struct.josephus** %3
%24 = load %struct.josephus*, %struct.josephus** %3
%25 = load %struct.josephus*, %struct.josephus** %2
%26 = getelementptr inbounds %struct.josephus, %struct.josephus* %25, i32 0, i32 1
store %struct.josephus* %24, %struct.josephus** %26
%27 = load i32, i32* %6
%28 = load %struct.josephus*, %struct.josephus** %3
%29 = getelementptr inbounds %struct.josephus, %struct.josephus* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load %struct.josephus*, %struct.josephus** %1
%31 = load %struct.josephus*, %struct.josephus** %3
%32 = getelementptr inbounds %struct.josephus, %struct.josephus* %31, i32 0, i32 1
store %struct.josephus* %30, %struct.josephus** %32
%33 = load %struct.josephus*, %struct.josephus** %3
store %struct.josephus* %33, %struct.josephus** %2
br label %34
34: ; preds = %21
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %17
37: ; preds = %17
%38 = load i32, i32* %4
store i32 %38, i32* %7
br label %39
39: ; preds = %63, %37
%40 = load i32, i32* %7
%41 = icmp sgt i32 %40, 1
br i1 %41, label %42, label %66
42: ; preds = %39
store i32 0, i32* %6
br label %43
43: ; preds = %52, %42
%44 = load i32, i32* %6
%45 = load i32, i32* %5
%46 = sub nsw i32 %45, 1
%47 = icmp slt i32 %44, %46
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load %struct.josephus*, %struct.josephus** %2
%50 = getelementptr inbounds %struct.josephus, %struct.josephus* %49, i32 0, i32 1
%51 = load %struct.josephus*, %struct.josephus** %50
store %struct.josephus* %51, %struct.josephus** %2
br label %52
52: ; preds = %48
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %43
55: ; preds = %43
%56 = load %struct.josephus*, %struct.josephus** %2
%57 = getelementptr inbounds %struct.josephus, %struct.josephus* %56, i32 0, i32 1
%58 = load %struct.josephus*, %struct.josephus** %57
%59 = getelementptr inbounds %struct.josephus, %struct.josephus* %58, i32 0, i32 1
%60 = load %struct.josephus*, %struct.josephus** %59
%61 = load %struct.josephus*, %struct.josephus** %2
%62 = getelementptr inbounds %struct.josephus, %struct.josephus* %61, i32 0, i32 1
store %struct.josephus* %60, %struct.josephus** %62
br label %63
63: ; preds = %55
%64 = load i32, i32* %7
%65 = add nsw i32 %64, -1
store i32 %65, i32* %7
br label %39
66: ; preds = %39
%67 = load %struct.josephus*, %struct.josephus** %2
%68 = getelementptr inbounds %struct.josephus, %struct.josephus* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %69)
ret void
}

LLVM IR (-O2):
%struct.josephus = type { i32, %struct.josephus* }
@.str = [40 x i8] c"\0A\09\09\09\09\09\09\09\09\09Enter the number of players: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [66 x i8] c"\0A\09\09\09\09\09\09\09Enter the value of k (every kth player gets eliminated): \00"
@.str.3 = [39 x i8] c"\0A\09\09\09\09\09\09\09\09\09 The winner is player %d\00"
define void @Josephusproblem4() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = call i64 @malloc(i32 16)
%10 = inttoptr i64 %9 to %struct.josephus*
%11 = getelementptr inbounds %struct.josephus, %struct.josephus* %10, i64 0, i32 0
store i32 1, i32* %11
%12 = load i32, i32* %1
%13 = icmp slt i32 %12, 2
br i1 %13, label %80, label %26
14: ; preds = %26
%15 = inttoptr i64 %29 to %struct.josephus*
%16 = icmp sgt i32 %35, 1
br i1 %16, label %17, label %80
17: ; preds = %14
%18 = load i32, i32* %2
%19 = add i32 %18, -1
%20 = icmp sgt i32 %18, 1
%21 = add i32 %18, -2
%22 = and i32 %19, 7
%23 = icmp ult i32 %21, 7
%24 = and i32 %19, -8
%25 = icmp eq i32 %22, 0
br label %37
26: ; preds = %0, %26
%27 = phi i32 [ %34, %26 ], [ 2, %0 ]
%28 = phi %struct.josephus* [ %30, %26 ], [ %10, %0 ]
%29 = call i64 @malloc(i32 16)
%30 = inttoptr i64 %29 to %struct.josephus*
%31 = getelementptr inbounds %struct.josephus, %struct.josephus* %28, i64 0, i32 1
store %struct.josephus* %30, %struct.josephus** %31
%32 = getelementptr inbounds %struct.josephus, %struct.josephus* %30, i64 0, i32 0
store i32 %27, i32* %32
%33 = getelementptr inbounds %struct.josephus, %struct.josephus* %30, i64 0, i32 1
store %struct.josephus* %10, %struct.josephus** %33
%34 = add nuw nsw i32 %27, 1
%35 = load i32, i32* %1
%36 = icmp slt i32 %27, %35
br i1 %36, label %26, label %14
37: ; preds = %17, %72
%38 = phi i32 [ %35, %17 ], [ %78, %72 ]
%39 = phi %struct.josephus* [ %15, %17 ], [ %73, %72 ]
br i1 %20, label %40, label %72
40: ; preds = %37
br i1 %23, label %62, label %41
41: ; preds = %40, %41
%42 = phi %struct.josephus* [ %59, %41 ], [ %39, %40 ]
%43 = phi i32 [ %60, %41 ], [ 0, %40 ]
%44 = getelementptr inbounds %struct.josephus, %struct.josephus* %42, i64 0, i32 1
%45 = load %struct.josephus*, %struct.josephus** %44
%46 = getelementptr inbounds %struct.josephus, %struct.josephus* %45, i64 0, i32 1
%47 = load %struct.josephus*, %struct.josephus** %46
%48 = getelementptr inbounds %struct.josephus, %struct.josephus* %47, i64 0, i32 1
%49 = load %struct.josephus*, %struct.josephus** %48
%50 = getelementptr inbounds %struct.josephus, %struct.josephus* %49, i64 0, i32 1
%51 = load %struct.josephus*, %struct.josephus** %50
%52 = getelementptr inbounds %struct.josephus, %struct.josephus* %51, i64 0, i32 1
%53 = load %struct.josephus*, %struct.josephus** %52
%54 = getelementptr inbounds %struct.josephus, %struct.josephus* %53, i64 0, i32 1
%55 = load %struct.josephus*, %struct.josephus** %54
%56 = getelementptr inbounds %struct.josephus, %struct.josephus* %55, i64 0, i32 1
%57 = load %struct.josephus*, %struct.josephus** %56
%58 = getelementptr inbounds %struct.josephus, %struct.josephus* %57, i64 0, i32 1
%59 = load %struct.josephus*, %struct.josephus** %58
%60 = add i32 %43, 8
%61 = icmp eq i32 %60, %24
br i1 %61, label %62, label %41
62: ; preds = %41, %40
%63 = phi %struct.josephus* [ undef, %40 ], [ %59, %41 ]
%64 = phi %struct.josephus* [ %39, %40 ], [ %59, %41 ]
br i1 %25, label %72, label %65
65: ; preds = %62, %65
%66 = phi %struct.josephus* [ %69, %65 ], [ %64, %62 ]
%67 = phi i32 [ %70, %65 ], [ 0, %62 ]
%68 = getelementptr inbounds %struct.josephus, %struct.josephus* %66, i64 0, i32 1
%69 = load %struct.josephus*, %struct.josephus** %68
%70 = add i32 %67, 1
%71 = icmp eq i32 %70, %22
br i1 %71, label %72, label %65
72: ; preds = %62, %65, %37
%73 = phi %struct.josephus* [ %39, %37 ], [ %63, %62 ], [ %69, %65 ]
%74 = getelementptr inbounds %struct.josephus, %struct.josephus* %73, i64 0, i32 1
%75 = load %struct.josephus*, %struct.josephus** %74
%76 = getelementptr inbounds %struct.josephus, %struct.josephus* %75, i64 0, i32 1
%77 = load %struct.josephus*, %struct.josephus** %76
store %struct.josephus* %77, %struct.josephus** %74
%78 = add nsw i32 %38, -1
%79 = icmp sgt i32 %38, 2
br i1 %79, label %37, label %80
80: ; preds = %72, %0, %14
%81 = phi %struct.josephus* [ %15, %14 ], [ %10, %0 ], [ %73, %72 ]
%82 = getelementptr inbounds %struct.josephus, %struct.josephus* %81, i64 0, i32 0
%83 = load i32, i32* %82
%84 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %83)
ret void
}